diff options
author | kitsunyan | 2018-05-19 07:00:49 +0000 |
---|---|---|
committer | kitsunyan | 2018-05-19 07:00:49 +0000 |
commit | dff2014e9551a4ff7fffa24608ed12e089b0a21b (patch) | |
tree | 616054d50d1e8caaae7823dda3021a9a0ef2f508 | |
parent | f8107753a9a850bb86a2a8d07c71de082ea7a2c5 (diff) |
Simplify help output formatter for choices
-rw-r--r-- | src/feature/syncinstall.nim | 41 | ||||
-rw-r--r-- | src/format.nim | 32 |
2 files changed, 42 insertions, 31 deletions
diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index ecab3f5..65266d4 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -286,15 +286,13 @@ proc editLoop(config: Config, base: string, repoPath: string, gitSubdir: Option[ defaultYes: bool, noconfirm: bool): char = proc editFileLoop(file: string): char = let default = if defaultYes: 'y' else: 'n' - let res = printColonUserChoice(config.color, - tr"View and edit $#?" % [base & "/" & file], ['y', 'n', 's', 'a', '?'], - default, '?', noconfirm, 'n') - - if res == '?': - printUserInputHelp(('s', tr"skip all files"), - ('a', tr"abort operation")) - editFileLoop(file) - elif res == 'y': + + let res = printColonUserChoiceWithHelp(config.color, + tr"View and edit $#?" % [base & "/" & file], + choices('y', 'n', ('s', tr"skip all files"), ('a', tr"abort operation")), + default, noconfirm, 'n') + + if res == 'y': let visualEnv = getEnv("VISUAL") let editorEnv = getEnv("EDITOR") let editor = if visualEnv.len > 0: @@ -453,17 +451,9 @@ proc buildFromSources(config: Config, commonArgs: seq[Argument], if interrupted: (buildResult, 1) elif code != 0: - proc ask(): char = - let res = printColonUserChoice(config.color, - tr"Build failed, retry?", ['y', 'e', 'n', '?'], 'n', '?', - noconfirm, 'n') - if res == '?': - printUserInputHelp(('e', tr"retry with --noextract option")) - ask() - else: - res + let res = printColonUserChoiceWithHelp(config.color, tr"Build failed, retry?", + choices('y', ('e', tr"retry with --noextract option"), 'n'), 'n', noconfirm, 'n') - let res = ask() if res == 'e': loop(true, true) elif res == 'y': @@ -664,17 +654,14 @@ proc confirmViewAndImportKeys(config: Config, basePackages: seq[seq[seq[PackageI let res = if skipKeys: 'y' else: - printColonUserChoice(config.color, - tr"Import PGP key $#?" % [pgpKeys[index]], ['y', 'n', 'c', 'a', '?'], - 'y', '?', noconfirm, 'y') + printColonUserChoiceWithHelp(config.color, + tr"Import PGP key $#?" % [pgpKeys[index]], + choices('y', 'n', ('c', tr"import all keys"), ('a', tr"abort operation")), + 'y', noconfirm, 'y') let newSkipKeys = skipKeys or res == 'c' - if res == '?': - printUserInputHelp(('c', tr"import all keys"), - ('a', tr"abort operation")) - keysLoop(index, newSkipKeys) - elif res == 'y' or newSkipKeys: + if res == 'y' or newSkipKeys: let importCode = forkWait(() => (block: dropPrivilegesAndChdir(none(string)): if config.pgpKeyserver.isSome: 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 |