Compare commits

...

1 Commits

Author SHA1 Message Date
f20e2bd522 feat(): Begin tab completion with omelette 2024-06-25 11:05:38 +02:00
5 changed files with 123 additions and 9 deletions

View File

@ -0,0 +1,47 @@
[[ "$1" != "" ]] || {
echo "Usage: source _completion_template <scriptname>"
}
[[ "$1" = "" ]] || {
_pm()
{
local cur prev
local IFS=$'\n'
# Retrieve the original value
local width=$(bind -v | sed -n 's/^set completion-display-width //p')
if [[ $width -ne 0 ]]; then
# Change the readline variable
bind "set completion-display-width 0"
# On the first tab press, expand a common prefix
# On the second tab press, list out options
# Any subsequent tab presses cycle through these options
bind "set show-all-if-ambiguous on"
bind "set menu-complete-display-prefix on"
bind "TAB: menu-complete"
bind "set colored-completion-prefix on"
bind "set colored-stats on"
# Set up PROMPT_COMMAND to reset itself to its current value
PROMPT_COMMAND="PROMPT_COMMAND=$(printf %q "$PROMPT_COMMAND")"
# Set up PROMPT_COMMAND to reset the readline variable
PROMPT_COMMAND+="; bind 'set completion-display-width $width'"
fi
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
# Get all options from omelette
COMPREPLY=( $(compgen -W '$(./'$1' --compbash --compgen "${COMP_CWORD}" "${prev}" "${COMP_LINE}")' -- "$cur") )
# If no options available, print files and folders
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=( $(compgen -f -o default -o plusdirs $cur) )
fi
return 0
}
complete -F _pm $1
}

25
pm
View File

@ -1,6 +1,7 @@
#!/usr/bin/env node
const path = require('path');
const omelette = require('omelette');
const { Command } = require('commander');
const program = new Command();
@ -9,10 +10,12 @@ const program = new Command();
// When `.command()` is invoked with a description argument,
// this tells Commander that you're going to use a stand-alone executable for the subcommand.
//
// Only `install` and `list` are implemented, see pm-install and pm-list.js
// See files pm-install, pm-search, pm-list and myUpdateSubCommand
scriptName = path.parse(process.argv[1]).base;
program
.name(path.parse(process.argv[1]).base)
.name(scriptName)
.version('0.0.1')
.description('Fake package manager')
.command('install [name]', 'install one or more packages')
@ -24,8 +27,26 @@ program
})
.command('list', 'list packages installed', { isDefault: false });
// function description for bash-completion
omelette(scriptName).tree({
install: ['name'],
search: ['query'],
update: [],
list: []
}).init()
program.parse();
// // If you want to have a setup feature, you can use `omeletteInstance.setupShellInitFile()` function.
// if (~process.argv.indexOf('--setup')) {
// omelette.completion.setupShellInitFile()
// }
// // Similarly, if you want to tear down autocompletion, use `omeletteInstance.cleanupShellInitFile()`
// if (~process.argv.indexOf('--cleanup')) {
// omelette.completion.cleanupShellInitFile()
// }
// Try the following on macOS or Linux:
// ./examples/pm
//

View File

@ -9,8 +9,10 @@ const errorColor = (str) => {
return `\x1b[31m${str}\x1b[0m`;
}
scriptName = path.parse(process.argv[1]).base;
program
.name(path.parse(process.argv[1]).base)
.name(scriptName)
.version('0.0.1')
.description('Install package with fake package manager')
.usage("-n 3 32 -l x y z -- op")

37
pm-list
View File

@ -1,11 +1,42 @@
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const omelette = require('omelette');
const { Command } = require('commander');
const program = new Command();
scriptName = path.parse(process.argv[1]).base;
program
.name(path.parse(process.argv[1]).base)
.name(scriptName)
.version('0.0.1')
.description('List packages of fake package manager')
.outputHelp();
.description('List packages of fake package manager');
// Write the CLI template.
const completion = omelette(`${scriptName} <actions> <user>`);
// Define new actions completion
completion.on('actions', ({ reply }) => {
reply(['firstCommand'])
})
completion.on('user', ({ reply }) => {
reply(fs.readdirSync('/Users/'))
})
program
.command('firstCommand')
.description('This is our first command')
.action(() => {
console.log('Fancy stuff happens here');
})
// Initialize the omelette and parse args
completion.init()
program.parse();
if (!Object.keys(program.opts()).length && !program.args.length) {
program.outputHelp();
}

View File

@ -1,11 +1,24 @@
#!/usr/bin/env node
const path = require('path');
const omelette = require('omelette');
const { Command } = require('commander');
const program = new Command();
scriptName = path.parse(process.argv[1]).base;
program
.name(path.parse(process.argv[1]).base)
.name(scriptName)
.version('0.0.1')
.description('Search packages of fake package manager')
.outputHelp();
.description('Search packages of fake package manager');
// Write the CLI template.
const completion = omelette(`${scriptName}`);
// Initialize the omelette and parse args
completion.init()
program.parse();
if (!Object.keys(program.opts()).length && !program.args.length) {
program.outputHelp();
}