Skip to content
Snippets Groups Projects
Verified Commit 692cd94c authored by orestis.malaspin's avatar orestis.malaspin
Browse files

made things more functional

parent aec3d81c
No related branches found
No related tags found
No related merge requests found
Pipeline #29443 passed
...@@ -43,7 +43,7 @@ function computeDepth(cmd: Command | undefined): number { ...@@ -43,7 +43,7 @@ function computeDepth(cmd: Command | undefined): number {
} }
// Computes the maximum number of commands until the root is reached // Computes the maximum number of commands until the root is reached
function computeHeigth(cmd: Command | null): number { function computeHeight(cmd: Command | null): number {
let height = 0 let height = 0
let tmp = cmd let tmp = cmd
while (tmp !== null) { while (tmp !== null) {
...@@ -124,29 +124,30 @@ function generateCommandChain(cmd: Command | null): string { ...@@ -124,29 +124,30 @@ function generateCommandChain(cmd: Command | null): string {
return data.trimEnd() return data.trimEnd()
} }
function optionsToString(cmd: Command): string { function hasOptions(cmd: Command): boolean {
if (cmd.options.length == 0) { return cmd.options.length > 0
return ''
} }
function optionsToString(cmd: Command): string {
return cmd.options.filter(opt => !opt.hidden).map(opt => { return cmd.options.filter(opt => !opt.hidden).map(opt => {
return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a "--${opt.name()}" -d "${opt.description}"` return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a "${opt.short ?? ''} ${opt.long ?? ''}" -d "${opt.description}"`
}).join('\n').concat('\n') }).join('\n').concat('\n')
} }
export function writeFishCompletion(root: Command, filename: string) { export function writeFishCompletion(root: Command, filename: string) {
const commands = flatten(root) const commands = flatten(root)
let data = fishFunction const data = fishFunction.concat(
// add commands and subcommands // add completions for options
commands.forEach(cmd => { commands.filter(cmd => hasOptions(cmd)).map(cmd => optionsToString(cmd)).filter(str => str != '').join('')
data += optionsToString(cmd) ).concat(
if (!isLeaf(cmd)) { // add completions for commands and subcommands
data += cmd.commands.map(subCmd => { commands.filter(
return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"` cmd => !isLeaf(cmd)).map(
}).join('\n').concat('\n') cmd => cmd.commands.map(subCmd => {
} return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
}) }).join('\n').concat('\n')).join('')
)
writeFileSync(filename, data); writeFileSync(filename, data);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment