Skip to content

Commit 30084dc

Browse files
ffrostfallaatxe
andauthored
Update ansi.luau (#160)
- ansi.luau -> colorful.luau - NO_COLOR support - export Styler type - add `combine(...)`, looks like `combine(colorful.red, colorful.bold)("This is red bold text!")` - update to use string interpolation instead of concatenation in some places --------- Co-authored-by: ariel <[email protected]>
1 parent 956f851 commit 30084dc

File tree

4 files changed

+131
-78
lines changed

4 files changed

+131
-78
lines changed

batteries/ansi.luau

Lines changed: 0 additions & 70 deletions
This file was deleted.

batteries/colorful.luau

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
local process = (require)("@lute/process") -- temporary ()
2+
3+
export type Styler = (text: string) -> string
4+
5+
local function searchForSubstringIn(str: string, substring: string, index: number)
6+
-- last argument of string.find() turns string.find into plain substring search
7+
return string.find(str, substring, index, true)
8+
end
9+
10+
local function replaceClose(str: string, close: string, replace: string, index: number?): string
11+
local result = ""
12+
local cursor = 1
13+
14+
while index do
15+
result ..= string.sub(str, cursor, index - 1) .. replace
16+
17+
cursor = index + #close
18+
19+
index = searchForSubstringIn(str, close, cursor)
20+
end
21+
22+
return result .. string.sub(str, cursor)
23+
end
24+
25+
local function formatter(open: string, close: string, replace: string?): Styler
26+
local replaceCloseWith = replace or open
27+
28+
local function created_formatter(str: string): string
29+
local index = searchForSubstringIn(str, close, #open + 1)
30+
local encasedString = if index then replaceClose(str, close, replaceCloseWith, index) else str
31+
32+
return open .. encasedString .. close
33+
end
34+
35+
return created_formatter
36+
end
37+
38+
local function combine(...: Styler): Styler
39+
local stylers = { ... }
40+
41+
local function combinedStyler(str: string): string
42+
local result = str
43+
44+
for _, styler in stylers do
45+
result = styler(result)
46+
end
47+
48+
return result
49+
end
50+
51+
return combinedStyler
52+
end
53+
54+
local function colorful(colorEnabled: boolean?)
55+
local f = formatter
56+
57+
if colorEnabled == nil then
58+
colorEnabled = process.env.NO_COLOR ~= nil
59+
end
60+
61+
if not colorEnabled then
62+
f = function()
63+
return function(str: string)
64+
return str
65+
end
66+
end
67+
end
68+
69+
return {
70+
combine = combine,
71+
72+
reset = f("\x1b[0m", "\x1b[0m"),
73+
bold = f("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
74+
dim = f("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
75+
italic = f("\x1b[3m", "\x1b[23m"),
76+
underline = f("\x1b[4m", "\x1b[24m"),
77+
inverse = f("\x1b[7m", "\x1b[27m"),
78+
hidden = f("\x1b[8m", "\x1b[28m"),
79+
strikethrough = f("\x1b[9m", "\x1b[29m"),
80+
81+
black = f("\x1b[30m", "\x1b[39m"),
82+
red = f("\x1b[31m", "\x1b[39m"),
83+
green = f("\x1b[32m", "\x1b[39m"),
84+
yellow = f("\x1b[33m", "\x1b[39m"),
85+
blue = f("\x1b[34m", "\x1b[39m"),
86+
magenta = f("\x1b[35m", "\x1b[39m"),
87+
cyan = f("\x1b[36m", "\x1b[39m"),
88+
white = f("\x1b[37m", "\x1b[39m"),
89+
gray = f("\x1b[90m", "\x1b[39m"),
90+
91+
bgBlack = f("\x1b[40m", "\x1b[49m"),
92+
bgRed = f("\x1b[41m", "\x1b[49m"),
93+
bgGreen = f("\x1b[42m", "\x1b[49m"),
94+
bgYellow = f("\x1b[43m", "\x1b[49m"),
95+
bgBlue = f("\x1b[44m", "\x1b[49m"),
96+
bgMagenta = f("\x1b[45m", "\x1b[49m"),
97+
bgCyan = f("\x1b[46m", "\x1b[49m"),
98+
bgWhite = f("\x1b[47m", "\x1b[49m"),
99+
100+
blackBright = f("\x1b[90m", "\x1b[39m"),
101+
redBright = f("\x1b[91m", "\x1b[39m"),
102+
greenBright = f("\x1b[92m", "\x1b[39m"),
103+
yellowBright = f("\x1b[93m", "\x1b[39m"),
104+
blueBright = f("\x1b[94m", "\x1b[39m"),
105+
magentaBright = f("\x1b[95m", "\x1b[39m"),
106+
cyanBright = f("\x1b[96m", "\x1b[39m"),
107+
whiteBright = f("\x1b[97m", "\x1b[39m"),
108+
109+
bgBlackBright = f("\x1b[100m", "\x1b[49m"),
110+
bgRedBright = f("\x1b[101m", "\x1b[49m"),
111+
bgGreenBright = f("\x1b[102m", "\x1b[49m"),
112+
bgYellowBright = f("\x1b[103m", "\x1b[49m"),
113+
bgBlueBright = f("\x1b[104m", "\x1b[49m"),
114+
bgMagentaBright = f("\x1b[105m", "\x1b[49m"),
115+
bgCyanBright = f("\x1b[106m", "\x1b[49m"),
116+
bgWhiteBright = f("\x1b[107m", "\x1b[49m"),
117+
}
118+
end
119+
120+
return colorful

examples/ansi.luau

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/colorful.luau

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local colorful = require("@batteries/colorful")
2+
3+
print(colorful.red("This is red text!"))
4+
print(colorful.bgRed("This is red background!"))
5+
print(colorful.bold("This is bold text!"))
6+
print(colorful.underline("This is underlined text!"))
7+
print(colorful.dim(colorful.italic("This is dim italic text!")))
8+
9+
print(colorful.red(`red({colorful.blue(`blue({colorful.green("green")})`)})`))
10+
11+
print(colorful.combine(colorful.red, colorful.bold)("This is bold red text!"))

0 commit comments

Comments
 (0)