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

started working of fish completion

parent 8d09a116
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !9. Comments created here will be created in the context of that merge request.
......@@ -9,7 +9,7 @@ import { stateConfigFile } from '../config/ConfigFiles';
import semver from 'semver/preload';
import { version } from '../config/Version';
import Config from '../config/Config';
import {getBashCompletion} from '../helpers/AutoCompletionHelper';
import {getBashCompletion, getFishCompletion} from '../helpers/AutoCompletionHelper';
class CommanderApp {
......@@ -45,6 +45,7 @@ class CommanderApp {
this.registerCommands();
getBashCompletion(this.program, "bash_completion.sh")
getFishCompletion(this.program, "dojo.fish")
this.program.parse();
}
......
......@@ -97,6 +97,10 @@ function isLeaf(tree: CmdNode): boolean {
return tree.children === undefined || tree.children.length == 0
}
function isRoot(tree: CmdNode): boolean {
return tree.parent === undefined
}
function search(tree: CmdNode, cmdName: string): Array<CmdNode> {
if (isLeaf(tree)) {
if (tree.name != cmdName) {
......@@ -173,6 +177,52 @@ function addLine(identLevel: number, pattern: string): string {
return `${' '.repeat(identLevel)}${pattern}\n`
}
const fishFunctions = `### Fish completions for dojo ###
function __fish_dojo_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = dojo ]
return 0
end
return 1
end
function __fish_dojo_using_command
set cmd (commandline -opc)
set num_cmd (count $cmd)
if [ $num_cmd -eq 2 ]
if [ $argv[(math $num_cmd - 1)] = $cmd[$num_cmd] ]
return 0
end
end
return 1
end
function __fish_dojo_using_two_commands
set cmd (commandline -opc)
set num_cmd (count $cmd)
if [ $num_cmd -gt 3 ]
if [ $argv[(math $num_cmd - 2)] = $cmd[(math $num_cmd - 1)] -a $argv[(math $num_cmd - 1)] = $cmd[$num_cmd] ]
return 0
end
end
return 1
end
`
export function getFishCompletion(root: Command, filename: string) {
const tree = buildCmdNode(root)
const commands = Array.from(new Set(flatten(tree).filter(cmd => !isLeaf(cmd)).map(cmd => cmd.name))).map(name => search(tree, name))
let data = fishFunctions
for (const node of commands) {
const cmd = node[0]
data += addLine(0, `complete -f -c dojo -n __fish_dojo_needs_command ${cmd.name} -a "${cmd.children.map(c => c.name).join(" ")} --help -h"`)
}
writeFileSync(filename, data);
}
export function getBashCompletion(root: Command, filename: string) {
const tree = buildCmdNode(root)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment