Hilbish/libs/ansikit/init.lua

148 lines
3.0 KiB
Lua
Raw Normal View History

2021-10-17 17:23:58 +00:00
local lunacolors = require 'lunacolors'
2022-03-06 13:42:35 +00:00
local ansikit = {
blockCursor = 1,
blockCursorSteady = 2,
underlineCursor = 3,
underlineCursorSteady = 4,
lineCursor = 5,
lineCursorSteady = 6,
}
ansikit.clear = function(scrollback)
2021-12-04 21:54:04 +00:00
local typ = (scrollback and 3 or 2)
return ansikit.printCSI(typ, 'J')
end
2021-12-04 21:56:11 +00:00
ansikit.clearFromPos = function()
return ansikit.printCSI(0, 'J')
end
ansikit.clearLine = function()
return ansikit.printCSI(2, 'K')
end
ansikit.clearToPos = function()
return ansikit.printCSI(1, 'J')
end
ansikit.color256 = function(color)
color = (color and color or 0)
return ansikit.printCSI('38;5;' .. color)
end
ansikit.cursorDown = function(y)
y = (y and y or 1)
return ansikit.printCSI(y, 'B')
end
ansikit.cursorLeft = function(x)
x = (x and x or 1)
return ansikit.printCSI(x, 'D')
end
-- TODO: cursorPos
-- https://github.com/Luvella/AnsiKit/blob/master/lib/index.js#L90
ansikit.cursorRight = function(x)
x = (x and x or 1)
return ansikit.printCSI(x, 'C')
end
ansikit.cursorStyle = function(style)
style = (style and style or ansikit.underlineCursor)
if style > 6 or style < 1 then style = ansikit.underlineCursor end
2021-12-04 21:56:11 +00:00
return ansikit.printCSI(style, ' q')
end
ansikit.cursorTo = function(x, y)
x, y = (x and x or 1), (y and y or 1)
return ansikit.printCSI(x .. ';' .. y, 'H')
end
ansikit.cursorUp = function(y)
y = (y and y or 1)
return ansikit.printCSI(y, 'A')
end
2021-04-06 13:40:50 +00:00
ansikit.getCode = function(code, terminate)
2021-04-06 16:52:56 +00:00
return string.char(0x001b) .. code ..
(terminate and string.char(0x001b) .. '\\' or '')
2021-04-06 13:40:50 +00:00
end
ansikit.getCSI = function(code, endc)
endc = (endc and endc or 'm')
code = (code and code or '')
2021-04-06 16:52:56 +00:00
return string.char(0x001b) .. '[' .. code .. endc
2021-04-06 13:40:50 +00:00
end
ansikit.hideCursor = function()
return ansikit.printCSI('?25', 'l')
end
2021-10-17 17:23:58 +00:00
ansikit.link = function(url, text)
if not url then error 'ansikit: missing url for hyperlink' end
text = (text and text or 'link')
return lunacolors.blue('\27]8;;' .. url .. '\27\\' .. text .. '\27]8;;\27\\\n')
2021-10-17 17:23:58 +00:00
end
ansikit.print = function(text)
io.write(ansikit.format(text))
return ansikit
end
ansikit.printCode = function(code, terminate)
io.write(ansikit.getCode(code, terminate))
return ansikit
end
ansikit.printCSI = function(code, endc)
io.write(ansikit.getCSI(code, endc))
return ansikit
end
ansikit.println = function(text)
print(ansikit.print(text))
return ansikit
end
2021-04-06 13:40:50 +00:00
ansikit.reset = function()
2021-04-06 16:52:56 +00:00
return ansikit.printCode('c')
2021-04-06 13:40:50 +00:00
end
ansikit.restoreCursor = function()
return ansikit.printCSI(nil, 'u')
end
ansikit.restoreState = function()
2021-04-06 16:52:56 +00:00
return ansikit.printCode(8)
2021-04-06 13:40:50 +00:00
end
ansikit.rgb = function(r, g, b)
r = (r and r or 0)
g = (g and g or 0)
b = (b and b or 0)
2021-04-06 16:52:56 +00:00
return ansikit.printCSI('38;2;' .. r .. ';' .. g .. ';' .. b)
2021-04-06 13:40:50 +00:00
end
ansikit.saveCursor = function()
return ansikit.printCSI(nil, 's')
end
ansikit.saveState = function()
2021-04-06 16:52:56 +00:00
return ansikit.printCode(7)
2021-04-06 13:40:50 +00:00
end
ansikit.setTitle = function(text)
return ansikit.printCode(']2;' .. text, true)
2021-04-06 13:40:50 +00:00
end
ansikit.showCursor = function()
return ansikit.printCSI('?25', 'h')
end
return ansikit