mirror of
https://github.com/daylinmorgan/bbansi.git
synced 2024-11-21 17:00:44 -06:00
fix: more efficiently handle back to back styles
This commit is contained in:
parent
5564fdb18a
commit
02ebab10d1
3 changed files with 11 additions and 6 deletions
|
@ -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,7 +51,8 @@ proc `$`*(bbs: BbString): string =
|
|||
result.add bbReset
|
||||
|
||||
proc endSpan(bbs: var BbString) =
|
||||
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
||||
if bbs.plain.len != 0:
|
||||
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
||||
|
||||
proc newSpan(bbs: var BbString, pattern: string) =
|
||||
bbs.spans.add BbSpan(styles: @[pattern], slice: [bbs.plain.len, 0])
|
||||
|
|
|
@ -16,12 +16,12 @@ suite "basic":
|
|||
test "simple":
|
||||
bbCheck "[red]red text", "\e[31mred text\e[0m"
|
||||
bbCheck "[red]Red Text", "\e[31mRed Text\e[0m"
|
||||
bbCheck "[yellow]Yellow Text","\e[33mYellow Text\e[0m"
|
||||
bbCheck "[yellow]Yellow Text", "\e[33mYellow Text\e[0m"
|
||||
bbCheck "[bold red]Bold Red Text", "\e[1;31mBold Red Text\e[0m"
|
||||
|
||||
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"
|
||||
|
@ -31,7 +31,7 @@ suite "basic":
|
|||
bbCheck "[unknown]Unknown Style", "Unknown Style"
|
||||
|
||||
test "escaped":
|
||||
bbCheck "[[red] ignored pattern","[red] ignored pattern"
|
||||
bbCheck "[[red] ignored pattern", "[red] ignored pattern"
|
||||
|
||||
test "newlines":
|
||||
# Proc Strings: raw strings,
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue