fix: more efficiently handle back to back styles

This commit is contained in:
Daylin Morgan 2023-09-15 00:17:45 -05:00
parent 5564fdb18a
commit 02ebab10d1
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
3 changed files with 11 additions and 6 deletions

View file

@ -13,6 +13,8 @@ type
plain: string
spans: seq[BbSpan]
proc len(span: BbSpan): int = span.slice[1] - span.slice[0]
proc `&`*(x: BbString, y: string): BbString =
result = x
result.raw &= y
@ -36,9 +38,11 @@ proc `$`*(bbs: BbString): string =
if noColor: return bbs.plain
for span in bbs.spans:
if span.len == 0:
continue
var codes = ""
if span.styles.len > 0:
codes = collect(for style in span.styles: style.toAnsiCode).join("")
codes = span.styles.join(" ").toAnsiCode
result.add codes
result.add bbs.plain[span.slice[0]..span.slice[1]]
@ -47,6 +51,7 @@ proc `$`*(bbs: BbString): string =
result.add bbReset
proc endSpan(bbs: var BbString) =
if bbs.plain.len != 0:
bbs.spans[^1].slice[1] = bbs.plain.len-1
proc newSpan(bbs: var BbString, pattern: string) =

View file

@ -21,7 +21,7 @@ suite "basic":
test "closing":
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
"\e[1mBold\e[0m\e[1m\e[31m Bold Red\e[0m\e[1m Bold Only\e[0m"
"\e[1mBold\e[0m\e[1;31m Bold Red\e[0m\e[1m Bold Only\e[0m"
test "abbreviated":
bbCheck "[b]Bold[/] Not Bold", "\e[1mBold\e[0m Not Bold"

View file

@ -19,5 +19,5 @@ suite "cli":
check execCmdEx(cmd).exitCode == 0
test "simple":
check "\e[31mRed\e[0m" == cliRun "[red]Red[/]"
check "\e[1m\e[0m\e[1m\e[31mRed\e[0m\e[1m Not Red but Bold\e[0m" ==
check "\e[1;31mRed\e[0m\e[1m Not Red but Bold\e[0m" ==
cliRun "'[red]Red[/] Not Red but Bold' " & "--style:bold"