refactor: enter the future with bbansi

This commit is contained in:
Daylin Morgan 2023-10-23 14:23:30 -05:00
parent f3ac3d7722
commit 212076b6d2
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
5 changed files with 45 additions and 34 deletions

View file

@ -8,7 +8,9 @@ binDir = "bin"
requires "nim >= 2.0.0", requires "nim >= 2.0.0",
"cligen" "cligen",
"https://github.com/daylinmorgan/bbansi#head"
import strformat import strformat

View file

@ -85,17 +85,16 @@ proc release(
checkTargets(cfg.targets.keys.toSeq()) checkTargets(cfg.targets.keys.toSeq())
if verbose: if verbose:
termEcho $cfg cfg.showConfig
if dryrun: if dryrun:
termEcho styleBright, fgBlue, "dry run...see below for commands" termEcho "[bold blue]dry run...see below for commands".bb
let let
baseCmd = if nimble or cfg.nimble: "nimble" else: "nim" baseCmd = if nimble or cfg.nimble: "nimble" else: "nim"
rest = parseArgs(args) rest = parseArgs(args)
termEcho styleBright, fgYellow, termEcho fmt"[bold yellow]compiling {cfg.bins.len} binaries for {cfg.targets.len} targets".bb
&"compiling {cfg.bins.len} binaries for {cfg.targets.len} targets"
for t, tArgs in cfg.targets: for t, tArgs in cfg.targets:
for b, bArgs in cfg.bins: for b, bArgs in cfg.bins:
@ -118,7 +117,7 @@ proc release(
stderr.writeLine cmd stderr.writeLine cmd
else: else:
if verbose: if verbose:
termEcho styleBright, "cmd: ", ansiResetCode, cmd termEcho fmt"[bold]cmd[/]: {cmd}".bb
let errCode = execCmd cmd let errCode = execCmd cmd
if errCode != 0: if errCode != 0:
termErr "cmd: ", cmd termErr "cmd: ", cmd

View file

@ -1,4 +1,5 @@
import std/[parsecfg, tables, os, strutils, strformat] import std/[parsecfg, tables, os, strutils, strformat]
import term
type type
ForgeConfig* = object ForgeConfig* = object
@ -10,21 +11,31 @@ type
version*: string version*: string
nimble*: bool nimble*: bool
proc `$`*(c: ForgeConfig): string = proc showConfig*(c: ForgeConfig) =
var lines: seq[string] = @[] var lines: string = ""
lines.add "config =" template addLine(l: string) = lines.add(l & "\n")
lines.add "| nimble " & $c.nimble proc addNameArgs(name, args: string): string =
lines.add "| outdir " & c.outdir result.add fmt"| {name}"
lines.add "| format " & c.format if args != "":
lines.add "| version " & c.version result.add fmt" | " & $args.bb("faint")
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: "")
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*( proc loadConfigFile*(
f: string, f: string,

View file

@ -1,17 +1,17 @@
import std/terminal import std/strutils
# TODO support NO_COLOR for these import bbansi
let prefix = ansiForegroundColorCode(fgMagenta, bright = true) & "forge" &
ansiResetCode & ansiForegroundColorCode(fgYellow) & " || " & ansiResetCode
template termEcho*(args: varargs[untyped]) = let prefix = "[bold magenta]forge[/] [yellow]||[/] ".bb
stderr.styledWriteLine(prefix, args)
template termErr*(args: varargs[untyped]) = template termEcho*(args: varargs[string | BbString, `$`]) =
stderr.styledWriteLine(prefix, fgRed, "error ", fgDefault, args) stderr.writeLine $prefix, args.join(" ")
template termErrQuit*(args: varargs[untyped]) = template termErr*(args: varargs[string | BbString, `$`]) =
termErr(args) stderr.writeLine $prefix, $"[red]error ||[/] ".bb, args.join(" ")
template termErrQuit*(args: varargs[string | BbString, `$`]) =
termErr args
quit 1 quit 1
export bbansi

View file

@ -1,5 +1,4 @@
import std/[json, os, osproc, terminal] import std/[json, os, osproc]
import term import term
proc zigTargets*(): seq[string] = proc zigTargets*(): seq[string] =
@ -26,7 +25,7 @@ template callZig*(zigCmd: string) =
proc zigExists*() = proc zigExists*() =
if (findExe "zig") == "": if (findExe "zig") == "":
termErr "zig not found" termErr "[red]zig not found".bb
termErr " forge requires a working installation of zig" termErr " forge requires a working installation of zig"
termErr " see: https://ziglang.org/download/" termErr " see: https://ziglang.org/download/"
quit 1 quit 1