|
| 1 | +--[[ |
| 2 | + richterm |
| 3 | + terminal ansi formatter |
| 4 | +]] |
| 5 | + |
| 6 | +export type Formatter = (s: string, nocolor: boolean?) -> string |
| 7 | + |
| 8 | +-- @noinline (if it ever exists) |
| 9 | +local function format(s: string, open: string, close: string, replace: string): string |
| 10 | + local index = string.find(s, close, #open + 1, true) |
| 11 | + local middle = s |
| 12 | + |
| 13 | + if index then |
| 14 | + local closeLen = #close |
| 15 | + local middleTbl = {} |
| 16 | + local cursor = 1 |
| 17 | + |
| 18 | + while index do |
| 19 | + table.insert(middleTbl, string.sub(s, cursor, index - 1)) |
| 20 | + cursor = index + closeLen |
| 21 | + index = string.find(s, close, cursor, true) |
| 22 | + end |
| 23 | + |
| 24 | + middle = table.concat(middleTbl, replace) |
| 25 | + end |
| 26 | + |
| 27 | + return open .. middle .. close |
| 28 | +end |
| 29 | + |
| 30 | +local function formatter(open: string, close: string, replace: string?): Formatter |
| 31 | + replace = replace or open |
| 32 | + |
| 33 | + return function(s, nocolor) |
| 34 | + return if nocolor then s else format(s, open, close, replace :: string) |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +local richterm = { |
| 39 | + reset = formatter("\x1b[0m", "\x1b[0m"), |
| 40 | + bold = formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"), |
| 41 | + dim = formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"), |
| 42 | + italic = formatter("\x1b[3m", "\x1b[23m"), |
| 43 | + underline = formatter("\x1b[4m", "\x1b[24m"), |
| 44 | + inverse = formatter("\x1b[7m", "\x1b[27m"), |
| 45 | + hidden = formatter("\x1b[8m", "\x1b[28m"), |
| 46 | + strikethrough = formatter("\x1b[9m", "\x1b[29m"), |
| 47 | + |
| 48 | + black = formatter("\x1b[30m", "\x1b[39m"), |
| 49 | + red = formatter("\x1b[31m", "\x1b[39m"), |
| 50 | + green = formatter("\x1b[32m", "\x1b[39m"), |
| 51 | + yellow = formatter("\x1b[33m", "\x1b[39m"), |
| 52 | + blue = formatter("\x1b[34m", "\x1b[39m"), |
| 53 | + magenta = formatter("\x1b[35m", "\x1b[39m"), |
| 54 | + cyan = formatter("\x1b[36m", "\x1b[39m"), |
| 55 | + white = formatter("\x1b[37m", "\x1b[39m"), |
| 56 | + gray = formatter("\x1b[90m", "\x1b[39m"), |
| 57 | + |
| 58 | + bgBlack = formatter("\x1b[40m", "\x1b[49m"), |
| 59 | + bgRed = formatter("\x1b[41m", "\x1b[49m"), |
| 60 | + bgGreen = formatter("\x1b[42m", "\x1b[49m"), |
| 61 | + bgYellow = formatter("\x1b[43m", "\x1b[49m"), |
| 62 | + bgBlue = formatter("\x1b[44m", "\x1b[49m"), |
| 63 | + bgMagenta = formatter("\x1b[45m", "\x1b[49m"), |
| 64 | + bgCyan = formatter("\x1b[46m", "\x1b[49m"), |
| 65 | + bgWhite = formatter("\x1b[47m", "\x1b[49m"), |
| 66 | + |
| 67 | + brightBlack = formatter("\x1b[90m", "\x1b[39m"), |
| 68 | + brightRed = formatter("\x1b[91m", "\x1b[39m"), |
| 69 | + brightGreen = formatter("\x1b[92m", "\x1b[39m"), |
| 70 | + brightYellow = formatter("\x1b[93m", "\x1b[39m"), |
| 71 | + brightBlue = formatter("\x1b[94m", "\x1b[39m"), |
| 72 | + brightMagenta = formatter("\x1b[95m", "\x1b[39m"), |
| 73 | + brightCyan = formatter("\x1b[96m", "\x1b[39m"), |
| 74 | + brightWhite = formatter("\x1b[97m", "\x1b[39m"), |
| 75 | + |
| 76 | + bgBrightBlack = formatter("\x1b[100m", "\x1b[49m"), |
| 77 | + bgBrightRed = formatter("\x1b[101m", "\x1b[49m"), |
| 78 | + bgBrightGreen = formatter("\x1b[102m", "\x1b[49m"), |
| 79 | + bgBrightYellow = formatter("\x1b[103m", "\x1b[49m"), |
| 80 | + bgBrightBlue = formatter("\x1b[104m", "\x1b[49m"), |
| 81 | + bgBrightMagenta = formatter("\x1b[105m", "\x1b[49m"), |
| 82 | + bgBrightCyan = formatter("\x1b[106m", "\x1b[49m"), |
| 83 | + bgBrightWhite = formatter("\x1b[107m", "\x1b[49m"), |
| 84 | +} |
| 85 | + |
| 86 | +function richterm.combine(...: Formatter): Formatter |
| 87 | + local formatters = { ... } |
| 88 | + |
| 89 | + return function(s, nocolor) |
| 90 | + if not nocolor then |
| 91 | + for _, formatter in formatters do |
| 92 | + s = formatter(s) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + return s |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +return table.freeze(richterm) |
0 commit comments