tests + stripAnsi

This commit is contained in:
Daylin Morgan 2024-09-30 00:38:23 -05:00
parent 99fd2c1546
commit a6641c5e37
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 16 additions and 0 deletions

View file

@ -11,6 +11,17 @@ import std/[os, sequtils, strformat, strutils]
import ./bbansi/[styles, utils, colors] import ./bbansi/[styles, utils, colors]
export utils export utils
func stripAnsi*(s: string): string =
## remove all ansi escape codes from a string
var i: int
while i < s.len:
if s[i] == '\e':
while s[i] != 'm':
inc i
inc i
result.add s[i]
inc i
type type
BbSpan* = object BbSpan* = object
styles*: seq[string] styles*: seq[string]
@ -206,6 +217,7 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} =
stdout.write('\n') stdout.write('\n')
stdout.flushFile stdout.flushFile
# NOTE: could move to standlone modules in the tools/ directory
when isMainModule: when isMainModule:
import std/[parseopt, sugar] import std/[parseopt, sugar]
const version = staticExec "git describe --tags --always --dirty=-dev" const version = staticExec "git describe --tags --always --dirty=-dev"

View file

@ -65,8 +65,12 @@ suite "basic":
bbCheck "[#FF0000]red", "\e[38;2;255;0;0mred\e[0m" bbCheck "[#FF0000]red", "\e[38;2;255;0;0mred\e[0m"
suite "strutils": suite "strutils":
test "stripAnsi":
check stripAnsi("\e[1mBold String!") == "Bold String!"
test "&": test "&":
check "plain string" & bb"[red]red string" == bb"plain string[red]red string" check "plain string" & bb"[red]red string" == bb"plain string[red]red string"
check (bb"a [b]bold string") & " and plain string" == bb"a [b]bold string[/] and plain string"
test "truncate": test "truncate":
let tester = bb"[red]a red[/] [blue on red]blue on red part" let tester = bb"[red]a red[/] [blue on red]blue on red part"