Skip to content

Commit

Permalink
add zsh and bash completion scripts
Browse files Browse the repository at this point in the history
Closes: #314
  • Loading branch information
N-R-K committed Jun 4, 2024
1 parent 0bb2810 commit 9c6da4b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ man_MANS = man/scrot.1

dist_doc_DATA = AUTHORS ChangeLog CONTRIBUTING.md doc/scrot.png FAQ.md README.md TODO.md

EXTRA_DIST = $(man_MANS) autogen.sh deps.pc
EXTRA_DIST = $(man_MANS) autogen.sh deps.pc etc/

SUBDIRS = src

Expand Down
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
11 changes: 11 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ 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 install zsh/bash completion scripts. Since the directory where
completion script go into can vary, we want the user to explicitly set the
installation directory. 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 9c6da4b

Please sign in to comment.