From 5564fdb18a818eac31013e5e41b8111b8798ce4c Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 14 Sep 2023 22:35:19 -0500 Subject: [PATCH] refactor: combine code more efficiently --- src/bbansi/styles.nim | 15 +++++++++------ tests/tbasic.nim | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bbansi/styles.nim b/src/bbansi/styles.nim index 273c262..f2f5f89 100644 --- a/src/bbansi/styles.nim +++ b/src/bbansi/styles.nim @@ -15,7 +15,6 @@ let "reverse": "7", "conceal": "8", "strike": "9", - }.newStringTable(modeCaseInsensitive) bbColors* = { @@ -31,6 +30,7 @@ let proc toAnsiCode*(s: string): string = var + codes: seq[string] styles: seq[string] bgStyle: string if " on " in s or s.startswith("on"): @@ -41,11 +41,14 @@ proc toAnsiCode*(s: string): string = styles = s.splitWhitespace() for style in styles: if style in bbStyles: - result.add "\e[" & bbStyles[style] & "m" + codes.add bbStyles[style] elif style in bbColors: - result.add "\e[3" & bbColors[style] & "m" + codes.add "3" & bbColors[style] + if bgStyle in bbColors: + codes.add "4" & bbColors[bgStyle] - let style = bgStyle - if style in bbColors: - result.add "\e[4" & bbColors[style] & "m" + if codes.len > 0: + result.add "\e[" + result.add codes.join ";" + result.add "m" diff --git a/tests/tbasic.nim b/tests/tbasic.nim index d2e48c8..9879dbd 100644 --- a/tests/tbasic.nim +++ b/tests/tbasic.nim @@ -17,7 +17,7 @@ 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 "[bold red]Bold Red Text", "\e[1m\e[31mBold Red 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", @@ -40,7 +40,7 @@ suite "basic": bbCheck "[red]Red Text[/]\nNext Line", "\e[31mRed Text\e[0m\nNext Line" test "on color": - bbCheck "[red on yellow]Red on Yellow", "\e[31m\e[43mRed on Yellow\e[0m" + bbCheck "[red on yellow]Red on Yellow", "\e[31;43mRed on Yellow\e[0m" test "concat-ops": check "[red]RED[/]".bb & " plain string" == "[red]RED[/] plain string".bb