diff --git a/src/bbansi.nim b/src/bbansi.nim index ad5ec32..191828c 100644 --- a/src/bbansi.nim +++ b/src/bbansi.nim @@ -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]) diff --git a/tests/tbasic.nim b/tests/tbasic.nim index 9879dbd..5859a31 100644 --- a/tests/tbasic.nim +++ b/tests/tbasic.nim @@ -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, diff --git a/tests/tcli.nim b/tests/tcli.nim index 9780a28..3d69bef 100644 --- a/tests/tcli.nim +++ b/tests/tcli.nim @@ -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"