Spice up the prompts.

This commit is contained in:
Dominik Picheta 2016-12-21 22:56:39 +01:00
commit 22929add07
2 changed files with 30 additions and 22 deletions

View file

@ -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

View file

@ -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"