From 4c63009f561e35a4b791ad5c59711a9d2bc57faa Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 5 Apr 2021 17:33:36 -0400 Subject: [PATCH] feat!: add more functions to ansikit, change text function to format --- libs/ansikit/init.lua | 107 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 4 deletions(-) diff --git a/libs/ansikit/init.lua b/libs/ansikit/init.lua index 67a743e..3546733 100644 --- a/libs/ansikit/init.lua +++ b/libs/ansikit/init.lua @@ -4,13 +4,78 @@ local ansikit = {} -ansikit.getCSI = function (code, endc) +ansikit.clear = function(scrollback) + typ = (scrollback and 3 or 2) + return ansikit.printCSI(typ, 'J') +end + +ansikit.clearFromPos = function(scrollback) + 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 + + 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 + +ansikit.getCode = function(code, terminate) + endc = (endc and endc or 'm') + return string.char(0x001b) .. code .. + (terminate and string.char(0x001b) .. '\\' or '') +end + +ansikit.getCSI = function(code, endc) endc = (endc and endc or 'm') return string.char(0x001b) .. '[' .. code .. endc end -ansikit.text = function (text) +ansikit.format = function(text) local colors = { + -- TODO: write codes manually instead of using functions + -- less function calls = faster ???????? reset = {'{reset}', ansikit.getCSI(0)}, bold = {'{bold}', ansikit.getCSI(1)}, dim = {'{dim}', ansikit.getCSI(2)}, @@ -25,8 +90,22 @@ ansikit.text = function (text) yellow = {'{yellow}', ansikit.getCSI(33)}, blue = {'{blue}', ansikit.getCSI(34)}, magenta = {'{magenta}', ansikit.getCSI(35)}, - cyan = {'{cyan}', ansikit.getCSI(36)} - -- TODO: Background, bright colors + cyan = {'{cyan}', ansikit.getCSI(36)}, + white = {'{white}', ansikit.getCSI(37)}, + red_bg = {'{red-bg}', ansikit.getCSI(41)}, + green_bg = {'{green-bg}', ansikit.getCSI(42)}, + yellow_bg = {'{green-bg}', ansikit.getCSI(43)}, + blue_bg = {'{blue-bg}', ansikit.getCSI(44)}, + magenta_bg = {'{magenta-bg}', ansikit.getCSI(45)}, + cyan_bg = {'{cyan-bg}', ansikit.getCSI(46)}, + white_bg = {'{white-bg}', ansikit.getCSI(47)}, + gray = {'{gray}', ansikit.getCSI(90)}, + bright_red = {'{bright-red}', ansikit.getCSI(91)}, + bright_green = {'{bright-green}', ansikit.getCSI(92)}, + bright_yellow = {'{bright-yellow}', ansikit.getCSI(93)}, + bright_blue = {'{bright-blue}', ansikit.getCSI(94)}, + bright_magenta = {'{bright-magenta}', ansikit.getCSI(95)}, + bright_cyan = {'{bright-cyan}', ansikit.getCSI(96)} } for k, v in pairs(colors) do @@ -36,5 +115,25 @@ ansikit.text = function (text) return text 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 + return ansikit