From 212076b6d2ac494967e174d5d9569596b5c5d48e Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 23 Oct 2023 14:23:30 -0500 Subject: [PATCH] refactor: enter the future with bbansi --- forge.nimble | 4 +++- src/forge.nim | 9 ++++----- src/forge/config.nim | 39 +++++++++++++++++++++++++-------------- src/forge/term.nim | 22 +++++++++++----------- src/forge/zig.nim | 5 ++--- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/forge.nimble b/forge.nimble index 59cce2e..34f545c 100644 --- a/forge.nimble +++ b/forge.nimble @@ -8,7 +8,9 @@ binDir = "bin" requires "nim >= 2.0.0", - "cligen" + "cligen", + "https://github.com/daylinmorgan/bbansi#head" + import strformat diff --git a/src/forge.nim b/src/forge.nim index c75e040..3bfdb0e 100644 --- a/src/forge.nim +++ b/src/forge.nim @@ -85,17 +85,16 @@ proc release( checkTargets(cfg.targets.keys.toSeq()) if verbose: - termEcho $cfg + cfg.showConfig if dryrun: - termEcho styleBright, fgBlue, "dry run...see below for commands" + termEcho "[bold blue]dry run...see below for commands".bb let baseCmd = if nimble or cfg.nimble: "nimble" else: "nim" rest = parseArgs(args) - termEcho styleBright, fgYellow, - &"compiling {cfg.bins.len} binaries for {cfg.targets.len} targets" + termEcho fmt"[bold yellow]compiling {cfg.bins.len} binaries for {cfg.targets.len} targets".bb for t, tArgs in cfg.targets: for b, bArgs in cfg.bins: @@ -118,7 +117,7 @@ proc release( stderr.writeLine cmd else: if verbose: - termEcho styleBright, "cmd: ", ansiResetCode, cmd + termEcho fmt"[bold]cmd[/]: {cmd}".bb let errCode = execCmd cmd if errCode != 0: termErr "cmd: ", cmd diff --git a/src/forge/config.nim b/src/forge/config.nim index 72ea83a..c77cd82 100644 --- a/src/forge/config.nim +++ b/src/forge/config.nim @@ -1,4 +1,5 @@ import std/[parsecfg, tables, os, strutils, strformat] +import term type ForgeConfig* = object @@ -10,21 +11,31 @@ type version*: string nimble*: bool -proc `$`*(c: ForgeConfig): string = - var lines: seq[string] = @[] - lines.add "config =" - lines.add "| nimble " & $c.nimble - lines.add "| outdir " & c.outdir - lines.add "| format " & c.format - lines.add "| version " & c.version - lines.add "| targets:" - for target, args in c.targets: - lines.add "| " & target & (if args != "": "|" & args else: "") - lines.add "| bins:" - for bin, args in c.bins: - lines.add "| " & bin & (if args != "": "|" & args else: "") +proc showConfig*(c: ForgeConfig) = + var lines: string = "" + template addLine(l: string) = lines.add(l & "\n") + proc addNameArgs(name, args: string): string = + result.add fmt"| {name}" + if args != "": + result.add fmt" | " & $args.bb("faint") - lines.join("\n") + addLine $fmt""" +config = +| [blue]nimble[/] {c.nimble} +| [blue]outdir[/] {c.outdir} +| [blue]format[/] {c.format} +| [blue]version[/] {c.version}""".bb + + + addLine $"| [green]targets[/]:".bb + for target, args in c.targets: + addLine addNameArgs(target, args) + + addLine $"| [green]bins[/]:".bb + for bin, args in c.bins: + addline addNameArgs(bin, args) + + termEcho lines proc loadConfigFile*( f: string, diff --git a/src/forge/term.nim b/src/forge/term.nim index 27e7285..5512396 100644 --- a/src/forge/term.nim +++ b/src/forge/term.nim @@ -1,17 +1,17 @@ -import std/terminal +import std/strutils -# TODO support NO_COLOR for these -let prefix = ansiForegroundColorCode(fgMagenta, bright = true) & "forge" & - ansiResetCode & ansiForegroundColorCode(fgYellow) & " || " & ansiResetCode +import bbansi -template termEcho*(args: varargs[untyped]) = - stderr.styledWriteLine(prefix, args) +let prefix = "[bold magenta]forge[/] [yellow]||[/] ".bb -template termErr*(args: varargs[untyped]) = - stderr.styledWriteLine(prefix, fgRed, "error ", fgDefault, args) +template termEcho*(args: varargs[string | BbString, `$`]) = + stderr.writeLine $prefix, args.join(" ") -template termErrQuit*(args: varargs[untyped]) = - termErr(args) +template termErr*(args: varargs[string | BbString, `$`]) = + stderr.writeLine $prefix, $"[red]error ||[/] ".bb, args.join(" ") + +template termErrQuit*(args: varargs[string | BbString, `$`]) = + termErr args quit 1 - +export bbansi diff --git a/src/forge/zig.nim b/src/forge/zig.nim index 6eed198..336aec5 100644 --- a/src/forge/zig.nim +++ b/src/forge/zig.nim @@ -1,5 +1,4 @@ -import std/[json, os, osproc, terminal] - +import std/[json, os, osproc] import term proc zigTargets*(): seq[string] = @@ -26,7 +25,7 @@ template callZig*(zigCmd: string) = proc zigExists*() = if (findExe "zig") == "": - termErr "zig not found" + termErr "[red]zig not found".bb termErr " forge requires a working installation of zig" termErr " see: https://ziglang.org/download/" quit 1