Console Output Library
The Console Output library aids with printing text to the console. The primary focus is on making the use of ANSI console features available to you with ease.

If your terminal does not support ANSI console features, this class will fall back to safer, more standard output, rather than cluttering the screen with jibberish. As such, this library is a great tool for creating stylish console apps without having to worry about which terminal features may be available for all end users.
#Loading the Console Output Library

The Console Output library is optional. In order to use this library, you must require it in one of your source files (preferably main.lua) prior to use. It only needs to be required once in a single file. It is recommended you do this at the top of your main.lua file, outside of any functions or other code, and you may do this by using this code:

Example:
require 'console/output'

#Default Styles
The following styles are available by default:
  • default
  • petty
  • info
  • comment
  • success
  • fail
  • warning
  • error
You may also create your own styles with setStyle()
Example:
require 'console/output' local output = ConsoleOutput() output:default("Default text") output:petty("Petty text") output:info("Info text") output:comment("Comment text") output:success("Success text") output:fail("Fail text") output:warning("Warning text") output:error("Error text")

#Supported Methods #Constructor class ConsoleOutput()

Instantiates a new Console Output object. The constructor has no side effects and accepts no parameters.

Example
require 'console/output' local output = ConsoleOutput()
#ConsoleOutput:setStyle ConsoleOutput:setStyle(string name, string fmt)

Configure a style for the console output object. The name can be anything you would like to refer to this style as, and should be unique. fmt should be an ANSI-compatible format string. See ANSI console features for more information.

Example:
require 'console/output' local output = ConsoleOutput() output:setStyle('my-custom-style', "\x1b[38;5;57m%s\x1b[0m") -- Creates a bright purple style named 'my-custom-style'
#ConsoleOutput:isAnsiAvailable bool ConsoleOutput:isAnsiAvailable()

Returns whether ANSI console features are available on your machine. This should, normally, return true. If your terminal does not support console styling, this will return false

Example:
require 'console/output' local output = ConsoleOutput() if output:isAnsiAvailable() then print("Your terminal supports styling!") else print("your terminal does not support styling") end
#ConsoleOutput:write ConsoleOutput:send(string write)

Writes a raw string to the console. No newline will be appended for you.

Example:
require 'console/output' local output = ConsoleOutput() output:write("Hello World")
#ConsoleOutput:writeln ConsoleOutput:get(string msg)

Same as write(), except a newline will be appended for you.

Example:
require 'console/output' local output = ConsoleOutput() output:writeln("Hello World")
#ConsoleOutput:sstyle string ConsoleOutput:sstyle(string style, msg)

Formats and returns a message (msg) that is styled with the required style. style must be either the name of a default console style, or one that you have created yourself with setStyle()

This function will not print the message to the screen; it will only prep the output for you and return it as a string.

Example:
require 'console/output' local output = ConsoleOutput() local helloMsg = output:sstyle("success", "Hello, World!") -- Stores the string "Hello, World!" in the success styling print(helloMsg) -- Prints it to the console; a green "hello World!" (success style) will now show up
#ConsoleOutput:style ConsoleOutput:style(string style, string msg)

Exactly the same as sstyle(), except it actually does print the message to the screen.

This will not append a newline for you, so be sure to include a "\n" if needed.

Example:
require 'console/output' local output = ConsoleOutput() output:style("success", "Hello, World!\n") -- Prints it to the console; a green "hello World!" (success style) will show up
#ConsoleOutput:default ConsoleOutput:default(string msg)

Prints msg to the screen using the 'default' style. This includes a newline. This is a good choice for "normal" text.

Example:
require 'console/output' local output = ConsoleOutput() output:default("Example message")
#ConsoleOutput:petty ConsoleOutput:petty(string msg)

Prints msg to the screen using the 'petty' style. This includes a newline. This is a good choice for insignificant text.

Example:
require 'console/output' local output = ConsoleOutput() output:petty("Example message")
#ConsoleOutput:info ConsoleOutput:info(string msg)

Prints msg to the screen using the 'info' style. This includes a newline. This is a good choice for informational messages.

Example:
require 'console/output' local output = ConsoleOutput() output:info("Example message")
#ConsoleOutput:comment ConsoleOutput:comment(string msg)

Prints msg to the screen using the 'comment' style. This includes a newline. This is a good choice for code comments.

Example:
require 'console/output' local output = ConsoleOutput() output:comment("Example message")
#ConsoleOutput:success ConsoleOutput:success(string msg)

Prints msg to the screen using the 'success' style. This includes a newline. This is a good choice for success messages or other positive connotations.

Example:
require 'console/output' local output = ConsoleOutput() output:success("Example message")
#ConsoleOutput:fail ConsoleOutput:fail(string msg)

Prints msg to the screen using the 'fail' style. This includes a newline. This is a good choice for when things go wrong, but are recoverable (ie. app will continue on).

Example:
require 'console/output' local output = ConsoleOutput() output:fail("Example message")
#ConsoleOutput:warning ConsoleOutput:warning(string msg)

Prints msg to the screen using the 'warning' style. This includes a newline. This is a good choice for warning messages.

Example:
require 'console/output' local output = ConsoleOutput() output:warning("Example message")
#ConsoleOutput:error ConsoleOutput:error(string msg)

Prints msg to the screen using the 'error' style. This includes a newline. This is useful for error messages, and indicates non-recoverable errors (ie. app will not be able to continue)

Example:
require 'console/output' local output = ConsoleOutput() output:error("Example message")

Page last updated at 2024-04-02 00:50:20


Copyright 2024