feat(): Begin bash completion functions and os script

This commit is contained in:
Chris Daßler 2024-06-30 22:40:34 +02:00
parent a09d060593
commit 4c114c7ee4
3 changed files with 123 additions and 28 deletions

View File

@ -0,0 +1,50 @@
[[ "$1" != "" ]] || {
echo "Usage: source bash_template.sh <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") )
# Get all options from commander
COMPREPLY=( $(compgen -W '$(./'$1' compgen --shell bash)' -- "$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
}

44
os Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env node
const This = require('./this');
class Os extends This {
constructor() {
super();
this.version = '0.0.1';
this.description = 'Shows informations about the operating system';
super.init(); // initialize commander with overridden version and description
}
info() {
/**
* @program()
* @command()
*/
console.warn(`Function ${this._getCurrentFunctionName()} not implemented`);
}
check() {
/**
* @program()
* @command()
*/
console.warn(`Function ${this._getCurrentFunctionName()} not implemented`);
}
checkEnv() {
/**
* @program()
* @command()
*/
console.warn(`Function ${this._getCurrentFunctionName()} not implemented`);
}
start() {
super.start();
}
}
// main
const os = new Os();
os.start();

57
this
View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
const os = require('os');
const path = require('path');
const { Command } = require('commander');
const shell = require('shelljs');
@ -28,6 +29,16 @@ class This {
outputError: (str, write) => write(this._echoInRed(str)),
});
// add functions for shell completion
this.program
.command('compgen')
.option('-s, --shell <name>', 'You can select between [bash]', 'bash')
.action((arg) => {
const words = this.listMethods();
console.log(words.join(os.EOL));
return process.exit();
});
// dynamically add all methods with appropriate docstring to commander
this.listMethods().forEach((method) => {
this._parseDocStringToCmd(this[method]);
@ -40,7 +51,7 @@ class This {
listMethods() {
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this)).filter(
(prop) => typeof this[prop] === 'function' && prop !== 'constructor' && !prop.startsWith('_')
(prop) => typeof this[prop] === 'function' && prop !== 'constructor' && prop !== 'start' && !prop.startsWith('_')
);
return methods;
}
@ -69,37 +80,27 @@ class This {
/**
* @program()
*/
if (this.debugLevel > 0) {
this.program.exitOverride();
this.program.exitOverride();
try {
this.program.parse();
// show help even if there are no commands registered
if (this.program.commands.length == 0) {
this.program.outputHelp();
}
} catch (err) {
// this.execCmd(`${this.workingDir}/log echo '\n'`);
// this.execCmd(`${this.workingDir}/log error '${err}'`);
process.exit(1);
}
if (Object.keys(this.program.opts()).length || this.program.args.length) {
// Debugging commander options and arguments
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
this.execCmd(`${this.workingDir}/log echo '\n'`);
this.execCmd(`${this.workingDir}/log echo 'Options: ${opts}'`);
this.execCmd(`${this.workingDir}/log echo 'Remaining arguments: ${args}'`);
}
} else {
try {
this.program.parse();
// show help even if there are no commands registered
if (this.program.commands.length == 0) {
this.program.outputHelp();
if (this.debugLevel == 0) {
process.exit(1);
}
} catch (err) {
// this.execCmd(`${this.workingDir}/log echo '\n'`);
// this.execCmd(`${this.workingDir}/log error '${err}'`);
process.exit(1);
}
if (Object.keys(this.program.opts()).length || this.program.args.length) {
// Debugging commander options and arguments
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
this.execCmd(`${this.workingDir}/log echo '\n'`);
this.execCmd(`${this.workingDir}/log echo 'Options: ${opts}'`);
this.execCmd(`${this.workingDir}/log echo 'Remaining arguments: ${args}'`);
}
}