mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-21 18:50:44 -06:00
begin 256 color implementation
This commit is contained in:
parent
ee241e2d29
commit
398b77cc2b
5 changed files with 20 additions and 30 deletions
|
@ -5,10 +5,8 @@
|
||||||
]##
|
]##
|
||||||
|
|
||||||
import std/[os, sequtils, strformat, strutils]
|
import std/[os, sequtils, strformat, strutils]
|
||||||
|
import ./bbansi/[styles, utils, colors]
|
||||||
import ./bbansi/[styles, utils]
|
|
||||||
export utils
|
export utils
|
||||||
# export bbReset
|
|
||||||
|
|
||||||
type
|
type
|
||||||
BbSpan* = object
|
BbSpan* = object
|
||||||
|
@ -118,6 +116,9 @@ proc bb*(s: string): BbString =
|
||||||
proc bb*(s: string, style: string): BbString =
|
proc bb*(s: string, style: string): BbString =
|
||||||
bb("[" & style & "]" & s & "[/" & style & "]")
|
bb("[" & style & "]" & s & "[/" & style & "]")
|
||||||
|
|
||||||
|
proc bb*(s: string, style: Color256): BbString =
|
||||||
|
bb(s, $style)
|
||||||
|
|
||||||
proc `&`*(x: BbString, y: string): BbString =
|
proc `&`*(x: BbString, y: string): BbString =
|
||||||
result = x
|
result = x
|
||||||
result.raw &= y
|
result.raw &= y
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import std/[parseutils, strutils]
|
import std/[parseutils, strutils]
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Color256* = range[0..255]
|
||||||
ColorRgb* = object
|
ColorRgb* = object
|
||||||
red, green, blue: int
|
red, green, blue: int
|
||||||
ColorHex* = object
|
ColorHex* = object
|
||||||
|
|
|
@ -18,28 +18,3 @@ func toStyle*(a: BbStyleAbbr): BbStyle =
|
||||||
of U: Underline
|
of U: Underline
|
||||||
|
|
||||||
const bbReset* = "\e[0m"
|
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
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[
|
import std/[
|
||||||
enumutils, os, strutils, terminal, sequtils]
|
enumutils, os, strutils, terminal, sequtils
|
||||||
|
]
|
||||||
import ./[styles, colors]
|
import ./[styles, colors]
|
||||||
|
|
||||||
type
|
type
|
||||||
|
@ -28,9 +29,12 @@ func toCode(color: ColorXterm): string = "38;5;" & $ord(color)
|
||||||
func toBgCode(color: ColorXterm): string = "48;5;" & $ord(color)
|
func toBgCode(color: ColorXterm): string = "48;5;" & $ord(color)
|
||||||
func toCode(c: ColorRgb): string = "38;2;" & $c
|
func toCode(c: ColorRgb): string = "38;2;" & $c
|
||||||
func toBgCode(c: ColorRgb): string = "48: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 ColorXTermNames = ColorXterm.items().toSeq().mapIt(($it).toLowerAscii().capitalizeAscii())
|
||||||
const BbStyleNames = BbStyle.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 =
|
func isHex(s: string): bool =
|
||||||
(s.startswith "#") and (s.len == 7)
|
(s.startswith "#") and (s.len == 7)
|
||||||
|
@ -51,14 +55,20 @@ proc toAnsiCode*(s: string): string =
|
||||||
styles = s.splitWhitespace()
|
styles = s.splitWhitespace()
|
||||||
for style in styles:
|
for style in styles:
|
||||||
let normalizedStyle = style.normStyle
|
let normalizedStyle = style.normStyle
|
||||||
|
|
||||||
if normalizedStyle in ["B", "I", "U"]:
|
if normalizedStyle in ["B", "I", "U"]:
|
||||||
codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode()
|
codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode()
|
||||||
elif normalizedStyle in BbStyleNames:
|
elif normalizedStyle in BbStyleNames:
|
||||||
codes.add parseEnum[BbStyle](normalizedStyle).toCode()
|
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()
|
codes.add parseEnum[ColorXterm](normalizedStyle).toCode()
|
||||||
elif normalizedStyle.isHex():
|
elif normalizedStyle.isHex():
|
||||||
codes.add normalizedStyle.hexToRgb.toCode()
|
codes.add normalizedStyle.hexToRgb.toCode()
|
||||||
|
elif normalizedStyle in ColorDigitStrings:
|
||||||
|
codes.add parseInt(normalizedStyle).toCode()
|
||||||
else:
|
else:
|
||||||
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
||||||
|
|
||||||
|
@ -68,6 +78,8 @@ proc toAnsiCode*(s: string): string =
|
||||||
codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode()
|
codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode()
|
||||||
elif normalizedBgStyle.isHex():
|
elif normalizedBgStyle.isHex():
|
||||||
codes.add normalizedBgStyle.hexToRgb().toBgCode()
|
codes.add normalizedBgStyle.hexToRgb().toBgCode()
|
||||||
|
elif normalizedBgStyle in ColorDigitStrings:
|
||||||
|
codes.add parseInt(normalizedBgStyle).toCode()
|
||||||
else:
|
else:
|
||||||
when defined(debugBB): echo "unknown bg style: " & normalizedBgStyle
|
when defined(debugBB): echo "unknown bg style: " & normalizedBgStyle
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,7 @@ suite "basic":
|
||||||
test "style full":
|
test "style full":
|
||||||
check "[red]Red[/red]".bb == bb("Red", "red")
|
check "[red]Red[/red]".bb == bb("Red", "red")
|
||||||
check "[b][yellow]not yellow[/][/b]".bb == bb("[yellow]not yellow[/]", "b")
|
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":
|
test "escape":
|
||||||
check bbEscape("[info] brackets") == "[[info] brackets"
|
check bbEscape("[info] brackets") == "[[info] brackets"
|
||||||
|
|
Loading…
Reference in a new issue