From 398b77cc2b48a69ac0cecdc7ade56dabc660ea0b Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 25 Sep 2024 13:25:09 -0500 Subject: [PATCH] begin 256 color implementation --- src/hwylterm/bbansi.nim | 7 ++++--- src/hwylterm/bbansi/colors.nim | 1 + src/hwylterm/bbansi/styles.nim | 25 ------------------------- src/hwylterm/bbansi/utils.nim | 16 ++++++++++++++-- tests/tbbansi.nim | 1 + 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/hwylterm/bbansi.nim b/src/hwylterm/bbansi.nim index a84a672..80a0d18 100644 --- a/src/hwylterm/bbansi.nim +++ b/src/hwylterm/bbansi.nim @@ -5,10 +5,8 @@ ]## import std/[os, sequtils, strformat, strutils] - -import ./bbansi/[styles, utils] +import ./bbansi/[styles, utils, colors] export utils -# export bbReset type BbSpan* = object @@ -118,6 +116,9 @@ proc bb*(s: string): BbString = proc bb*(s: string, style: string): BbString = bb("[" & style & "]" & s & "[/" & style & "]") +proc bb*(s: string, style: Color256): BbString = + bb(s, $style) + proc `&`*(x: BbString, y: string): BbString = result = x result.raw &= y diff --git a/src/hwylterm/bbansi/colors.nim b/src/hwylterm/bbansi/colors.nim index 8399dec..389191a 100644 --- a/src/hwylterm/bbansi/colors.nim +++ b/src/hwylterm/bbansi/colors.nim @@ -1,6 +1,7 @@ import std/[parseutils, strutils] type + Color256* = range[0..255] ColorRgb* = object red, green, blue: int ColorHex* = object diff --git a/src/hwylterm/bbansi/styles.nim b/src/hwylterm/bbansi/styles.nim index d17076d..79bffff 100644 --- a/src/hwylterm/bbansi/styles.nim +++ b/src/hwylterm/bbansi/styles.nim @@ -18,28 +18,3 @@ func toStyle*(a: BbStyleAbbr): BbStyle = of U: Underline const bbReset* = "\e[0m" -# bbStyles* = { -# "reset": "0", -# "bold": "1", -# "b": "1", -# "faint": "2", -# "italic": "3", -# "i": "3", -# "underline": "4", -# "u": "4", -# "blink": "5", -# "reverse": "7", -# "conceal": "8", -# "strike": "9", -# }.toTable -# -# bbColors* = { -# "black": "0", -# "red": "1", -# "green": "2", -# "yellow": "3", -# "blue": "4", -# "magenta": "5", -# "cyan": "6", -# "white": "7", -# }.toTable diff --git a/src/hwylterm/bbansi/utils.nim b/src/hwylterm/bbansi/utils.nim index 8022b73..7b24648 100644 --- a/src/hwylterm/bbansi/utils.nim +++ b/src/hwylterm/bbansi/utils.nim @@ -1,5 +1,6 @@ import std/[ - enumutils, os, strutils, terminal, sequtils] + enumutils, os, strutils, terminal, sequtils +] import ./[styles, colors] type @@ -28,9 +29,12 @@ func toCode(color: ColorXterm): string = "38;5;" & $ord(color) func toBgCode(color: ColorXterm): string = "48;5;" & $ord(color) func toCode(c: ColorRgb): string = "38;2;" & $c func toBgCode(c: ColorRgb): string = "48:2;" & $c +func toCode(c: Color256): string = "38;5;" & $c +func toBgCode(c: Color256): string = "48;5;" & $c const ColorXTermNames = ColorXterm.items().toSeq().mapIt(($it).toLowerAscii().capitalizeAscii()) const BbStyleNames = BbStyle.items().toSeq().mapIt(($it).toLowerAscii().capitalizeAscii()) +const ColorDigitStrings = (1..255).toSeq().mapIt($it) func isHex(s: string): bool = (s.startswith "#") and (s.len == 7) @@ -51,14 +55,20 @@ proc toAnsiCode*(s: string): string = styles = s.splitWhitespace() for style in styles: let normalizedStyle = style.normStyle + if normalizedStyle in ["B", "I", "U"]: codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode() elif normalizedStyle in BbStyleNames: codes.add parseEnum[BbStyle](normalizedStyle).toCode() - elif normalizedStyle in ColorXtermNames and bbMode == On: + + if not (bbMode == On): continue + + if normalizedStyle in ColorXtermNames: codes.add parseEnum[ColorXterm](normalizedStyle).toCode() elif normalizedStyle.isHex(): codes.add normalizedStyle.hexToRgb.toCode() + elif normalizedStyle in ColorDigitStrings: + codes.add parseInt(normalizedStyle).toCode() else: when defined(debugBB): echo "unknown style: " & normalizedStyle @@ -68,6 +78,8 @@ proc toAnsiCode*(s: string): string = codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode() elif normalizedBgStyle.isHex(): codes.add normalizedBgStyle.hexToRgb().toBgCode() + elif normalizedBgStyle in ColorDigitStrings: + codes.add parseInt(normalizedBgStyle).toCode() else: when defined(debugBB): echo "unknown bg style: " & normalizedBgStyle diff --git a/tests/tbbansi.nim b/tests/tbbansi.nim index abe35d6..8686be9 100644 --- a/tests/tbbansi.nim +++ b/tests/tbbansi.nim @@ -51,6 +51,7 @@ suite "basic": test "style full": check "[red]Red[/red]".bb == bb("Red", "red") check "[b][yellow]not yellow[/][/b]".bb == bb("[yellow]not yellow[/]", "b") + check "[9]color 9[/9]".bb == bb("color 9", 9) # syntax will change to [color(9)] test "escape": check bbEscape("[info] brackets") == "[[info] brackets"