add bbMarkup

This commit is contained in:
Daylin Morgan 2025-01-30 13:02:52 -06:00
parent 097d963b2a
commit 5d7e404d0d
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 6 additions and 0 deletions

View file

@ -353,6 +353,10 @@ func add*(x: var Bbstring, y: string) =
x.plain.add y
x.spans.add BbSpan(styles: @[], slice:[i, i + y.len - 1 ])
func bbMarkup*(s: string, style: string): string =
## enclose a string in bbansi markup for the given style
fmt"[{style}]{s}[/{style}]"
func bbEscape*(s: string): string {.inline.} =
s.replace("[", "[[").replace("\\", "\\\\")

View file

@ -16,11 +16,13 @@ suite "basic":
bbCheck "[bold red]Bold Red Text", "\e[1;38;5;1mBold Red Text\e[0m"
bbCheck "[red]5[/]", "\e[38;5;1m5\e[0m"
bbCheck "[bold][red]5","\e[1;38;5;1m5\e[0m"
check "[bold]bold[/bold]" == "bold".bbMarkup("bold")
test "compile time":
const s = bb"[red]red text"
check s == bb"[red]red text"
test "closing":
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
"\e[1mBold\e[0m\e[1;38;5;1m Bold Red\e[0m\e[1m Bold Only\e[0m"