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 {
}
// 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 tmp = cmd
while (tmp !== null) {
......@@ -124,29 +124,30 @@ function generateCommandChain(cmd: Command | null): string {
return data.trimEnd()
}
function optionsToString(cmd: Command): string {
if (cmd.options.length == 0) {
return ''
function hasOptions(cmd: Command): boolean {
return cmd.options.length > 0
}
function optionsToString(cmd: Command): string {
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')
}
export function writeFishCompletion(root: Command, filename: string) {
const commands = flatten(root)
let data = fishFunction
// add commands and subcommands
commands.forEach(cmd => {
data += optionsToString(cmd)
if (!isLeaf(cmd)) {
data += cmd.commands.map(subCmd => {
return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
}).join('\n').concat('\n')
}
})
const data = fishFunction.concat(
// add completions for options
commands.filter(cmd => hasOptions(cmd)).map(cmd => optionsToString(cmd)).filter(str => str != '').join('')
).concat(
// add completions for commands and subcommands
commands.filter(
cmd => !isLeaf(cmd)).map(
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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment