Improved list message.
h3rald h3rald@h3rald.com
Fri, 30 Mar 2018 18:43:04 +0200
2 files changed,
70 insertions(+),
13 deletions(-)
M
lib/messaging.nim
→
lib/messaging.nim
@@ -1,9 +1,16 @@
import terminal, - strutils + strutils, + sequtils, + unicode import minimline + +type + TreeNode* = object + label: string + nodes: seq[TreeNode] proc foreground(str: string, color: ForegroundColor) = stdout.setForegroundColor(color)@@ -49,3 +56,49 @@ proc printAdded*(label, value: string) =
printGreen("+++ ") echo label & ": " & value +when defined(windows): + proc ch(s): string = + case s: + of "└": + return $(192.chr) + of "├": + return $(195.chr) + of "─": + return $(196.chr) + of "┬": + return $(194.chr) + of "│": + return $(179.chr) +else: + proc ch(s: string): string = + return s + +proc newTreeNode*(label: string): TreeNode = + result.label = label + result.nodes = newSeq[TreeNode]() + +proc add*(x: var TreeNode, node: TreeNode) = + x.nodes.add(node) + +proc tree*(node: TreeNode, prefix = ""): string = + let splitterPart = if node.nodes.len > 0: ch("│") else: "" + let splitter = "\n" & prefix & splitterPart & "" + return prefix & [node.label].join(splitter) & "\n" & node.nodes.map(proc(x: TreeNode): string = + let ix = node.nodes.find(x) + let last = node.nodes.len-1 == ix + let more = x.nodes.len > 0 + let prefixPart = if last: " " else: ch("│") + let newPrefix = prefix & prefixPart & " " + let lastPart = if last: ch("└") else: ch("├") + let morePart = if more: ch("┬") else: ch("─") + let rec = tree(x, newPrefix) + var offset = 3 + var endSpace = "" + if lastPart == ch("└"): + offset = 2 + endSpace = " " + return prefix & lastPart & ch("─") & morePart & endSpace & rec[prefix.len+offset .. rec.len-1] + ).join("") + + +
M
nifty.nim
→
nifty.nim
@@ -24,7 +24,7 @@
Usage: nifty <command> [<package>] Executes <command> (on <package>). - For more information on available commands, run: nifty help + => For more information on available commands, run: nifty help Options: --log, -l Specifies the log level (debug|info|notice|warn|error|fatal).@@ -77,7 +77,7 @@ printAdded(newv.label, $newv.value)
return confirm("Confirm change?") proc confirmAndRemoveDir(dir: string) = - let answer = confirm "Delete directory '$1' and all its contents? [y/n]" % dir + let answer = confirm "Delete directory '$1' and all its contents?" % dir if answer: dir.removeDir()@@ -94,14 +94,17 @@ pkg.confirmAndRemoveDir()
else: warn "Package '$1' not found." % pkg -proc walkPkgs(prj: NiftyProject, dir: string, level = 1) = +proc listPackages(prj: NiftyProject, dir: string): TreeNode = + var node = newTreeNode(dir.extractFilename) for k, v in prj.packages.pairs: - echo " ".repeat(level*2) & "-" & " " & k var d = dir / prj.storage / k var p = newNiftyProject(d) if p.configured: p.load - walkPkgs(p, d, level+1) + node.add listPackages(p, d) + else: + node.add newTreeNode(k) + return node proc updateDefinitions(prj: var NiftyProject): bool = result = false@@ -200,6 +203,7 @@ if args.len < 2:
fatal "No package specified." quit(3) let alias = args[1] + prj.load if not prj.packages.hasKey(alias): fatal "Package '$1' not defined." % [alias] quit(4)@@ -219,9 +223,7 @@ confirmAndRemovePackage(prj.storage/args[1])
of "list": prj.load let pwd = getCurrentDir() - let parts = pwd.split(DirSep) - echo parts[parts.len-1] - walkPkgs(prj, pwd) + echo listPackages(prj, pwd).tree of "info": if args.len < 2: fatal "No package specified."@@ -235,17 +237,19 @@ let data = prj.packages[alias]
for k, v in data.pairs: echo "$1:\t$2" % [k, $v] of "help": + echo "" if args.len < 2: for k, v in prj.help.pairs: - printGreen "nifty $1" % v["_syntax"].getStr - echo "\n $1" % v["_description"].getStr + printGreen " nifty $1" % v["_syntax"].getStr + echo "\n $1\n" % v["_description"].getStr else: let cmd = args[1] + let help = prj.help[cmd] if not prj.help.hasKey(cmd): fatal "Command '$1' is not defined." % cmd quit(5) - printGreen "nifty " & prj.help[cmd]["_syntax"].getStr - echo "\n " & prj.help[cmd]["_description"].getStr + printGreen " nifty " & help["_syntax"].getStr + echo "\n $1\n" % help["_description"].getStr of "update-commands": prj.load if updateDefinitions(prj):