Skip to content

Commit b43d31b

Browse files
okapiaEnselic
authored andcommitted
Improvements and fixes to zsh completion
* Use correct return status so that approximate completion isn't broken * Follow zsh conventions on description forms * Some options can be used multiple times, e.g. -H, -r, --file-name * Set completion context correctly for the cache subcommand * Better completion for --map-syntax argument * Add --nonprintable-notation option * Correct some of the mutual exclusion lists for options
1 parent ad3ff26 commit b43d31b

File tree

2 files changed

+64
-52
lines changed

2 files changed

+64
-52
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Refactor and cleanup build script #2756 (@cyqsimon)
1111
- Checks changelog has been written to for PRs in CI #2766 (@cyqsimon)
1212
- Minor benchmark script improvements #2768 (@cyqsimon)
13+
- Update and improve `zsh` completion, see #2772 (@okapia)
1314

1415
## Syntaxes
1516

assets/completions/bat.zsh.in

+63-52
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#compdef {{PROJECT_EXECUTABLE}}
22

3-
local context state state_descr line
3+
local curcontext="$curcontext" ret=1
4+
local -a state state_descr line
45
typeset -A opt_args
56

67
(( $+functions[_{{PROJECT_EXECUTABLE}}_cache_subcommand] )) ||
78
_{{PROJECT_EXECUTABLE}}_cache_subcommand() {
89
local -a args
910
args=(
10-
'(-b --build -c --clear)'{-b,--build}'[Initialize or update the syntax/theme cache]'
11-
'(-b --build -c --clear)'{-c,--clear}'[Remove the cached syntax definitions and themes]'
12-
'(--source)'--source='[Use a different directory to load syntaxes and themes from]:directory:_files -/'
13-
'(--target)'--target='[Use a different directory to store the cached syntax and theme set]:directory:_files -/'
14-
'(--blank)'--blank'[Create completely new syntax and theme sets]'
15-
'(: -)'{-h,--help}'[Prints help information]'
16-
'*: :'
11+
'(-b --build -c --clear)'{-b,--build}'[initialize or update the syntax/theme cache]'
12+
'(-b --build -c --clear)'{-c,--clear}'[remove the cached syntax definitions and themes]'
13+
--source='[specify directory to load syntaxes and themes from]:directory:_files -/'
14+
--target='[specify directory to store the cached syntax and theme set in]:directory:_files -/'
15+
--blank'[create completely new syntax and theme sets]'
16+
--acknowledgements'[build acknowledgements.bin]'
17+
'(: -)'{-h,--help}'[show help information]'
1718
)
1819

1920
_arguments -S -s $args
@@ -23,69 +24,79 @@ _{{PROJECT_EXECUTABLE}}_cache_subcommand() {
2324
_{{PROJECT_EXECUTABLE}}_main() {
2425
local -a args
2526
args=(
26-
'(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]'
27-
'*'{-p,--plain}'[Show plain style (alias for `--style=plain`), repeat twice to disable disable automatic paging (alias for `--paging=never`)]'
28-
'(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]:<language>:->language'
29-
'(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:<N\:M>...'
30-
'(--file-name)'--file-name'[Specify the name to display for a file]:<name>...:_files'
31-
'(-d --diff)'--diff'[Only show lines that have been added/removed/modified]'
32-
'(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]:<N> (lines):()'
33-
'(--tabs)'--tabs'[Set the tab width to T spaces]:<T> (tab width):()'
34-
'(--wrap)'--wrap='[Specify the text-wrapping mode]:<when>:(auto never character)'
35-
'(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:<width>'
36-
'(-n --number)'{-n,--number}'[Show line numbers]'
37-
'(--color)'--color='[When to use colors]:<when>:(auto never always)'
38-
'(--italic-text)'--italic-text='[Use italics in output]:<when>:(always never)'
39-
'(--decorations)'--decorations='[When to show the decorations]:<when>:(auto never always)'
40-
'(--paging)'--paging='[Specify when to use the pager]:<when>:(auto never always)'
41-
'(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:<glob\:syntax>...'
42-
'(--theme)'--theme='[Set the color theme for syntax highlighting]:<theme>:->theme'
43-
'(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]'
44-
'(--style)'--style='[Comma-separated list of style elements to display]:<components>:->style'
45-
'(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:<N\:M>...'
46-
'(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]'
47-
'(: --no-config)'--no-config'[Do not use the configuration file]'
48-
'(: --no-custom-assets)'--no-custom-assets'[Do not load custom assets]'
49-
'(: --lessopen)'--lessopen'[Enable the $LESSOPEN preprocessor]'
50-
'(: --no-lessopen)'--no-lessopen'[Disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)]'
51-
'(: --config-dir)'--config-dir'[Show bat'"'"'s configuration directory]'
52-
'(: --config-file)'--config-file'[Show path to the configuration file]'
53-
'(: --generate-config-file)'--generate-config-file'[Generates a default configuration file]'
54-
'(: --cache-dir)'--cache-dir'[Show bat'"'"'s cache directory]'
55-
'(: -)'{-h,--help}'[Print this help message]'
56-
'(: -)'{-V,--version}'[Show version information]'
57-
'*: :_files'
27+
'(-A --show-all)'{-A,--show-all}'[show non-printable characters (space, tab, newline, ..)]'
28+
--nonprintable-notation='[specify how to display non-printable characters when using --show-all]:notation:(caret unicode)'
29+
\*{-p,--plain}'[show plain style (alias for `--style=plain`), repeat twice to disable disable automatic paging (alias for `--paging=never`)]'
30+
'(-l --language)'{-l+,--language=}'[set the language for syntax highlighting]:language:->languages'
31+
\*{-H+,--highlight-line=}'[highlight specified block of lines]:start\:end'
32+
\*--file-name='[specify the name to display for a file]:name:_files'
33+
'(-d --diff)'--diff'[only show lines that have been added/removed/modified]'
34+
--diff-context='[specify lines of context around added/removed/modified lines when using `--diff`]:lines'
35+
--tabs='[set the tab width]:tab width [4]'
36+
--wrap='[specify the text-wrapping mode]:mode [auto]:(auto never character)'
37+
'!(--wrap)'{-S,--chop-long-lines}
38+
--terminal-width='[explicitly set the width of the terminal instead of determining it automatically]:width'
39+
'(-n --number --diff --diff-context)'{-n,--number}'[show line numbers]'
40+
--color='[specify when to use colors]:when:(auto never always)'
41+
--italic-text='[use italics in output]:when:(always never)'
42+
--decorations='[specify when to show the decorations]:when:(auto never always)'
43+
--paging='[specify when to use the pager]:when:(auto never always)'
44+
'(-m --map-syntax)'{-m+,--map-syntax=}'[map a glob pattern to an existing syntax name]: :->syntax-maps'
45+
'(--theme)'--theme='[set the color theme for syntax highlighting]:theme:->themes'
46+
'(: --list-themes --list-languages -L)'--list-themes'[show all supported highlighting themes]'
47+
--style='[comma-separated list of style elements to display]: : _values "style [default]"
48+
default auto full plain changes header header-filename header-filesize grid rule numbers snip'
49+
\*{-r+,--line-range=}'[only print the specified line range]:start\:end'
50+
'(* -)'{-L,--list-languages}'[display all supported languages]'
51+
"--no-config[don't use the configuration file]"
52+
"--no-custom-assets[don't load custom assets]"
53+
'(--no-lessopen)'--lessopen'[enable the $LESSOPEN preprocessor]'
54+
'(--lessopen)'--no-lessopen'[disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)]'
55+
'(* -)'--config-dir"[show bat's configuration directory]"
56+
'(* -)'--config-file'[show path to the configuration file]'
57+
'(* -)'--generate-config-file'[generate a default configuration file]'
58+
'(* -)'--cache-dir"[show bat's cache directory]"
59+
'(* -)'{-h,--help}'[show help information]'
60+
'(* -)'{-V,--version}'[show version information]'
61+
'*: :{ _files || compadd cache }'
5862
)
5963

60-
_arguments -S -s $args
64+
_arguments -S -s $args && ret=0
6165

6266
case "$state" in
63-
language)
67+
syntax-maps)
68+
if ! compset -P '*:'; then
69+
_message -e patterns 'glob pattern:language'
70+
return
71+
fi
72+
;& # fall-through
73+
74+
languages)
6475
local IFS=$'\n'
6576
local -a languages
6677
languages=( $({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') )
6778

68-
_describe 'language' languages
79+
_describe 'language' languages && ret=0
6980
;;
7081

71-
theme)
72-
local IFS=$'\n'
73-
local -a themes
74-
themes=( $({{PROJECT_EXECUTABLE}} --list-themes | sort) )
75-
76-
_values 'theme' $themes
77-
;;
82+
themes)
83+
local -a themes expl
84+
themes=( ${(f)"$(_call_program themes {{PROJECT_EXECUTABLE}} --list-themes)"} )
7885

79-
style)
80-
_values -s , 'style' default auto full plain changes header header-filename header-filesize grid rule numbers snip
86+
_wanted themes expl 'theme' compadd -a themes && ret=0
8187
;;
8288
esac
89+
90+
return ret
8391
}
8492

8593
case $words[2] in
8694
cache)
8795
## Completion of the 'cache' command itself is removed for better UX
8896
## See https://github.com/sharkdp/bat/issues/2085#issuecomment-1271646802
97+
shift words
98+
(( CURRENT-- ))
99+
curcontext="${curcontext%:*}-${words[1]}:"
89100
_{{PROJECT_EXECUTABLE}}_cache_subcommand
90101
;;
91102

0 commit comments

Comments
 (0)