multimaze/utils/ansi.lua

98 lines
3.0 KiB
Lua

--[[
VT100 ANSI escape sequences.
Ported from the Lantern Build Engine, which si written in Bash.
Other versions:
- Bash, in the Lantern Build Engine: https://gitlab.com/sbrl/lantern-build-engine/-/blob/master/lantern.sh
- Javascript: massCode/Ansi.mjs
- C♯: massCode/Ansi.cs
Changelog:
6th September 2020:
Initial release - port complete!
]]--
local ansi
if os.getenv("NO_COLOR") ~= nil then
ansi = {
RS = "",
HC = "",
UL = "",
INV = "",
LC = "",
FBLK = "",
FRED = "",
FGRN = "",
FYEL = "",
FBLE = "",
FMAG = "",
FCYN = "",
FWHT = "",
BBLK = "",
BRED = "",
BGRN = "",
BYEL = "",
BBLE = "",
BMAG = "",
BCYN = "",
BWHT = "",
}
else
ansi = {
RS = "\27[0m", -- reset
HC = "\27[1m", -- hicolor
UL = "\27[4m", -- underline
INV = "\27[7m", -- inverse background and foreground
LC = "\27[2m", -- locolor / dim
FBLK = "\27[30m", -- foreground black
FRED = "\27[31m", -- foreground red
FGRN = "\27[32m", -- foreground green
FYEL = "\27[33m", -- foreground yellow
FBLE = "\27[34m", -- foreground blue
FMAG = "\27[35m", -- foreground magenta
FCYN = "\27[36m", -- foreground cyan
FWHT = "\27[37m", -- foreground white
BBLK = "\27[40m", -- background black
BRED = "\27[41m", -- background red
BGRN = "\27[42m", -- background green
BYEL = "\27[43m", -- background yellow
BBLE = "\27[44m", -- background blue
BMAG = "\27[45m", -- background magenta
BCYN = "\27[46m", -- background cyan
BWHT = "\27[47m", -- background white
-- Invalid escape sequence, apparently - we'll need to look into how to do it properly
-- Perhaps knowing the hex code for the character we want would be helpful
-- URL_START = '\e]8;;',
-- URL_DISPLAY_TEXT = '\e\\',
-- URL_END = '\e]8;;\e\\'
}
end
function ansi.wrap(str, start)
return start..str..ansi.RS
end
function ansi.hicol(str) return ansi.wrap(str, ansi.HC) end
function ansi.locol(str) return ansi.wrap(str, ansi.LC) end
function ansi.uline(str) return ansi.wrap(str, ansi.UL) end
function ansi.invert(str) return ansi.wrap(str, ansi.INV) end
function ansi.fblack(str) return ansi.wrap(str, ansi.FBLK) end
function ansi.fred(str) return ansi.wrap(str, ansi.FRED) end
function ansi.fgreen(str) return ansi.wrap(str, ansi.FGRN) end
function ansi.fyellow(str) return ansi.wrap(str, ansi.FYEL) end
function ansi.fblue(str) return ansi.wrap(str, ansi.FBLE) end
function ansi.fmagenta(str) return ansi.wrap(str, ansi.FMAG) end
function ansi.fcyan(str) return ansi.wrap(str, ansi.FCYN) end
function ansi.fwhite(str) return ansi.wrap(str, ansi.FWHT) end
function ansi.bblack(str) return ansi.wrap(str, ansi.FBLK) end
function ansi.bred(str) return ansi.wrap(str, ansi.BRED) end
function ansi.bgreen(str) return ansi.wrap(str, ansi.BGRN) end
function ansi.byellow(str) return ansi.wrap(str, ansi.BYEL) end
function ansi.bblue(str) return ansi.wrap(str, ansi.BBLE) end
function ansi.bmagenta(str) return ansi.wrap(str, ansi.BMAG) end
function ansi.bcyan(str) return ansi.wrap(str, ansi.BCYN) end
function ansi.bwhite(str) return ansi.wrap(str, ansi.BWHT) end
return ansi