diff options
| author | tv <tv@krebsco.de> | 2026-11-19 19:03:30 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-19 20:14:09 +0100 |
| commit | 25689b7dc006d5db3e0065d613302c0eb5335efe (patch) | |
| tree | 7a28d2e1ea98a63643d977c9a2ed041e998ae3dd /share/bash-completion/completions/continuum-party | |
| parent | 8a6474557ad9df0b29e7f6fe9f2c231fc9900588 (diff) | |
share/bash-completion: continuum-party
A hand-written completion, where distributions package it:
share/bash-completion/completions/continuum-party, read by
bash-completion when first needed (it uses _init_completion and
_filedir where they are, plain compgen else).
It completes the commands, the intents of `propose` and `fork` (`fork`
without `checkpoint`, with `proposal`), the options of each, files where
one is taken, `--federation` from ~/.config/continuum/federations (or
$XDG_CONFIG_HOME's), and `--revision` and `propose revision` from the
features `--version` names. From the node, asked with the options given
before the command: the proposals on the current version by hash
(`proposals`), for vote, show and fork proposal; the parties (`status`),
for remove, give, keys and nodes; and the places holding a value
(`values`), for write and values.
Diffstat (limited to 'share/bash-completion/completions/continuum-party')
| -rw-r--r-- | share/bash-completion/completions/continuum-party | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/share/bash-completion/completions/continuum-party b/share/bash-completion/completions/continuum-party new file mode 100644 index 0000000..e79eee2 --- /dev/null +++ b/share/bash-completion/completions/continuum-party @@ -0,0 +1,241 @@ +# bash completion for continuum-party, a party's client of a federation -*- shell-script -*- +# +# Completes its commands and options, and from the node it works with (the options given +# before the command: --node, --federation, --config and the rest) the proposals on the +# current version by hash, the parties, the places holding a value; the federations in +# ~/.config/continuum/federations; and the revisions' features it knows. + +# The client, with the options given before the command, its errors left out. +_continuum_party_asking() +{ + "${_continuum_party_client[@]}" "$@" 2>/dev/null +} + +# The proposals on the current version, by hash. +_continuum_party_proposals() +{ + _continuum_party_asking proposals | sed -n 's/^\([0-9a-f]\{64\}\): .*/\1/p' +} + +# The parties of the current version. +_continuum_party_parties() +{ + _continuum_party_asking status | + sed -n 's/^version .* passing a change (\(.*\))$/\1/p' | tr -d ',' +} + +# The places holding a value. +_continuum_party_places() +{ + _continuum_party_asking values | cut -d' ' -f1 +} + +# The features of the revisions the client knows, as `--version` names them. +_continuum_party_features() +{ + "${_continuum_party_client[0]}" --version 2>/dev/null | + grep -o '([a-z0-9-]*)' | tr -d '()' +} + +# The federations, by their directories' names. +_continuum_party_federations() +{ + local dir=${XDG_CONFIG_HOME:-$HOME/.config}/continuum/federations entry + [[ -d $dir ]] || return + for entry in "$dir"/*/; do + [[ -d $entry ]] || continue + entry=${entry%/} + printf '%s\n' "${entry##*/}" + done +} + +# Files, or directories to find them in. +_continuum_party_files() +{ + if declare -F _filedir >/dev/null; then + _filedir + else + COMPREPLY=($(compgen -f -- "$cur")) + fi +} + +_continuum_party_words() +{ + COMPREPLY=($(compgen -W "$1" -- "$cur")) +} + +_continuum_party() +{ + local cur prev words cword + if declare -F _init_completion >/dev/null; then + _init_completion -n =: || return + else + words=("${COMP_WORDS[@]}") + cword=$COMP_CWORD + cur=${words[cword]} + prev=${words[cword - 1]} + fi + + local global_options='--node --token --key --record --federation --config --unset + --help --version' + local commands='init write keys nodes next propose vote again found root fork check + judge proposals show status values twice help' + local intents='admit remove give take keys nodes checkpoint revision' + + # The command, the intent of `propose` and `fork`, and the arguments after them that + # are no options, as far as the word completed. + local i command= intent= word + local -a positional=() + _continuum_party_client=("${words[0]}") + for ((i = 1; i < cword; i++)); do + word=${words[i]} + if [[ -z $command ]]; then + case $word in + --node | --token | --key | --record | --federation | --config | --unset) + _continuum_party_client+=("$word" "${words[i + 1]}") + ((i++)) + ;; + -*) _continuum_party_client+=("$word") ;; + *) command=$word ;; + esac + continue + fi + case $word in + --keys | --nodes | --at | --founder | --rule | --revision | --root | --add | \ + --remove | --set) + # Options taking values: those up to the next option are theirs. + while ((i + 1 < cword)) && [[ ${words[i + 1]} != -* ]]; do + ((i++)) + case $word in + --keys | --nodes | --at | --add | --remove | --set) ;; + *) break ;; + esac + done + ;; + -*) ;; + *) + if [[ -z $intent && ${#positional[@]} -eq 0 ]] && + [[ $command == propose || $command == fork ]] && + [[ " $intents proposal " == *" $word "* ]]; then + intent=$word + else + positional+=("$word") + fi + ;; + esac + done + + # A value of the option before. + case $prev in + --federation) + _continuum_party_words "$(_continuum_party_federations)" + return + ;; + --key | --record | --config | --root | --keys) + _continuum_party_files + return + ;; + --revision) + _continuum_party_words "$(_continuum_party_features)" + return + ;; + --node | --token | --unset | --nodes | --founder | --rule | --at | --add | --remove | \ + --set) + return + ;; + esac + + if [[ -z $command ]]; then + if [[ $cur == -* ]]; then + _continuum_party_words "$global_options" + else + _continuum_party_words "$commands" + fi + return + fi + + local n=${#positional[@]} + case $command in + propose | fork) + local own + if [[ $command == propose ]]; then + own='--yes --print' + else + own='--at --founder' + fi + if [[ -z $intent ]]; then + if [[ $cur == -* ]]; then + [[ $command == propose ]] && _continuum_party_words "$own" + elif ((n == 0)); then + local offered=$intents + [[ $command == fork ]] && offered="${intents/checkpoint/} proposal" + _continuum_party_words "$offered" + local -a named=("${COMPREPLY[@]}") + _continuum_party_files + COMPREPLY+=("${named[@]}") + fi + return + fi + case $intent in + admit) own+=' --keys --nodes' ;; + esac + if [[ $cur == -* ]]; then + _continuum_party_words "$own" + return + fi + case $intent:$n in + remove:0 | give:1 | keys:0 | nodes:0) + local parties + parties=$(_continuum_party_parties) + [[ $intent == give ]] && parties+=' {party}' + _continuum_party_words "$parties" + ;; + keys:*) _continuum_party_files ;; + revision:0) _continuum_party_words "$(_continuum_party_features)" ;; + proposal:0) _continuum_party_words "$(_continuum_party_proposals)" ;; + esac + ;; + vote) + if [[ $cur == -* ]]; then + _continuum_party_words '--through' + elif ((n == 0)); then + _continuum_party_words "$(_continuum_party_proposals)" + elif ((n == 1)); then + _continuum_party_words 'yes no' + fi + ;; + show) + ((n == 0)) && _continuum_party_words "$(_continuum_party_proposals)" + ;; + keys | nodes) + if [[ $cur == -* ]]; then + _continuum_party_words '--list --add --remove --set --force' + fi + ;; + write | values) + if [[ $cur == -* ]]; then + [[ $command == values ]] && _continuum_party_words '--json' + elif ((n == 0)); then + _continuum_party_words "$(_continuum_party_places)" + fi + ;; + init) + [[ $cur == -* ]] && _continuum_party_words '--root' + ;; + next) + [[ $cur == -* ]] && _continuum_party_words '--revision' + ;; + found) + [[ $cur == -* ]] && _continuum_party_words '--nodes --rule --revision' + ;; + root | check | twice) + _continuum_party_files + ;; + help) + ((n == 0)) && _continuum_party_words "$commands" + ;; + esac +} && + complete -F _continuum_party continuum-party + +# ex: filetype=sh |
