all repos — nifty @ 2a8262c36baf39a880f4fbbcdba53cff7ac56a60

A tiny (pseudo) package manager and script runner.

Refactoring: messaging procs.
h3rald h3rald@h3rald.com
Sat, 24 Mar 2018 14:54:15 +0100
commit

2a8262c36baf39a880f4fbbcdba53cff7ac56a60

parent

7b3af68439dae5516777f1dce5648da6fc876208

2 files changed, 104 insertions(+), 81 deletions(-)

jump to
A lib/messaging.nim

@@ -0,0 +1,51 @@

+import + terminal, + strutils + +import + minimline + +proc foreground(str: string, color: ForegroundColor) = + stdout.setForegroundColor(color) + stdout.write(str) + resetAttributes() + +proc printGreen*(str: string) = + foreground(str, fgGreen) + +proc printRed*(str: string) = + foreground(str, fgRed) + +proc printYellow*(str: string) = + foreground(str, fgYellow) + +proc printBlue*(str: string) = + foreground(str, fgBlue) + +proc confirm*(q: string): bool = + printYellow("(!) " & q & " [y/n]: ") + var ed = initEditor() + let answer = ed.readLine().toLowerAscii[0] + if answer == 'y': + return true + return false + +proc printValue*(key, value: string) = + printBlue(" -> $1: " % key) + printGreen(value) + resetAttributes() + stdout.write("\n") + +proc editValue*(key: string, value = ""): string = + printBlue(" -> $1: " % key) + var ed = initEditor() + result = ed.edit(value) + +proc printDeleted*(label, value: string) = + printRed("--- ") + echo label & ": " & value + +proc printAdded*(label, value: string) = + printGreen("+++ ") + echo label & ": " & value +
M nifty.nimnifty.nim

@@ -5,7 +5,6 @@ ospaths,

parseopt, logging, strutils, - terminal, sequtils import

@@ -17,44 +16,43 @@

import lib/config, lib/project, - lib/minimline + lib/messaging -proc confirm(q: string): bool = - stdout.setForegroundColor(fgYellow) - stdout.write("(!) " & q & " [y/n]: ") - resetAttributes() - var ed = initEditor() - let answer = ed.readLine().toLowerAscii[0] - if answer == 'y': - return true - return false +let usage* = """ $1 v$2 - $3 + (c) 2017-2018 Fabio Cevasco + + Usage: + nifty <command> [<package>] Executes <command> (on <package>). + + For more information on available commands, run: nifty help + + Options: + --log, -l Specifies the log level (debug|info|notice|warn|error|fatal). + Default: info + --help, -h Displays this message. + --version, -h Displays the version of the application. +""" % [appname, version, appdesc] + + +# Helper Methods proc addProperty(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] = var done = false while (not done): if name == "": - stdout.setForegroundColor(fgBlue) - stdout.write(" -> Name: ") - resetAttributes() - result.key = stdin.readLine + result.key = editValue("Name") elif name == "name": warn "Property identifier 'name' cannot be modified." else: - stdout.setForegroundColor(fgBlue) - echo " -> Name: " & name - resetAttributes() + printValue(" Name", name) result.key = name var ok = false while (not ok): - stdout.setForegroundColor(fgBlue) - stdout.write(" -> Value: ") - resetAttributes() - var ed = initEditor() var value = "" if parentObj.hasKey(result.key): value = $parentObj[result.key] try: - result.value = ed.edit(value).parseJson + result.value = editValue("Value", value).parseJson if (result.value == newJNull()): ok = confirm("Remove property '$1'?" % result.key) done = true

@@ -73,34 +71,11 @@ done = not confirm("Do you want to add/remove more properties?")

proc changeValue(oldv: tuple[label: string, value: JsonNode], newv: tuple[label: string, value: JsonNode]): bool = if oldv.value != newJNull(): - stdout.setForegroundColor(fgRed) - stdout.write("--- ") - resetAttributes() - echo oldv.label & ": " & $oldv.value + printDeleted(oldv.label, $oldv.value) if newv.value != newJNull(): - stdout.setForegroundColor(fgGreen) - stdout.write("+++ ") - resetAttributes() - echo newv.label & ": " & $newv.value + printAdded(newv.label, $newv.value) return confirm("Confirm change?") -let usage* = """ $1 v$2 - $3 - (c) 2017-2018 Fabio Cevasco - - Usage: - nifty <command> [<package>] Executes <command> (on <package>). - - For more information on available commands, run: nifty help - - Options: - --log, -l Specifies the log level (debug|info|notice|warn|error|fatal). - Default: info - --help, -h Displays this message. - --version, -h Displays the version of the application. -""" % [appname, version, appdesc] - -var args = newSeq[string](0) - proc confirmAndRemoveDir(dir: string) = let answer = confirm "Delete directory '$1' and all its contents? [y/n]" % dir if answer:

@@ -119,26 +94,6 @@ pkg.confirmAndRemoveDir()

else: warn "Package '$1' not found." % pkg -for kind, key, val in getopt(): - case kind: - of cmdArgument: - args.add key - of cmdLongOption, cmdShortOption: - case key: - of "log", "l": - var val = val - setLogLevel(val) - of "help", "h": - echo usage - quit(0) - of "version", "v": - echo version - quit(0) - else: - discard - else: - discard - proc walkPkgs(prj: NiftyProject, dir: string, level = 1) = for k, v in prj.packages.pairs: echo " ".repeat(level*2) & "-" & " " & k

@@ -170,20 +125,38 @@ result = true

else: result = true # Adding new property - stdout.setForegroundColor(fgGreen) - stdout.write("+++ ") - resetAttributes() - echo "$1.$2: $3" % [k, prop, $sysProp] + printAdded("$1.$2" % [k, prop], $sysProp) prjCommand[prop] = sysProp else: result = true # Adding new command - stdout.setForegroundColor(fgGreen) - stdout.write("+++ ") - resetAttributes() - echo "$1: $2" % [k, $sysCommands[k]] + printAdded(k, $sysCommands[k]) prj.commands[k] = sysCommands[k] +### MAIN ### + +var args = newSeq[string](0) + +for kind, key, val in getopt(): + case kind: + of cmdArgument: + args.add key + of cmdLongOption, cmdShortOption: + case key: + of "log", "l": + var val = val + setLogLevel(val) + of "help", "h": + echo usage + quit(0) + of "version", "v": + echo version + quit(0) + else: + discard + else: + discard + var prj = newNiftyProject(getCurrentDir()) if args.len == 0:

@@ -264,16 +237,15 @@ echo "$1:\t$2" % [k, $v]

of "help": if args.len < 2: for k, v in prj.help.pairs: - stdout.setForegroundColor(fgGreen) - echo "nifty $1" % v["_syntax"].getStr - resetAttributes() - echo " $1" % v["_description"].getStr + printGreen "nifty $1" % v["_syntax"].getStr + echo "\n $1" % v["_description"].getStr else: let cmd = args[1] if not prj.help.hasKey(cmd): fatal "Command '$1' is not defined." % cmd quit(5) - echo "nifty $1\n $2" % [prj.help[cmd]["_syntax"].getStr, prj.help[cmd]["_description"].getStr] + printGreen "nifty " & prj.help[cmd]["_syntax"].getStr + echo "\n " & prj.help[cmd]["_description"].getStr of "update-commands": prj.load if updateDefinitions(prj):