diff --git a/src/nimblepkg/cli.nim b/src/nimblepkg/cli.nim index 5206411..3ac4b45 100644 --- a/src/nimblepkg/cli.nim +++ b/src/nimblepkg/cli.nim @@ -26,6 +26,9 @@ type DisplayType* = enum Error, Warning, Message, Success + ForcePrompt* = enum + dontForcePrompt, forcePromptYes, forcePromptNo + const longestCategory = len("Downloading") foregrounds: array[Error .. Success, ForegroundColor] = @@ -47,8 +50,8 @@ proc calculateCategoryOffset(category: string): int = assert category.len <= longestCategory return longestCategory - category.len -proc displayLine(category, line: string, displayType: DisplayType, - priority: Priority) = +proc displayCategory(category: string, displayType: DisplayType, + priority: Priority) = # Calculate how much the `category` must be offset to align along a center # line. let offset = calculateCategoryOffset(category) @@ -58,6 +61,10 @@ proc displayLine(category, line: string, displayType: DisplayType, writeStyled("$1$2 " % [repeatChar(offset), category], styles[priority]) resetAttributes() +proc displayLine(category, line: string, displayType: DisplayType, + priority: Priority) = + displayCategory(category, displayType, priority) + # Display the message. echo(line) @@ -97,6 +104,26 @@ proc displayTip*() = $globalCLI.suppressionCount display("Tip:", msg, Warning, HighPriority) +proc prompt*(forcePrompts: ForcePrompt, question: string): bool = + case forcePrompts + of forcePromptYes: + display("Prompt:", question & " -> [forced yes]", Warning, HighPriority) + return true + of forcePromptNo: + display("Prompt:", question & " -> [forced no]", Warning, HighPriority) + return false + of dontForcePrompt: + display("Prompt:", question & " [y/N]", Warning, HighPriority) + displayCategory("...", Warning, HighPriority) + let yn = stdin.readLine() + case yn.normalize + of "y", "yes": + return true + of "n", "no": + return false + else: + return false + proc setVerbosity*(level: Priority) = globalCLI.level = level diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index fe0b5fe..724857c 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -45,9 +45,6 @@ type arguments*: seq[string] flags*: StringTableRef - ForcePrompt* = enum - dontForcePrompt, forcePromptYes, forcePromptNo - const help* = """ @@ -168,23 +165,7 @@ proc prompt*(options: Options, question: string): bool = ## ## The proc will return immediately without asking the user if the global ## forcePrompts has a value different than dontForcePrompt. - case options.forcePrompts - of forcePromptYes: - echo(question & " -> [forced yes]") - return true - of forcePromptNo: - echo(question & " -> [forced no]") - return false - of dontForcePrompt: - echo(question & " [y/N]") - let yn = stdin.readLine() - case yn.normalize - of "y", "yes": - return true - of "n", "no": - return false - else: - return false + return prompt(options.forcePrompts, question) proc renameBabelToNimble(options: Options) {.deprecated.} = let babelDir = getHomeDir() / ".babel"