Skip to content

Commit

Permalink
add zsh and bash completion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
N-R-K committed May 28, 2024
1 parent 78c748a commit 171f38b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ Look for any bugs reported downstream or in our github issues and fix them:
- <https://bugzilla.redhat.com/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&component=scrot&product=Fedora>
- <https://bugs.gentoo.org/buglist.cgi?quicksearch=scrot>

## 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
Expand Down
29 changes: 29 additions & 0 deletions etc/bash-completion/scrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
#
# Copyright 2024 NRK <nrk@disroot.org>
#
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <https://unlicense.org/>

_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
30 changes: 30 additions & 0 deletions etc/zsh-completion/_scrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#compdef scrot
#
# Copyright 2024 NRK <nrk@disroot.org>
#
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <https://unlicense.org/>

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[@]}"

0 comments on commit 171f38b

Please sign in to comment.