aboutsummaryrefslogtreecommitdiff
path: root/src/format.nim
diff options
context:
space:
mode:
authorkitsunyan2018-05-19 07:00:49 +0000
committerkitsunyan2018-05-19 07:00:49 +0000
commitdff2014e9551a4ff7fffa24608ed12e089b0a21b (patch)
tree616054d50d1e8caaae7823dda3021a9a0ef2f508 /src/format.nim
parentf8107753a9a850bb86a2a8d07c71de082ea7a2c5 (diff)
Simplify help output formatter for choices
Diffstat (limited to 'src/format.nim')
-rw-r--r--src/format.nim32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/format.nim b/src/format.nim
index d092153..f20eaf7 100644
--- a/src/format.nim
+++ b/src/format.nim
@@ -1,5 +1,5 @@
import
- future, options, posix, sequtils, strutils, times, unicode,
+ future, macros, options, posix, sequtils, strutils, times, unicode,
utils
type
@@ -266,9 +266,33 @@ proc printColonUserChoice*(color: bool, s: string, answers: openArray[char],
else:
negative
-proc printUserInputHelp*(operations: varargs[tuple[answer: char, description: string]]) =
- for operation in (@operations & ('?', tr"view this help")):
- echo(" ", operation.answer, " - ", operation.description)
+proc printColonUserChoiceWithHelp*(color: bool, s: string,
+ answers: openArray[tuple[c: char, help: Option[string]]], positive: char,
+ noconfirm: bool, default: char): char =
+ let c = printColonUserChoice(color, s, answers.map(a => a.c) & '?',
+ positive, '?', noconfirm, default)
+
+ if c == '?':
+ for answer in @answers & ('?', some(tr"view this help")):
+ if answer.help.isSome:
+ echo(" ", answer.c, " - ", answer.help.unsafeGet)
+ printColonUserChoiceWithHelp(color, s, answers, positive, noconfirm, default)
+ else:
+ c
+
+macro choices*(choices: varargs[untyped]): untyped =
+ result = newNimNode(nnkBracket)
+ for choice in choices:
+ case choice.kind:
+ of nnkCharLit:
+ result.add(newPar(choice, newCall(ident("none"), ident("string"))))
+ of nnkPar:
+ if choice.len == 2:
+ result.add(newPar(choice[0], newCall(ident("some"), choice[1])))
+ else:
+ error("error")
+ else:
+ error("error")
proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () -> void) =
let width = getWindowSize().width