mirror of
https://github.com/daylinmorgan/bbansi.git
synced 2024-12-21 18:40:44 -06:00
fix: properly highlight individual characters
This commit is contained in:
parent
4029129c75
commit
16356ad51f
2 changed files with 10 additions and 6 deletions
|
@ -18,8 +18,11 @@ type
|
||||||
proc len(span: BbSpan): int = span.slice[1] - span.slice[0]
|
proc len(span: BbSpan): int = span.slice[1] - span.slice[0]
|
||||||
|
|
||||||
proc endSpan(bbs: var BbString) =
|
proc endSpan(bbs: var BbString) =
|
||||||
if bbs.plain.len != 0:
|
if bbs.spans.len == 0: return
|
||||||
|
if bbs.plain.len >= 1:
|
||||||
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
bbs.spans[^1].slice[1] = bbs.plain.len-1
|
||||||
|
if bbs.spans[^1].len == 0 and bbs.plain.len == 0:
|
||||||
|
bbs.spans.delete(bbs.spans.len - 1)
|
||||||
|
|
||||||
proc newSpan(bbs: var BbString, styles: seq[string] = @[]) =
|
proc newSpan(bbs: var BbString, styles: seq[string] = @[]) =
|
||||||
bbs.spans.add BbSpan(styles: styles, slice: [bbs.plain.len, 0])
|
bbs.spans.add BbSpan(styles: styles, slice: [bbs.plain.len, 0])
|
||||||
|
@ -34,8 +37,8 @@ proc closeLastStyle(bbs: var BbString) =
|
||||||
bbs.newSpan newStyle
|
bbs.newSpan newStyle
|
||||||
|
|
||||||
proc addToSpan(bbs: var BbString, pattern: string) =
|
proc addToSpan(bbs: var BbString, pattern: string) =
|
||||||
bbs.endSpan
|
|
||||||
let currStyl = bbs.spans[^1].styles
|
let currStyl = bbs.spans[^1].styles
|
||||||
|
bbs.endSpan
|
||||||
bbs.newSpan currStyl & @[pattern]
|
bbs.newSpan currStyl & @[pattern]
|
||||||
|
|
||||||
proc closeStyle(bbs: var BbString, pattern: string) =
|
proc closeStyle(bbs: var BbString, pattern: string) =
|
||||||
|
@ -85,6 +88,7 @@ proc bb*(s: string): BbString =
|
||||||
resetPattern
|
resetPattern
|
||||||
else:
|
else:
|
||||||
next
|
next
|
||||||
|
|
||||||
result.closeFinalSpan
|
result.closeFinalSpan
|
||||||
|
|
||||||
proc bb*(s: string, style: string): BbString =
|
proc bb*(s: string, style: string): BbString =
|
||||||
|
@ -113,8 +117,6 @@ 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 = span.styles.join(" ").toAnsiCode
|
codes = span.styles.join(" ").toAnsiCode
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
#
|
#
|
||||||
# To run these tests, simply execute `nimble test`.
|
# To run these tests, simply execute `nimble test`.
|
||||||
|
|
||||||
import std/unittest
|
import std/[strutils,unittest]
|
||||||
|
|
||||||
import bbansi
|
import bbansi
|
||||||
|
|
||||||
template bbCheck(input: string, output: string): untyped =
|
template bbCheck(input: string, output: string): untyped =
|
||||||
check $bb(input) == output
|
check escape($bb(input)) == escape(output)
|
||||||
|
|
||||||
suite "basic":
|
suite "basic":
|
||||||
test "simple":
|
test "simple":
|
||||||
|
@ -18,6 +18,8 @@ suite "basic":
|
||||||
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"
|
bbCheck "[bold red]Bold Red Text", "\e[1;31mBold Red Text\e[0m"
|
||||||
|
bbCheck "[red]5[/]", "\e[31m5\e[0m"
|
||||||
|
bbCheck "[bold][red]5","\e[1;31m5\e[0m"
|
||||||
|
|
||||||
test "closing":
|
test "closing":
|
||||||
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
|
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
|
||||||
|
|
Loading…
Reference in a new issue