mirror of
https://github.com/daylinmorgan/bbansi.git
synced 2024-11-24 17:50:45 -06:00
refactor: switch back to strtables
This commit is contained in:
parent
eef4d80cfe
commit
30a337f755
1 changed files with 36 additions and 44 deletions
|
@ -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
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue