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 DisplayType* = enum
Error, Warning, Message, Success Error, Warning, Message, Success
ForcePrompt* = enum
dontForcePrompt, forcePromptYes, forcePromptNo
const const
longestCategory = len("Downloading") longestCategory = len("Downloading")
foregrounds: array[Error .. Success, ForegroundColor] = foregrounds: array[Error .. Success, ForegroundColor] =
@ -47,8 +50,8 @@ proc calculateCategoryOffset(category: string): int =
assert category.len <= longestCategory assert category.len <= longestCategory
return longestCategory - category.len return longestCategory - category.len
proc displayLine(category, line: string, displayType: DisplayType, proc displayCategory(category: string, displayType: DisplayType,
priority: Priority) = priority: Priority) =
# Calculate how much the `category` must be offset to align along a center # Calculate how much the `category` must be offset to align along a center
# line. # line.
let offset = calculateCategoryOffset(category) let offset = calculateCategoryOffset(category)
@ -58,6 +61,10 @@ proc displayLine(category, line: string, displayType: DisplayType,
writeStyled("$1$2 " % [repeatChar(offset), category], styles[priority]) writeStyled("$1$2 " % [repeatChar(offset), category], styles[priority])
resetAttributes() resetAttributes()
proc displayLine(category, line: string, displayType: DisplayType,
priority: Priority) =
displayCategory(category, displayType, priority)
# Display the message. # Display the message.
echo(line) echo(line)
@ -97,6 +104,26 @@ proc displayTip*() =
$globalCLI.suppressionCount $globalCLI.suppressionCount
display("Tip:", msg, Warning, HighPriority) 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) = proc setVerbosity*(level: Priority) =
globalCLI.level = level globalCLI.level = level

View file

@ -45,9 +45,6 @@ type
arguments*: seq[string] arguments*: seq[string]
flags*: StringTableRef flags*: StringTableRef
ForcePrompt* = enum
dontForcePrompt, forcePromptYes, forcePromptNo
const const
help* = """ help* = """
@ -168,23 +165,7 @@ proc prompt*(options: Options, question: string): bool =
## ##
## The proc will return immediately without asking the user if the global ## The proc will return immediately without asking the user if the global
## forcePrompts has a value different than dontForcePrompt. ## forcePrompts has a value different than dontForcePrompt.
case options.forcePrompts return prompt(options.forcePrompts, question)
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
proc renameBabelToNimble(options: Options) {.deprecated.} = proc renameBabelToNimble(options: Options) {.deprecated.} =
let babelDir = getHomeDir() / ".babel" let babelDir = getHomeDir() / ".babel"