From a6641c5e37737d6c8d79c3ad33b80e667488fce4 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 30 Sep 2024 00:38:23 -0500 Subject: [PATCH] tests + stripAnsi --- src/hwylterm/bbansi.nim | 12 ++++++++++++ tests/tbbansi.nim | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/hwylterm/bbansi.nim b/src/hwylterm/bbansi.nim index a6441b2..55fc21a 100644 --- a/src/hwylterm/bbansi.nim +++ b/src/hwylterm/bbansi.nim @@ -11,6 +11,17 @@ import std/[os, sequtils, strformat, strutils] import ./bbansi/[styles, utils, colors] 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 BbSpan* = object styles*: seq[string] @@ -206,6 +217,7 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} = stdout.write('\n') stdout.flushFile +# NOTE: could move to standlone modules in the tools/ directory when isMainModule: import std/[parseopt, sugar] const version = staticExec "git describe --tags --always --dirty=-dev" diff --git a/tests/tbbansi.nim b/tests/tbbansi.nim index 5b40ad4..2f92aae 100644 --- a/tests/tbbansi.nim +++ b/tests/tbbansi.nim @@ -65,8 +65,12 @@ suite "basic": bbCheck "[#FF0000]red", "\e[38;2;255;0;0mred\e[0m" suite "strutils": + test "stripAnsi": + check stripAnsi("\e[1mBold String!") == "Bold String!" + test "&": 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": let tester = bb"[red]a red[/] [blue on red]blue on red part"