begin 256 color implementation

This commit is contained in:
Daylin Morgan 2024-09-25 13:25:09 -05:00
parent ee241e2d29
commit 398b77cc2b
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 20 additions and 30 deletions

View file

@ -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

View file

@ -1,6 +1,7 @@
import std/[parseutils, strutils]
type
Color256* = range[0..255]
ColorRgb* = object
red, green, blue: int
ColorHex* = object

View file

@ -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

View file

@ -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

View file

@ -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"