Puedes mejorar el siguiente codigo para que sea mas eficiente: [code]
- User
Puedes mejorar el siguiente codigo para que sea mas eficiente:
import std/[strutils, sequtils, algorithm] import types, utils const firstMargin = blank(2) const leftMargin = blank(4) #-- Wrap message proc wrapMessage(msg: string, maxLen: int): seq[string] = let leftMargin = maxLen + firstMargin.len if msg.len > 99: result.add(wrapText(msg, 99, 2, leftMargin)) else: result.add(firstMargin & msg) #-- Create the Usage section proc makeMsgUsage(data: static Cmdos): seq[string] = # Adds the header for the usage message result.add("$#Usage:$#" % [emphasis, unstyle]) # Constructs the usage message var cmdNames: seq[string] for cmd in data.cmds: if "" in cmd.alias: cmdNames.add("<options>") else: cmdNames.add("[$#]" % [cmd.alias.join("/")]) cmdNames.sort() let msg = data.app & " " & cmdNames.join(" ") result.add(wrapMessage(msg, data.app.len)) #-- Create the Commands section proc makeMsgCommands(data: static Cmdos): seq[string] = let cmds = data.cmds let maxLen = cmds.mapIt(it.alias.join(", ").len + (if it.args.len > 0: suffix.len else: 0)).max # Adds the header for the list of commands result.add("$#Commands:$#" % [emphasis, unstyle]) # Iterates over each command and generates its message var cmdMsgs: seq[string] for cmd in cmds: let name = cmd.alias.join(", ") & (if cmd.args.len > 0: " <options>" else: "") let space = blank(maxLen + leftMargin.len - name.len) let msg = name & space & cmd.info if "" in cmd.alias: continue cmdMsgs.add(wrapMessage(msg, maxLen + 4)) result.add(cmdMsgs) #-- Create the Options section proc makeMsgOptions(data: static Cmdos): seq[string] = for cmd in data.cmds: if cmd.args.len > 0: let longName = utils.getLongestWord(cmd.alias) let maxLen = cmd.args.mapIt(it.alias.join(", ").len + it.label.len).max # Adds the header for the options of the current command result.add("\n$#Options for: '$#'$#" % [emphasis, longName, unstyle]) # Constructs the option message for opt in cmd.args: let name = ( if areSeqEqual(opt.alias, cmd.alias): opt.label else: "$# $#" % [opt.alias.join(", "), opt.label] ) let space = blank(maxLen + leftMargin.len - name.len) let showDfValue = ( if opt.defVal.len > 0: " (default: $#)" % [opt.defVal.join(", ")] else: "" ) let desc = opt.info & showDfValue let msg = name & space & desc result.add(wrapMessage(msg, maxLen + 4)) #-- Process the help screen proc processHelp*(data: static Cmdos): string = const usage = makeMsgUsage(data).join("\n") & "\n" const commands = makeMsgCommands(data).join("\n") const options = makeMsgOptions(data).join("\n") result = [usage, commands, options].join("\n") & "\n" - Assistant