[+] Generate shell completions

https://github.com/hykilpikonna/hyfetch/issues/96
This commit is contained in:
Azalea Gui 2023-05-01 17:22:06 -04:00
parent 2007c7c01e
commit 365abe2c80
5 changed files with 212 additions and 5 deletions

View file

@ -298,9 +298,9 @@ def create_config() -> Config:
def create_parser() -> argparse.ArgumentParser:
# Create CLI
hyfetch = color('&l&bhyfetch&~&L')
parser = argparse.ArgumentParser(description=color(f'{hyfetch} - neofetch with flags <3'))
parser = argparse.ArgumentParser(description=color(f'{hyfetch} - neofetch with flags <3'), prog="hyfetch")
parser.add_argument('-c', '--config', action='store_true', help=color(f'Configure {hyfetch}'))
parser.add_argument('-c', '--config', action='store_true', help=color(f'Configure hyfetch'))
parser.add_argument('-C', '--config-file', dest='config_file', default=CONFIG_PATH, help=f'Use another config file')
parser.add_argument('-p', '--preset', help=f'Use preset', choices=list(PRESETS.keys()))
parser.add_argument('-m', '--mode', help=f'Color mode', choices=['8bit', 'rgb'])

View file

@ -0,0 +1,142 @@
# AUTOMATICALLY GENERATED by `shtab`
_shtab_hyfetch_option_strings=('-h' '--help' '-c' '--config' '-C' '--config-file' '-p' '--preset' '-m' '--mode' '-b' '--backend' '--c-scale' '--c-set-l' '-V' '--version' '--debug' '--distro' '--test-distro' '--ascii-file')
_shtab_hyfetch__p_choices=('rainbow' 'transgender' 'nonbinary' 'agender' 'queer' 'genderfluid' 'bisexual' 'pansexual' 'polysexual' 'omnisexual' 'omniromantic' 'gay-men' 'lesbian' 'abrosexual' 'asexual' 'aromantic' 'aroace1' 'aroace2' 'aroace3' 'autosexual' 'intergender' 'greygender' 'akiosexual' 'bigender' 'demigender' 'demiboy' 'demigirl' 'transmasculine' 'transfeminine' 'genderfaun' 'demifaun' 'genderfae' 'demifae' 'neutrois' 'biromantic1' 'biromantic2' 'autoromantic' 'boyflux2' 'finsexual' 'unlabeled1' 'unlabeled2' 'pangender' 'gendernonconforming1' 'gendernonconforming2' 'femboy' 'tomboy' 'gendervoid' 'voidgirl' 'voidboy' 'beiyang' 'burger')
_shtab_hyfetch___preset_choices=('rainbow' 'transgender' 'nonbinary' 'agender' 'queer' 'genderfluid' 'bisexual' 'pansexual' 'polysexual' 'omnisexual' 'omniromantic' 'gay-men' 'lesbian' 'abrosexual' 'asexual' 'aromantic' 'aroace1' 'aroace2' 'aroace3' 'autosexual' 'intergender' 'greygender' 'akiosexual' 'bigender' 'demigender' 'demiboy' 'demigirl' 'transmasculine' 'transfeminine' 'genderfaun' 'demifaun' 'genderfae' 'demifae' 'neutrois' 'biromantic1' 'biromantic2' 'autoromantic' 'boyflux2' 'finsexual' 'unlabeled1' 'unlabeled2' 'pangender' 'gendernonconforming1' 'gendernonconforming2' 'femboy' 'tomboy' 'gendervoid' 'voidgirl' 'voidboy' 'beiyang' 'burger')
_shtab_hyfetch__m_choices=('8bit' 'rgb')
_shtab_hyfetch___mode_choices=('8bit' 'rgb')
_shtab_hyfetch__b_choices=('neofetch' 'fastfetch' 'fastfetch-old')
_shtab_hyfetch___backend_choices=('neofetch' 'fastfetch' 'fastfetch-old')
_shtab_hyfetch__h_nargs=0
_shtab_hyfetch___help_nargs=0
_shtab_hyfetch__c_nargs=0
_shtab_hyfetch___config_nargs=0
_shtab_hyfetch__V_nargs=0
_shtab_hyfetch___version_nargs=0
_shtab_hyfetch___debug_nargs=0
_shtab_hyfetch___test_print_nargs=0
_shtab_hyfetch___ask_exit_nargs=0
# $1=COMP_WORDS[1]
_shtab_compgen_files() {
compgen -f -- $1 # files
}
# $1=COMP_WORDS[1]
_shtab_compgen_dirs() {
compgen -d -- $1 # recurse into subdirs
}
# $1=COMP_WORDS[1]
_shtab_replace_nonword() {
echo "${1//[^[:word:]]/_}"
}
# set default values (called for the initial parser & any subparsers)
_set_parser_defaults() {
local subparsers_var="${prefix}_subparsers[@]"
sub_parsers=${!subparsers_var}
local current_option_strings_var="${prefix}_option_strings[@]"
current_option_strings=${!current_option_strings_var}
completed_positional_actions=0
_set_new_action "pos_${completed_positional_actions}" true
}
# $1=action identifier
# $2=positional action (bool)
# set all identifiers for an action's parameters
_set_new_action() {
current_action="${prefix}_$(_shtab_replace_nonword $1)"
local current_action_compgen_var=${current_action}_COMPGEN
current_action_compgen="${!current_action_compgen_var}"
local current_action_choices_var="${current_action}_choices[@]"
current_action_choices="${!current_action_choices_var}"
local current_action_nargs_var="${current_action}_nargs"
if [ -n "${!current_action_nargs_var}" ]; then
current_action_nargs="${!current_action_nargs_var}"
else
current_action_nargs=1
fi
current_action_args_start_index=$(( $word_index + 1 ))
current_action_is_positional=$2
}
# Notes:
# `COMPREPLY`: what will be rendered after completion is triggered
# `completing_word`: currently typed word to generate completions for
# `${!var}`: evaluates the content of `var` and expand its content as a variable
# hello="world"
# x="hello"
# ${!x} -> ${hello} -> "world"
_shtab_hyfetch() {
local completing_word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=()
prefix=_shtab_hyfetch
word_index=0
_set_parser_defaults
word_index=1
# determine what arguments are appropriate for the current state
# of the arg parser
while [ $word_index -ne $COMP_CWORD ]; do
local this_word="${COMP_WORDS[$word_index]}"
if [[ -n $sub_parsers && " ${sub_parsers[@]} " =~ " ${this_word} " ]]; then
# valid subcommand: add it to the prefix & reset the current action
prefix="${prefix}_$(_shtab_replace_nonword $this_word)"
_set_parser_defaults
fi
if [[ " ${current_option_strings[@]} " =~ " ${this_word} " ]]; then
# a new action should be acquired (due to recognised option string or
# no more input expected from current action);
# the next positional action can fill in here
_set_new_action $this_word false
fi
if [[ "$current_action_nargs" != "*" ]] && \
[[ "$current_action_nargs" != "+" ]] && \
[[ "$current_action_nargs" != *"..." ]] && \
(( $word_index + 1 - $current_action_args_start_index >= \
$current_action_nargs )); then
$current_action_is_positional && let "completed_positional_actions += 1"
_set_new_action "pos_${completed_positional_actions}" true
fi
let "word_index+=1"
done
# Generate the completions
if [[ "${completing_word}" == -* ]]; then
# optional argument started: use option strings
COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") )
else
# use choices & compgen
local IFS=$'\n' # items may contain spaces, so delimit using newline
COMPREPLY=( $([ -n "${current_action_compgen}" ] \
&& "${current_action_compgen}" "${completing_word}") )
unset IFS
COMPREPLY+=( $(compgen -W "${current_action_choices[*]}" -- "${completing_word}") )
fi
return 0
}
complete -o filenames -F _shtab_hyfetch hyfetch

