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

added functions to make the string composition a bit nicier. still ugly imo

parent b005f201
No related branches found
No related tags found
1 merge request!8Draft: Resolve "Add bash completion helper function"
Pipeline #29369 passed
...@@ -121,26 +121,21 @@ function flatten(tree: CmdNode): Array<CmdNode> { ...@@ -121,26 +121,21 @@ function flatten(tree: CmdNode): Array<CmdNode> {
} }
} }
function addWords(cmd: CmdNode, prefix: string): string {
return `${prefix} words="${cmd.children.map(c => c.name).join(" ")} --help -h"\n`
}
function printWordsGrandParents(node: CmdNode): string { function printWordsGrandParents(node: CmdNode): string {
let parentWords = "" let parentWords = ""
let prefix = "" let ident = 3
if (node.parent !== undefined) { if (node.parent !== undefined) {
parentWords = parentWords += openCase(ident, 'grand_parent')
` case "\${grand_parent}" in ident += 1
${node.parent})\n` parentWords += addLine(ident, `${node.parent})`)
prefix = " " ident += 1
} }
parentWords += addWords(node, prefix) parentWords += addLine(ident, `words="${node.children.map(c => c.name).join(" ")} --help -h"`)
ident -= 1
parentWords += addLine(ident, ';;')
if (node.parent !== undefined) { if (node.parent !== undefined) {
parentWords += ident -= 1
` ;; parentWords += `${closeCase(ident)}`
*)
;;
esac\n`
} }
return parentWords return parentWords
} }
...@@ -164,37 +159,46 @@ function buildCmdNode(root: Command): CmdNode { ...@@ -164,37 +159,46 @@ function buildCmdNode(root: Command): CmdNode {
return createCmdNode(root.name(), root.commands.map(subCmd => getSubCmdNode(subCmd, root)).concat(getOptions(root))) return createCmdNode(root.name(), root.commands.map(subCmd => getSubCmdNode(subCmd, root)).concat(getOptions(root)))
} }
function openCase(identLevel: number, pattern: string): string {
return `${' '.repeat(identLevel)}case "\${${pattern}}" in\n`
}
function closeCase(identLevel: number): string {
return `${' '.repeat(identLevel + 1)}*)\n`
+ `${' '.repeat(identLevel + 1)};;\n`
+ `${' '.repeat(identLevel)}esac\n`
}
function addLine(identLevel: number, pattern: string): string {
return `${' '.repeat(identLevel)}${pattern}\n`
}
export function getBashCompletion(root: Command, filename: string) { export function getBashCompletion(root: Command, filename: string) {
const tree = buildCmdNode(root) const tree = buildCmdNode(root)
let data = let data =
`#/usr/bin/env bash\nfunction _dojo_completions() `${addLine(0, '#/usr/bin/env bash\nfunction _dojo_completions()')}`
{ + `${addLine(0, '{')}${addLine(1, 'latest="${COMP_WORDS[$COMP_CWORD]}"')}`
latest="\${COMP_WORDS[$COMP_CWORD]}" + `${addLine(1, 'parent="${COMP_WORDS[$COMP_CWORD - 1]}"')}`
parent="\${COMP_WORDS[$COMP_CWORD - 1]}" + `${addLine(1, 'grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"')}`
grand_parent="\${COMP_WORDS[$COMP_CWORD - 2]}" + `${addLine(1, 'words=""')}${openCase(1, "parent")}`
words=""
case "\${parent}" in\n`
const commands = Array.from(new Set(flatten(tree).filter(cmd => !isLeaf(cmd)).map(cmd => cmd.name))).map(name => search(tree, name)) const commands = Array.from(new Set(flatten(tree).filter(cmd => !isLeaf(cmd)).map(cmd => cmd.name))).map(name => search(tree, name))
for (const node of commands) { for (const node of commands) {
const cmd = node[0] const cmd = node[0]
data += ` ${cmd.name})\n` data += addLine(2, `${cmd.name})`)
if (cmd !== undefined && node.length == 1) { if (cmd !== undefined && node.length == 1) {
data += addWords(cmd, "") data += addLine(3, `words="${cmd.children.map(c => c.name).join(" ")} --help -h"`)
} else if (cmd !== undefined && node.length > 1) { } else if (cmd !== undefined && node.length > 1) {
node.forEach(n => data += printWordsGrandParents(n)) node.forEach(n => data += printWordsGrandParents(n))
} }
data += " ;;\n" data += addLine(2, ';;')
} }
data += data +=
` *) `${closeCase(1)}`
;; + `${addLine(1, 'COMPREPLY=($(compgen -W "$words" -- $latest))')}`
esac + `${addLine(1, 'return 0')}`
COMPREPLY=($(compgen -W "$words" -- $latest)) + `${addLine(0, '}')}`
return 0 + `${addLine(0, 'complete -F _dojo_completions dojo')}`
}
complete -F _dojo_completions dojo
`
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