Skip to content

Commit

Permalink
Fix alignment in help
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavlin committed Apr 5, 2023
1 parent c8063eb commit 14d382a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
57 changes: 42 additions & 15 deletions confutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const
useBufferedOutput = defined(nimscript)
noColors = useBufferedOutput or defined(confutils_no_colors)
hasCompletions = not defined(nimscript)
descPadding = 6
minNameWidth = 24 - descPadding
descPadding = 2
minNameWidth = 24 - descPadding

when not defined(nimscript):
import
Expand All @@ -28,8 +28,10 @@ type
copyrightBanner: string
hasAbbrs: bool
maxNameLen: int
maxAbbrLen: int
terminalWidth: int
namesWidth: int
abbrsWidth: int

CmdInfo = ref object
name: string
Expand Down Expand Up @@ -78,7 +80,8 @@ when defined(nimscript):

proc appInvocation: string =
let scriptNameIdx = scriptNameParamIdx()
"nim " & (if paramCount() > scriptNameIdx: paramStr(scriptNameIdx) else: "<nims-script>")
"nim " & (if paramCount() > scriptNameIdx: paramStr(
scriptNameIdx) else: "<nims-script>")

type stderr = object

Expand Down Expand Up @@ -194,6 +197,16 @@ iterator subCmds(cmd: CmdInfo): CmdInfo =
template isSubCommand(cmd: CmdInfo): bool =
cmd.name.len > 0

func maxAbbrLen(cmd: CmdInfo): int =
result = 0
for opt in cmd.opts:
if opt.kind == Arg or opt.kind == Discriminator and opt.isCommand:
continue
result = max(result, opt.abbr.len)
if opt.kind == Discriminator:
for subCmd in opt.subCmds:
result = max(result, subCmd.maxAbbrLen)