View file

@ -0,0 +1,14 @@
# AUTOMATICALLY GENERATED by `shtab`
complete hyfetch \
'c/--/(version test-distro ascii-file preset c-set-l help debug config-file c-scale config backend mode distro)/' \
'c/-/(C m V h p b c -)/' \
'n/-p/(rainbow transgender nonbinary agender queer genderfluid bisexual pansexual polysexual omnisexual omniromantic gay-men lesbian abrosexual asexual aromantic aroace1 aroace2 aroace3 autosexual intergender greygender akiosexual bigender demigender demiboy demigirl transmasculine transfeminine genderfaun demifaun genderfae demifae neutrois biromantic1 biromantic2 autoromantic boyflux2 finsexual unlabeled1 unlabeled2 pangender gendernonconforming1 gendernonconforming2 femboy tomboy gendervoid voidgirl voidboy beiyang burger)/' \
'n/--preset/(rainbow transgender nonbinary agender queer genderfluid bisexual pansexual polysexual omnisexual omniromantic gay-men lesbian abrosexual asexual aromantic aroace1 aroace2 aroace3 autosexual intergender greygender akiosexual bigender demigender demiboy demigirl transmasculine transfeminine genderfaun demifaun genderfae demifae neutrois biromantic1 biromantic2 autoromantic boyflux2 finsexual unlabeled1 unlabeled2 pangender gendernonconforming1 gendernonconforming2 femboy tomboy gendervoid voidgirl voidboy beiyang burger)/' \
'n/-m/(8bit rgb)/' \
'n/--mode/(8bit rgb)/' \
'n/-b/(neofetch fastfetch fastfetch-old)/' \
'n/--backend/(neofetch fastfetch fastfetch-old)/' \
'p/*/()/'

