Implement --noColor option.

This commit is contained in:
Dominik Picheta 2017-04-30 15:40:46 +01:00
commit d9f2e6c49e
2 changed files with 21 additions and 5 deletions

View file

@ -19,6 +19,7 @@ type
level: Priority
warnings: HashSet[(string, string)]
suppressionCount: int ## Amount of messages which were not shown.
showColor: bool ## Whether messages should be colored.
Priority* = enum
DebugPriority, LowPriority, MediumPriority, HighPriority
@ -40,7 +41,8 @@ proc newCLI(): CLI =
result = CLI(
level: HighPriority,
warnings: initSet[(string, string)](),
suppressionCount: 0
suppressionCount: 0,
showColor: true
)
var globalCLI = newCLI()
@ -55,11 +57,16 @@ proc displayCategory(category: string, displayType: DisplayType,
# Calculate how much the `category` must be offset to align along a center
# line.
let offset = calculateCategoryOffset(category)
# Display the category.
if priority != DebugPriority:
setForegroundColor(stdout, foregrounds[displayType])
writeStyled("$1$2 " % [repeatChar(offset), category], styles[priority])
resetAttributes()
let text = "$1$2 " % [spaces(offset), category]
if globalCLI.showColor:
if priority != DebugPriority:
setForegroundColor(stdout, foregrounds[displayType])
writeStyled(text, styles[priority])
resetAttributes()
else:
stdout.write(text)
proc displayLine(category, line: string, displayType: DisplayType,
priority: Priority) =
@ -145,6 +152,9 @@ proc promptCustom*(question, default: string): string =
proc setVerbosity*(level: Priority) =
globalCLI.level = level
proc setShowColor*(val: bool) =
globalCLI.showColor = val
when isMainModule:
display("Reading", "config file at /Users/dom/.config/nimble/nimble.ini",
priority = LowPriority)

View file

@ -20,6 +20,7 @@ type
pkgInfoCache*: TableRef[string, PackageInfo]
showHelp*: bool
showVersion*: bool
noColor*: bool
ActionType* = enum
actionNil, actionRefresh, actionInit, actionDump, actionPublish,
@ -92,6 +93,7 @@ Options:
--nimbleDir:dirname Set the Nimble directory.
--verbose Show all non-debug output.
--debug Show all output including debug messages.
--noColor Don't colorise output.
For more information read the Github readme:
https://github.com/nim-lang/nimble#readme
@ -249,6 +251,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
of "nimbledir": result.nimbleDir = val
of "verbose": result.verbosity = LowPriority
of "debug": result.verbosity = DebugPriority
of "nocolor": result.noColor = true
# Action-specific flags.
else:
case result.action.typ
@ -318,6 +321,9 @@ proc parseCmdLine*(): Options =
# Set verbosity level.
setVerbosity(result.verbosity)
# Set whether color should be shown.
setShowColor(not result.noColor)
# Parse config.
result.config = parseConfig()