func maxNameLen(cmd: CmdInfo): int =
result = 0
for opt in cmd.opts:
Expand Down Expand Up @@ -318,10 +331,12 @@ proc describeOptions(help: var string,
helpOutput " "

if opt.abbr.len > 0:
helpOutput fgOption, styleBright, "-", opt.abbr, ", "
let switch = "-" & opt.abbr & ", "
helpOutput fgOption, styleBright, switch, padding(switch,
appInfo.abbrsWidth)
elif appInfo.hasAbbrs:
# Add additional indentatition, so all names are aligned
helpOutput " "
helpOutput spaces(appInfo.abbrsWidth)

if opt.name.len > 0:
let switch = "--" & opt.name
Expand All @@ -342,7 +357,8 @@ proc describeOptions(help: var string,
for i, subCmd in opt.subCmds:
if not subCmd.hasOpts: continue

helpOutput "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle, " = ", fgGreen, subCmd.name
helpOutput "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle,
" = ", fgGreen, subCmd.name

if i == opt.defaultSubCmd: helpOutput " (default)"
help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts
Expand Down Expand Up @@ -371,9 +387,14 @@ proc showHelp(help: var string,
let cmd = activeCmds[^1]

appInfo.maxNameLen = cmd.maxNameLen
if cmd.hasAbbrs:
appInfo.maxAbbrLen = cmd.maxAbbrLen
echo cmd.maxAbbrLen
appInfo.abbrsWidth = max(4, appInfo.maxAbbrLen) + 1

appInfo.hasAbbrs = cmd.hasAbbrs
appInfo.terminalWidth = terminalWidth()
appInfo.namesWidth = min(minNameWidth, appInfo.maxNameLen) + descPadding
appInfo.namesWidth = max(minNameWidth, appInfo.maxNameLen) + descPadding

var cmdInvocation = appInfo.appInvocation
for i in 1 ..< activeCmds.len:
Expand Down Expand Up @@ -563,7 +584,7 @@ proc completeCmdArg*(T: type[InputFile|TypedInputFile|InputDir|OutFile|OutDir|Ou
let show_dotfiles = len(name) > 0 and name[0] == '.'

try:
for kind, path in walkDir(dir_path, relative=true):
for kind, path in walkDir(dir_path, relative = true):
if not show_dotfiles and path[0] == '.':
continue

Expand Down Expand Up @@ -600,7 +621,8 @@ template setField[T](loc: var T, val: Option[string], defaultVal: untyped) =
loc = if isSome(val): parseCmdArgAux(FieldType, val.get)
else: FieldType(defaultVal)

template setField[T](loc: var seq[T], val: Option[string], defaultVal: untyped) =
template setField[T](loc: var seq[T], val: Option[string],
defaultVal: untyped) =
if val.isSome:
loc.add parseCmdArgAux(type(loc[0]), val.get)
else:
Expand Down Expand Up @@ -660,7 +682,8 @@ proc generateFieldSetters(RecordType: NimNode): NimNode =
newLit($paramName),
setterName, completerName,
newCall(bindSym"requiresInput", fixedFieldType),
newCall(bindSym"acceptsMultipleValues", fixedFieldType))
newCall(bindSym"acceptsMultipleValues",
fixedFieldType))

when (NimMajor, NimMinor) >= (1, 6):
result.add quote do:
Expand Down Expand Up @@ -809,7 +832,8 @@ proc cmdInfoFromType(T: NimNode): CmdInfo =

if field.caseBranch.kind == nnkElse:
error "Sub-command parameters cannot appear in an else branch. " &
"Please specify the sub-command branch precisely", field.caseBranch[0]
"Please specify the sub-command branch precisely",
field.caseBranch[0]

var branchEnumVal = field.caseBranch[0]
if branchEnumVal.kind == nnkDotExpr:
Expand Down Expand Up @@ -913,7 +937,7 @@ proc loadImpl[C, SecondarySources](

template applySetter(setterIdx: int, cmdLineVal: string) =
when defined(nimHasWarnBareExcept):
{.push warning[BareExcept]:off.}
{.push warning[BareExcept]: off.}

try:
fieldSetters[setterIdx][1](confAddr[], some(cmdLineVal))
Expand Down Expand Up @@ -947,7 +971,8 @@ proc loadImpl[C, SecondarySources](
argName
argAbbr

proc showMatchingOptions(cmd: CmdInfo, prefix: string, filterKind: set[ArgKindFilter]) =
proc showMatchingOptions(cmd: CmdInfo, prefix: string, filterKind: set[
ArgKindFilter]) =
var matchingOptions: seq[OptInfo]

if len(prefix) > 0:
Expand Down Expand Up @@ -1057,7 +1082,8 @@ proc loadImpl[C, SecondarySources](
let subCmdDiscriminator = lastCmd.getSubCmdDiscriminator
if subCmdDiscriminator != nil:
if subCmdDiscriminator.defaultSubCmd != -1:
let defaultCmd = subCmdDiscriminator.subCmds[subCmdDiscriminator.defaultSubCmd]
let defaultCmd = subCmdDiscriminator.subCmds[
subCmdDiscriminator.defaultSubCmd]
opt = findOpt(defaultCmd.opts, key)
if opt != nil:
activateCmd(subCmdDiscriminator, defaultCmd)
Expand Down Expand Up @@ -1096,7 +1122,8 @@ proc loadImpl[C, SecondarySources](
if subCmdDiscriminator != nil and
subCmdDiscriminator.defaultSubCmd != -1 and
fieldCounters[subCmdDiscriminator.idx] == 0:
let defaultCmd = subCmdDiscriminator.subCmds[subCmdDiscriminator.defaultSubCmd]
let defaultCmd = subCmdDiscriminator.subCmds[
subCmdDiscriminator.defaultSubCmd]
activateCmd(subCmdDiscriminator, defaultCmd)

if secondarySources != nil:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_envvar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ proc testUtils() =
readWrite("some number", 123'u32)
readWrite("some number 64", 123'u64)
readWrite("some bytes", @[1.byte, 2.byte])
readWrite("some int list", @[4,5,6])
readWrite("some int list", @[4, 5, 6])
readWrite("some array", [1.byte, 2.byte, 4.byte])
readWrite("some string", "hello world")
readWrite("some enum", Apple)
Expand Down Expand Up @@ -74,7 +74,7 @@ proc testEncoder() =
fuel: Diesel
),
wheel: 6,
door: [1,2,3,4],
door: [1, 2, 3, 4],
suspension: [
Suspension(dist: 1, length: 5),
Suspension(dist: 2, length: 6),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ignore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ type
dataDir* {.
ignore
defaultValue: "nimbus"
name: "data-dir"}: string
name: "data-dir".}: string

logLevel* {.
defaultValue: "DEBUG"
desc: "Sets the log level."
name: "log-level" }: string
name: "log-level".}: string

suite "test ignore option":
test "ignored option have no default value":
Expand Down

0 comments on commit 14d382a

Please sign in to comment.