View file

@ -0,0 +1,51 @@
#compdef hyfetch
# AUTOMATICALLY GENERATED by `shtab`
_shtab_hyfetch_commands() {
local _commands=(
)
_describe 'hyfetch commands' _commands
}
_shtab_hyfetch_options=(
"(- : *)"{-h,--help}"[show this help message and exit]"
{-c,--config}"[Configure hyfetch]"
{-C,--config-file}"[Use another config file]:config_file:"
{-p,--preset}"[Use preset]:preset:(rainbow transgender nonbinary agender queer genderfluid bisexual pansexual polysexual omnisexual omniromantic gay-men lesbian abrosexual asexual aromantic aroace1 aroace2 aroace3 autosexual intergender greygender akiosexual bigender demigender demiboy demigirl transmasculine transfeminine genderfaun demifaun genderfae demifae neutrois biromantic1 biromantic2 autoromantic boyflux2 finsexual unlabeled1 unlabeled2 pangender gendernonconforming1 gendernonconforming2 femboy tomboy gendervoid voidgirl voidboy beiyang burger)"
{-m,--mode}"[Color mode]:mode:(8bit rgb)"
{-b,--backend}"[Choose a \*fetch backend]:backend:(neofetch fastfetch fastfetch-old)"
"--c-scale[Lighten colors by a multiplier]:scale:"
"--c-set-l[Set lightness value of the colors]:light:"
{-V,--version}"[Check version]"
"--debug[Debug mode]"
{--distro,--test-distro}"[Test for a specific distro]:distro:"
"--ascii-file[Use a specific file for the ascii art]:ascii_file:"
)
_shtab_hyfetch() {
local context state line curcontext="$curcontext" one_or_more='(-)*' remainder='(*)'
if ((${_shtab_hyfetch_options[(I)${(q)one_or_more}*]} + ${_shtab_hyfetch_options[(I)${(q)remainder}*]} == 0)); then # noqa: E501
_shtab_hyfetch_options+=(': :_shtab_hyfetch_commands' '*::: :->hyfetch')
fi
_arguments -C $_shtab_hyfetch_options
case $state in
hyfetch)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:_shtab_hyfetch-$line[1]:"
case $line[1] in
esac
esac
}
typeset -A opt_args
_shtab_hyfetch "$@"

View file

@ -3,6 +3,6 @@
# Echo all commands
set -x
shtab --shell=bash hyfetch.main.create_parser > hyfetch/scripts/autocomplete.bash
shtab --shell=zsh hyfetch.main.create_parser > hyfetch/scripts/autocomplete.zsh
shtab --shell=tcsh hyfetch.main.create_parser > hyfetch/scripts/autocomplete.csh
shtab --shell=bash -u hyfetch.main.create_parser > hyfetch/scripts/autocomplete.bash
shtab --shell=zsh -u hyfetch.main.create_parser > hyfetch/scripts/autocomplete.zsh
shtab --shell=tcsh -u hyfetch.main.create_parser > hyfetch/scripts/autocomplete.csh