mirror of
https://github.com/daylinmorgan/bbansi.git
synced 2024-11-22 01:10: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
|
plain: string
|
||||||
spans: seq[BbSpan]
|
spans: seq[BbSpan]
|
||||||
|
|
||||||
|
proc len(span: BbSpan): int = span.slice[1] - span.slice[0]
|
||||||
|
|
||||||
proc `&`*(x: BbString, y: string): BbString =
|
proc `&`*(x: BbString, y: string): BbString =
|
||||||
result = x
|
result = x
|
||||||
result.raw &= y
|
result.raw &= y
|
||||||
|
@ -36,9 +38,11 @@ proc `$`*(bbs: BbString): string =
|
||||||
if noColor: return bbs.plain
|
if noColor: return bbs.plain
|
||||||
|
|
||||||
for span in bbs.spans:
|
for span in bbs.spans:
|
||||||
|
if span.len == 0:
|
||||||
|
continue
|
||||||
var codes = ""
|
var codes = ""
|
||||||
if span.styles.len > 0:
|
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 codes
|
||||||
result.add bbs.plain[span.slice[0]..span.slice[1]]
|
result.add bbs.plain[span.slice[0]..span.slice[1]]
|
||||||
|
@ -47,6 +51,7 @@ proc `$`*(bbs: BbString): string =
|
||||||
result.add bbReset
|
result.add bbReset
|
||||||
|
|
||||||
proc endSpan(bbs: var BbString) =
|
proc endSpan(bbs: var BbString) =
|
||||||
|
if bbs.plain.len != 0:
|
||||||
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
||||||
|
|
||||||
proc newSpan(bbs: var BbString, pattern: string) =
|
proc newSpan(bbs: var BbString, pattern: string) =
|
||||||
|
|
|
@ -21,7 +21,7 @@ suite "basic":
|
||||||
|
|
||||||
test "closing":
|
test "closing":
|
||||||
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
|
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":
|
test "abbreviated":
|
||||||
bbCheck "[b]Bold[/] Not Bold", "\e[1mBold\e[0m Not Bold"
|
bbCheck "[b]Bold[/] Not Bold", "\e[1mBold\e[0m Not Bold"
|
||||||
|
|
|
@ -19,5 +19,5 @@ suite "cli":
|
||||||
check execCmdEx(cmd).exitCode == 0
|
check execCmdEx(cmd).exitCode == 0
|
||||||
test "simple":
|
test "simple":
|
||||||
check "\e[31mRed\e[0m" == cliRun "[red]Red[/]"
|
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"
|
cliRun "'[red]Red[/] Not Red but Bold' " & "--style:bold"
|
||||||
|
|
Loading…
Reference in a new issue