refactor: rearrange imports

This commit is contained in:
Daylin Morgan 2023-09-18 12:58:40 -05:00
parent e77ad85cd3
commit 0285a823b8
Signed by: daylin
GPG Key ID: C1E52E7DD81DF79F
3 changed files with 36 additions and 30 deletions

View File

@ -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"""

View File

@ -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"

28
src/bbansi/utils.nim Normal file
View File

@ -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"