From 14d382a3a81b28fa7dd62b7c7ecdacc3bd20271b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pavl=C3=ADn?= Date: Wed, 5 Apr 2023 17:11:11 +0200 Subject: [PATCH 1/4] Fix alignment in help --- confutils.nim | 57 +++++++++++++++++++++++++++++++------------ tests/test_envvar.nim | 4 +-- tests/test_ignore.nim | 4 +-- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/confutils.nim b/confutils.nim index 083d079..436a1a5 100644 --- a/confutils.nim +++ b/confutils.nim @@ -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 @@ -28,8 +28,10 @@ type copyrightBanner: string hasAbbrs: bool maxNameLen: int + maxAbbrLen: int terminalWidth: int namesWidth: int + abbrsWidth: int CmdInfo = ref object name: string @@ -78,7 +80,8 @@ when defined(nimscript): proc appInvocation: string = let scriptNameIdx = scriptNameParamIdx() - "nim " & (if paramCount() > scriptNameIdx: paramStr(scriptNameIdx) else: "") + "nim " & (if paramCount() > scriptNameIdx: paramStr( + scriptNameIdx) else: "") type stderr = object @@ -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: @@ -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 @@ -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 @@ -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: @@ -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 @@ -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: @@ -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: @@ -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: @@ -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)) @@ -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: @@ -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) @@ -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: diff --git a/tests/test_envvar.nim b/tests/test_envvar.nim index 472811e..44f8c7a 100644 --- a/tests/test_envvar.nim +++ b/tests/test_envvar.nim @@ -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) @@ -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), diff --git a/tests/test_ignore.nim b/tests/test_ignore.nim index de49218..5bacd41 100644 --- a/tests/test_ignore.nim +++ b/tests/test_ignore.nim @@ -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": From a0e80198b506634f6a18a4d37eb63495f57c99fa Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 20 Feb 2024 22:03:59 +0100 Subject: [PATCH 2/4] formatting --- confutils.nim | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/confutils.nim b/confutils.nim index 0042c3c..4665eff 100644 --- a/confutils.nim +++ b/confutils.nim @@ -89,8 +89,11 @@ when defined(nimscript): proc appInvocation: string = let scriptNameIdx = scriptNameParamIdx() - "nim " & (if paramCount() > scriptNameIdx: paramStr( - scriptNameIdx) else: "") + "nim " & ( + if paramCount() > scriptNameIdx: + paramStr(scriptNameIdx) + else: + "") type stderr = object @@ -350,8 +353,8 @@ proc describeOptions(help: var string, if opt.abbr.len > 0: let switch = "-" & opt.abbr & ", " - helpOutput fgOption, styleBright, switch, padding(switch, - appInfo.abbrsWidth) + helpOutput fgOption, styleBright, switch, + padding(switch, appInfo.abbrsWidth) elif appInfo.hasAbbrs: # Add additional indentatition, so all names are aligned helpOutput spaces(appInfo.abbrsWidth) @@ -376,7 +379,7 @@ proc describeOptions(help: var string, if not subCmd.hasOpts: continue helpOutput "\pWhen ", styleBright, fgBlue, opt.humaneName, resetStyle, - " = ", fgGreen, subCmd.name + " = ", fgGreen, subCmd.name if i == opt.defaultSubCmd: helpOutput " (default)" help.describeOptions subCmd, cmdInvocation, appInfo, conditionalOpts @@ -710,12 +713,12 @@ proc generateFieldSetters(RecordType: NimNode): NimNode = # be typed as a tyTypeDesc[tyString] instead of just `tyString`. To be filed. var fixedFieldType = newTree(nnkTypeOfExpr, field.typ) - settersArray.add newTree(nnkTupleConstr, - newLit($paramName), - setterName, completerName, - newCall(bindSym"requiresInput", fixedFieldType), - newCall(bindSym"acceptsMultipleValues", - fixedFieldType)) + settersArray.add newTree( + nnkTupleConstr, + newLit($paramName), + setterName, completerName, + newCall(bindSym"requiresInput", fixedFieldType), + newCall(bindSym"acceptsMultipleValues", fixedFieldType)) result.add quote do: {.push hint[XCannotRaiseY]: off.} @@ -861,7 +864,7 @@ 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] + field.caseBranch[0] var branchEnumVal = field.caseBranch[0] if branchEnumVal.kind == nnkDotExpr: @@ -982,7 +985,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)) @@ -1016,8 +1019,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: @@ -1139,8 +1142,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) @@ -1179,8 +1182,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: From 34695736f85cec18e68f1c4a71eb772ebdf68cb5 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 20 Feb 2024 22:06:10 +0100 Subject: [PATCH 3/4] formatting --- confutils.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/confutils.nim b/confutils.nim index 4665eff..653a931 100644 --- a/confutils.nim +++ b/confutils.nim @@ -659,8 +659,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: From 327fc37f33db0cb5f34d234b0c53076e816ed836 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 20 Feb 2024 22:06:37 +0100 Subject: [PATCH 4/4] remove debug echo --- confutils.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/confutils.nim b/confutils.nim index 653a931..e41fd30 100644 --- a/confutils.nim +++ b/confutils.nim @@ -410,7 +410,6 @@ proc showHelp(help: var string, 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