refactor: switch back to strtables

This commit is contained in:
Daylin Morgan 2023-09-13 02:33:10 -05:00
parent eef4d80cfe
commit 30a337f755
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -1,58 +1,50 @@
import std/[strtabs, strutils] import std/[strtabs, strutils]
export strtabs
let bbReset* = "\e[0m" let bbReset* = "\e[0m"
type let
BbStyle = enum bbStyles = {
bold = 1, "bold": "1",
faint, "b": "1",
italic, "faint": "2",
underline, "italic": "3",
blink, "i": "3",
reverse=7, "underline": "4",
conceal, "u": "4",
strike, "blink": "5",
black = 30 "reverse": "7",
red, "conceal": "8",
green, "strike": "9",
yellow, }.newStringTable(modeCaseInsensitive)
blue,
magenta,
cyan,
white
bbColors = {
"black": "0",
"red": "1",
"green": "2",
"yellow": "3",
"blue": "4",
"magenta": "5",
"cyan": "6",
"white": "7",
}.newStringTable(modeCaseInsensitive)
proc toAnsiCode*(s: string): string = proc toAnsiCode*(s: string): string =
var var
styles: seq[string] styles: seq[string]
bgStyle: string bgStyle: string
if " on " in s: if " on " in s:
let fg_bg_split = s.rsplit(" on ", maxsplit=1) let fgBgSplit = s.rsplit(" on ", maxsplit = 1)
styles = fg_bg_split[0].splitWhitespace() styles = fgBgSplit[0].splitWhitespace()
bgStyle = fg_bg_split[1].strip() bgStyle = fgBgSplit[1].strip()
else: else:
styles = s.splitWhitespace() styles = s.splitWhitespace()
for style in styles: for style in styles:
try: if style in bbStyles:
var bbStyle: BbStyle result.add "\e[" & bbStyles[style] & "m"
if style.len == 1: elif style in bbColors:
bbstyle = parseEnum[BbStyle]( result.add "\e[3" & bbColors[style] & "m"
case style:
of "b": "bold" let style = bgStyle
of "i": "italic" if style in bbColors:
of "u": "underline" result.add "\e[4" & bbColors[style] & "m"
else: "" # this parse enum lookup is unneccesary
)
else:
bbstyle = parseEnum[BbStyle](style)
# if we fail to parse treat it like a noop..
result.add "\e[" & $bbStyle.ord() & "m"
except ValueError: discard
try:
let bbStyle = parseEnum[BbStyle](bgStyle)
result.add "\e[" & $(bbStyle.ord()+10) & "m"
except ValueError: discard