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

fish completion done

parent 14ab635b
No related branches found
No related tags found
No related merge requests found
Pipeline #29386 passed
This commit is part of merge request !9. Comments created here will be created in the context of that merge request.
...@@ -23,6 +23,17 @@ function isLeaf(cmd: Command): boolean { ...@@ -23,6 +23,17 @@ function isLeaf(cmd: Command): boolean {
return cmd.commands.length == 0 return cmd.commands.length == 0
} }
function flatten(cmd: Command): Array<Command> {
if (isLeaf(cmd)) {
return [cmd]
} else {
return cmd.commands
.map(child => flatten(child))
.reduce((acc, cmd) => acc.concat(cmd), [cmd])
}
}
// Computes the maximum number of commands until a leaf is reached
function computeDepth(cmd: Command | undefined): number { function computeDepth(cmd: Command | undefined): number {
if (cmd === undefined) { if (cmd === undefined) {
return 0 return 0
...@@ -31,6 +42,17 @@ function computeDepth(cmd: Command | undefined): number { ...@@ -31,6 +42,17 @@ function computeDepth(cmd: Command | undefined): number {
} }
} }
// Computes the maximum number of commands until the root is reached
function computeHeigth(cmd: Command | null): number {
let height = 0
let tmp = cmd
while (tmp !== null) {
tmp = tmp.parent
height += 1
}
return height
}
function getOptions(cmd: Command): string { function getOptions(cmd: Command): string {
// we remove <args>, [command], and , from option lines // we remove <args>, [command], and , from option lines
return cmd.options.filter(opt => !opt.hidden).map(opt => return cmd.options.filter(opt => !opt.hidden).map(opt =>
...@@ -93,22 +115,40 @@ export function writeBashCompletion(root: Command, filename: string) { ...@@ -93,22 +115,40 @@ export function writeBashCompletion(root: Command, filename: string) {
const prefix = 'complete -f -c dojo -n \'__fish_dojo_using_commands' const prefix = 'complete -f -c dojo -n \'__fish_dojo_using_commands'
function generateFishSubCommands(cmd: Command, crtDepth: number): string { function generateCommandChain(cmd: Command | null): string {
let data = '' let data = ''
for (const arg of cmd.commands) { while (cmd !== null) {
data += `${prefix} ${crtDepth} ${cmd.name()}' -a ${arg.name()} -d "${arg.description()}"\n` data = cmd.name().concat(` ${data}`)
cmd = cmd.parent
} }
return data return data.trimEnd()
} }
export function writeFishCompletion(root: Command, filename: string) { export function writeFishCompletion(root: Command, filename: string) {
const depth = computeDepth(root) const commands = flatten(root)
let data = let data = fishFunction
fishFunction // add commands and subcommands
for (let i = 1; i <= depth; i++) { commands.forEach(cmd => {
data += generateFishSubCommands(root, i) if (isLeaf(cmd)) {
cmd.options.forEach(opt => {
if (!opt.hidden) {
data += `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a "--${opt.name()}" -d "${opt.description}"\n`
}
})
}
cmd.commands.forEach(subCmd => {
data += `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"\n`
if (cmd.options.length > 0) {
cmd.options.forEach(opt => {
if (!opt.hidden) {
data += `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a "--${opt.name()}" -d "${opt.description}"\n`
}
})
} }
})
})
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