From 0285a823b8734deb8ff8389481976f66e5f2187f Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 18 Sep 2023 12:58:40 -0500 Subject: [PATCH] refactor: rearrange imports --- src/bbansi.nim | 6 ++++-- src/bbansi/styles.nim | 32 ++++---------------------------- src/bbansi/utils.nim | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 src/bbansi/utils.nim diff --git a/src/bbansi.nim b/src/bbansi.nim index 2d17566..f155c76 100644 --- a/src/bbansi.nim +++ b/src/bbansi.nim @@ -1,5 +1,7 @@ -import std/[os, sequtils, strutils, sugar] +import std/[os, sequtils, strutils] + import bbansi/styles +import bbansi/utils # TODO: add support for some kind of FORCE_COLOR and detect terminals... let noColor = os.getEnv("NO_COLOR") != "" @@ -134,7 +136,7 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} = stdout.flushFile when isMainModule: - import std/[strformat, parseopt] + import std/[parseopt, strformat, sugar] const version = staticExec "git describe --tags --always --dirty=-dev" const longOptPad = 8 let help = fmt""" diff --git a/src/bbansi/styles.nim b/src/bbansi/styles.nim index 3561daf..9d693dd 100644 --- a/src/bbansi/styles.nim +++ b/src/bbansi/styles.nim @@ -1,9 +1,9 @@ -import std/[strutils, tables] - +import std/tables +export tables let bbReset* = "\e[0m" const - bbStyles = { + bbStyles* = { "bold": "1", "b": "1", "faint": "2", @@ -17,7 +17,7 @@ const "strike": "9", }.toTable - bbColors = { + bbColors* = { "black": "0", "red": "1", "green": "2", @@ -28,27 +28,3 @@ const "white": "7", }.toTable -proc toAnsiCode*(s: string): string = - var - codes: seq[string] - styles: seq[string] - bgStyle: string - if " on " in s or s.startswith("on"): - let fgBgSplit = s.rsplit("on", maxsplit = 1) - styles = fgBgSplit[0].toLowerAscii().splitWhitespace() - bgStyle = fgBgSplit[1].strip().toLowerAscii() - else: - styles = s.splitWhitespace() - for style in styles: - if style in bbStyles: - codes.add bbStyles[style] - elif style in bbColors: - codes.add "3" & bbColors[style] - if bgStyle in bbColors: - codes.add "4" & bbColors[bgStyle] - - if codes.len > 0: - result.add "\e[" - result.add codes.join ";" - result.add "m" - diff --git a/src/bbansi/utils.nim b/src/bbansi/utils.nim new file mode 100644 index 0000000..60497d6 --- /dev/null +++ b/src/bbansi/utils.nim @@ -0,0 +1,28 @@ +import std/[strutils] + +import styles + +proc toAnsiCode*(s: string): string = + var + codes: seq[string] + styles: seq[string] + bgStyle: string + if " on " in s or s.startswith("on"): + let fgBgSplit = s.rsplit("on", maxsplit = 1) + styles = fgBgSplit[0].toLowerAscii().splitWhitespace() + bgStyle = fgBgSplit[1].strip().toLowerAscii() + else: + styles = s.splitWhitespace() + for style in styles: + if style in bbStyles: + codes.add bbStyles[style] + elif style in bbColors: + codes.add "3" & bbColors[style] + if bgStyle in bbColors: + codes.add "4" & bbColors[bgStyle] + + if codes.len > 0: + result.add "\e[" + result.add codes.join ";" + result.add "m" +