From b4cfa8c3718380c6652a463e4a54d27aba6f4f94 Mon Sep 17 00:00:00 2001 From: UndeadMaelys Date: Mon, 9 May 2022 17:58:30 +0200 Subject: [PATCH] upload --- colors.lua | 18 ++++++++++ effects.lua | 12 +++++++ main.lua | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 colors.lua create mode 100644 effects.lua create mode 100644 main.lua diff --git a/colors.lua b/colors.lua new file mode 100644 index 0000000..fe8bf3e --- /dev/null +++ b/colors.lua @@ -0,0 +1,18 @@ +TERMINAL_COLOR = Enum { + "Black", + "Red", + "Green", + "Yellow", + "Blue", + "Purple", + "Cyan", + "LightGray", + "Gray", + "HighRed", + "HighGreen", + "HighYellow", + "HighBlue", + "HighPurple", + "HighCyan", + "White" +} diff --git a/effects.lua b/effects.lua new file mode 100644 index 0000000..0996b18 --- /dev/null +++ b/effects.lua @@ -0,0 +1,12 @@ +TERMINAL_EFFECT = Enum { + "Reset", + "Bold", + "Dim", + "Italic", + "Underline", + "BlinkSlow", + "BlinkFast", + "Invert", + "Conceal", + "CrossedOut" +} diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..f667114 --- /dev/null +++ b/main.lua @@ -0,0 +1,101 @@ +function Enum(tbl) + for i = 1, #tbl do + local v = tbl[i] + tbl[v] = i + end + return tbl +end + +require "colors" +require "effects" + +Enum = nil + +local function hex(str) + if tonumber(str) then return tonumber(str) end + if str == "a" then + return 10 + elseif str == "b" then + return 11 + elseif str == "c" then + return 12 + elseif str == "d" then + return 13 + elseif str == "e" then + return 14 + elseif str == "f" then + return 15 + end +end + +local function todecimal(str) + str = string.lower(str) + local number = 0 + local scale = 0 + for i=string.len(str), 1, -1 do + number = number + hex(string.sub(str:lower(),i,i))*16^scale + scale = scale + 1 + end + return math.floor(number) +end + +local function tabulateArguments(...) + local text = "" + for _, v in pairs({...}) do + if tab then + text = text .. "\t" + else + local tab = true + end + text = text .. v + end + return text +end + +function string.effect(effect, ...) + local tab = false + local text = tabulateArguments(...) + return "\027["..tostring(effect-1).."m"..text.."\027[0;m" +end + +local function get_colormode(color) + -- colors are from 0 to 1 + if type(color) == "table" then + return tostring(math.floor(color[1]*255+0.5)) ..";" .. tostring(math.floor(color[2]*255+0.5)) ..";" .. tostring(math.floor(color[3]*255+0.5)), "2" + elseif type(color) == "string" then + if string.sub(color,1,1) == "#" then color = string.sub(color,2) end + local c1 = todecimal(string.sub(color,1,2)) + local c2 = todecimal(string.sub(color,3,4)) + local c3 = todecimal(string.sub(color,5,6)) + if c1 and c2 and c3 then + return c1 .. ";" .. c2 .. ";" .. c3, "2" + end + elseif type(color) == "number" then + return color-1, "5" + end +end + +function string.color(color, ...) + local tab = false + local text = tabulateArguments(...) + local color, color_mode = get_colormode(color) + return "\027[38;" .. + color_mode .. ";" .. + color .. "m" .. + text .. "\027[0;m" +end + +function string.background_color(color, ...) + local tab = false + local text = tabulateArguments(...) + local color, color_mode = get_colormode(color) + return "\027[48;" .. + color_mode .. ";" .. + color .. "m" .. + text .. "\027[0;m" +end + +--[[ +function scrollTerminalUp(amount) + return "\027["..amount.."T" +end]] \ No newline at end of file