Skip to content

Commit 9c6da4b

Browse files
committed
add zsh and bash completion scripts
Closes: #314
1 parent 0bb2810 commit 9c6da4b

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ man_MANS = man/scrot.1
3838

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

41-
EXTRA_DIST = $(man_MANS) autogen.sh deps.pc
41+
EXTRA_DIST = $(man_MANS) autogen.sh deps.pc etc/
4242

4343
SUBDIRS = src
4444

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Debian users can install scrot from their package manager:
9999
$ sudo apt install scrot
100100
```
101101

102+
Bash and Zsh completion scripts are available in [etc/](./etc).
103+
Distro packagers are encouraged to install them to the appropriate directory.
104+
102105
## Author ##
103106

104107
scrot was originally developed by Tom Gilbert.

TODO.md

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ Look for any bugs reported downstream or in our github issues and fix them:
1111
- <https://bugzilla.redhat.com/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&component=scrot&product=Fedora>
1212
- <https://bugs.gentoo.org/buglist.cgi?quicksearch=scrot>
1313

14+
## Shell completion improvements
15+
16+
- Improve the bash completion script. Currently it's quite rudimentary.
17+
- Filter out mutually exclusive options in zsh completion. E.g if `-b` is
18+
already provided then `--border` should no longer be considered a potential
19+
match.
20+
- Add some way to install zsh/bash completion scripts. Since the directory where
21+
completion script go into can vary, we want the user to explicitly set the
22+
installation directory. E.g via the configure script
23+
`./configure --with-zsh-completion-path=...`.
24+
1425
## Switch to newer Imlib2 interfaces
1526

1627
These will require a minimum version bump on imlib2 and so has to be done with

etc/bash-completion/scrot

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2024 NRK <[email protected]>
4+
#
5+
# This is free and unencumbered software released into the public domain.
6+
# For more information, please refer to <https://unlicense.org/>
7+
8+
_scrot_autocomp() {
9+
local -a _scrot_opt_list
10+
IFS=$'\n' _scrot_opt_list=( $(scrot --list-options=tsv) ) 2>/dev/null
11+
local _scrot_opts=""
12+
for ln in "${_scrot_opt_list[@]}"; do
13+
local -a tokens
14+
IFS=$'\t' tokens=( ${ln} )
15+
16+
local sopt="${tokens[0]}"
17+
local lopt="${tokens[1]}"
18+
# TODO: better support for flags that accept argument
19+
# local argtype=${tokens[2]%%:*}
20+
# local argdesc=${tokens[2]#*:}
21+
# local desc=${tokens[3]}
22+
_scrot_opts+="--${lopt} "
23+
if [[ "$sopt" = [[:alnum:]]* ]]; then
24+
_scrot_opts+="-${sopt} "
25+
fi
26+
done
27+
printf "%s" "${_scrot_opts% }"
28+
}
29+
complete -W "$(_scrot_autocomp)" scrot

etc/zsh-completion/_scrot

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#compdef scrot
2+
#
3+
# Copyright 2024 NRK <[email protected]>
4+
#
5+
# This is free and unencumbered software released into the public domain.
6+
# For more information, please refer to <https://unlicense.org/>
7+
8+
local -a args
9+
local list=$($~words[1] --list-options=tsv) 2>/dev/null
10+
for ln in ${(f)list}; do
11+
IFS=$'\t' local tokens=( ${=ln} )
12+
13+
local sopt="${tokens[1]}"
14+
local lopt="${tokens[2]}"
15+
local argtype=${tokens[3]%%:*}
16+
local argdesc=${tokens[3]#*:}
17+
local desc="[${tokens[4]}]"
18+
19+
case "$argtype" in
20+
R) desc+=":$argdesc" ;; # Required
21+
O) sopt+="+"; lopt+="=" ;; # Optional
22+
N) ;; # None
23+
esac
24+
25+
if [[ "${sopt}" = [[:alnum:]]* ]]; then
26+
args+=( "-${sopt}${desc}" )
27+
fi
28+
args+=( "--${lopt}${desc}" )
29+
done
30+
_arguments "${args[@]}"

0 commit comments

Comments
 (0)