diff --git a/README.md b/README.md index 81a16c9..3c86d0f 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,9 @@ Debian users can install scrot from their package manager: $ sudo apt install scrot ``` +Bash and Zsh completion scripts are available in [etc/](./etc). +Distro packagers are encouraged to install them to the appropriate directory. + ## Author ## scrot was originally developed by Tom Gilbert. diff --git a/TODO.md b/TODO.md index a1cf927..fab6ab8 100644 --- a/TODO.md +++ b/TODO.md @@ -11,6 +11,15 @@ Look for any bugs reported downstream or in our github issues and fix them: - - +## Shell completion improvements + +- Improve the bash completion script. Currently it's quite rudimentary. +- Filter out mutually exclusive options in zsh completion. E.g if `-b` is + already provided then `--border` should no longer be considered a potential + match. +- Add some way to explicitly install zsh/bash completion scripts. E.g via the + configure script `./configure --with-zsh-completion-path=...`. + ## Switch to newer Imlib2 interfaces These will require a minimum version bump on imlib2 and so has to be done with diff --git a/etc/bash-completion/scrot b/etc/bash-completion/scrot new file mode 100644 index 0000000..4f5bdff --- /dev/null +++ b/etc/bash-completion/scrot @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Copyright 2024 NRK +# +# This is free and unencumbered software released into the public domain. +# For more information, please refer to + +_scrot_autocomp() { + local -a _scrot_opt_list + IFS=$'\n' _scrot_opt_list=( $(scrot --list-options=tsv) ) 2>/dev/null + local _scrot_opts="" + for ln in "${_scrot_opt_list[@]}"; do + local -a tokens + IFS=$'\t' tokens=( ${ln} ) + + local sopt="${tokens[0]}" + local lopt="${tokens[1]}" + # TODO: better support for flags that accept argument + # local argtype=${tokens[2]%%:*} + # local argdesc=${tokens[2]#*:} + # local desc="[${tokens[3]}]" + _scrot_opts+="--${lopt} " + if [[ "$sopt" = [[:alnum:]]* ]]; then + _scrot_opts+="-${sopt} " + fi + done + printf "%s" "${_scrot_opts% }" +} +complete -W "$(_scrot_autocomp)" scrot diff --git a/etc/zsh-completion/_scrot b/etc/zsh-completion/_scrot new file mode 100644 index 0000000..bf07088 --- /dev/null +++ b/etc/zsh-completion/_scrot @@ -0,0 +1,30 @@ +#compdef scrot +# +# Copyright 2024 NRK +# +# This is free and unencumbered software released into the public domain. +# For more information, please refer to + +local -a args +local list=$($~words[1] --list-options=tsv) 2>/dev/null +for ln in ${(f)list}; do + IFS=$'\t' local tokens=( ${=ln} ) + + local sopt="${tokens[1]}" + local lopt="${tokens[2]}" + local argtype=${tokens[3]%%:*} + local argdesc=${tokens[3]#*:} + local desc="[${tokens[4]}]" + + case "$argtype" in + R) desc+=":$argdesc" ;; # Required + O) sopt+="+"; lopt+="=" ;; # Optional + N) ;; # None + esac + + if [[ "${sopt}" = [[:alnum:]]* ]]; then + args+=( "-${sopt}${desc}" ) + fi + args+=( "--${lopt}${desc}" ) +done +_arguments "${args[@]}"