A small library to print colored text to the console.
LuaText should be compatible with LuaJIT 2.1, Lua 5.1, 5.2, 5.3, and 5.4.
A simple example to render an entire string in red on a black background, underlined:
local text = require("luatext")
local my_str = text
.Text
:new("Hello world!!")
:fg(160) -- red as an ANSI256 color code
:bg({0, 0, 0}) -- black as an RGB value
:underlined()
print(my_str)
Running this code will produce the following output:
You can also add substrings to your text. The substrings will inherit the formatting of the parent text. Thus you can for instance run:
local text = require("luatext")
local my_str = text
.Text
:new()
:fg(text.Color.Red)
:append(
"Hello ",
text.Text:new("beautiful"):underlined(),
" world"
)
print(my_str)
Which will produce a fully red string, with only the substring beautiful
underlined:
A LuaText object can also be used as a standard string:
local text = require("luatext")
print("Hello "..text.Text:new("fading"):blink().." world...")
Which will print Hello fading world...
with the word fading
blinking.
For a full reference of the API, see the reference.
This library supports NO_COLOR
.
This module is hosted on LuaRocks and can thus be installed via:
luarocks install luatext
Otherwise, since the module is fully self contained, one can also simply copy the luatext.lua
into
their project.
To get a list of ANSI color codes run:
curl -s https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/e50a28ec54188d2413518788de6c6367ffcea4f7/print256colours.sh | bash
You can setup a dev environment with the needed Lua version:
# launch shell with some lua version and the dependencies installed:
nix develop .#lua52
and then test with busted
.