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/styles
import bbansi/utils
# TODO: add support for some kind of FORCE_COLOR and detect terminals... # TODO: add support for some kind of FORCE_COLOR and detect terminals...
let noColor = os.getEnv("NO_COLOR") != "" let noColor = os.getEnv("NO_COLOR") != ""
@ -134,7 +136,7 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} =
stdout.flushFile stdout.flushFile
when isMainModule: when isMainModule:
import std/[strformat, parseopt] import std/[parseopt, strformat, sugar]
const version = staticExec "git describe --tags --always --dirty=-dev" const version = staticExec "git describe --tags --always --dirty=-dev"
const longOptPad = 8 const longOptPad = 8
let help = fmt""" let help = fmt"""

View file

@ -1,9 +1,9 @@
import std/[strutils, tables] import std/tables
export tables
let bbReset* = "\e[0m" let bbReset* = "\e[0m"
const const
bbStyles = { bbStyles* = {
"bold": "1", "bold": "1",
"b": "1", "b": "1",
"faint": "2", "faint": "2",
@ -17,7 +17,7 @@ const
"strike": "9", "strike": "9",
}.toTable }.toTable
bbColors = { bbColors* = {
"black": "0", "black": "0",
"red": "1", "red": "1",
"green": "2", "green": "2",
@ -28,27 +28,3 @@ const
"white": "7", "white": "7",
}.toTable }.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"