From 62bf8d46f6ad0cf05f7baabc0f17cff43c3c03d5 Mon Sep 17 00:00:00 2001 From: Kienyew Date: Sat, 8 Aug 2020 13:49:53 +0800 Subject: [PATCH 1/8] Add zsh completion --- assets/completions/bat.zsh | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 assets/completions/bat.zsh diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh new file mode 100644 index 0000000000..35ec298267 --- /dev/null +++ b/assets/completions/bat.zsh @@ -0,0 +1,56 @@ +#compdef bat +# FIXME: help me with the subcommand `cache`, zsh completion is hard as hell + +local -a args +local state + +args=( + '(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' + {-p,--plain}'[Show plain style (alias for `--style=plain`)]:When `-p` is used twice (`-pp`), it also disables automatic paging (alias for `--style=plain --paging=never`)' + '(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]::->language' + '(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:...' + '(--file-name)'--file-name'[Specify the name to display for a file]:...:_files' + '(-d --diff)'--diff'[Only show lines that have been added/removed/modified]' + '(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]: (lines):()' + '(--tabs)'--tabs'[Set the tab width to T spaces]: (tab width):()' + '(--wrap)'--wrap='[Specify the text-wrapping mode]::(auto never character)' + '(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:' + '(-n --number)'{-n,--number}'[Show line numbers]' + '(--color)'--color='[When to use colors]::(auto never always)' + '(--italic-text)'--italic-text='[Use italics in output]::(always never)' + '(--decorations)'--decorations='[When to show the decorations]::(auto never always)' + '(--paging)'--paging='[Specify when to use the pager]::(auto never always)' + '(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:...' + '(--theme)'--theme='[Set the color theme for syntax highlighting]::->theme' + '(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]' + '(--style)'--style='[Comma-separated list of style elements to display]::->style' + '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:...' + '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' + '(: -)'{-h,--help}'[Print this help message]' + '(: -)'{-V,--version}'[Show version information]' + '*: :_files' +) + +_arguments -S -s $args + +case "$state" in + language) + local IFS=$'\n' + local -a languages + languages=( $(bat --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) + + _describe 'language' languages + ;; + + theme) + local IFS=$'\n' + local -a themes + themes=( $(bat --list-themes | sort) ) + + _values 'theme' $themes + ;; + + style) + _values -s , 'style' auto full plain changes header grid numbers snip + ;; +esac From 1a3eb71e2fca6f4e8493945d67f3adada9a6f2a7 Mon Sep 17 00:00:00 2001 From: Kienyew <31496021+Kienyew@users.noreply.github.com> Date: Sat, 8 Aug 2020 14:31:39 +0800 Subject: [PATCH 2/8] Fix character escape problem --- assets/completions/bat.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh index 35ec298267..5e39d2be18 100644 --- a/assets/completions/bat.zsh +++ b/assets/completions/bat.zsh @@ -8,7 +8,7 @@ args=( '(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' {-p,--plain}'[Show plain style (alias for `--style=plain`)]:When `-p` is used twice (`-pp`), it also disables automatic paging (alias for `--style=plain --paging=never`)' '(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]::->language' - '(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:...' + '(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:...' '(--file-name)'--file-name'[Specify the name to display for a file]:...:_files' '(-d --diff)'--diff'[Only show lines that have been added/removed/modified]' '(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]: (lines):()' @@ -20,11 +20,11 @@ args=( '(--italic-text)'--italic-text='[Use italics in output]::(always never)' '(--decorations)'--decorations='[When to show the decorations]::(auto never always)' '(--paging)'--paging='[Specify when to use the pager]::(auto never always)' - '(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:...' + '(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:...' '(--theme)'--theme='[Set the color theme for syntax highlighting]::->theme' '(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]' '(--style)'--style='[Comma-separated list of style elements to display]::->style' - '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:...' + '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:...' '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' '(: -)'{-h,--help}'[Print this help message]' '(: -)'{-V,--version}'[Show version information]' From 32905f78dd60a2a181b3792d73ba8e5701736ddb Mon Sep 17 00:00:00 2001 From: Kienyew Date: Sat, 29 Aug 2020 23:19:42 +0800 Subject: [PATCH 3/8] add completion for 'cache' subcommand --- assets/completions/bat.zsh | 145 +++++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 55 deletions(-) diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh index 5e39d2be18..2dc8e2214e 100644 --- a/assets/completions/bat.zsh +++ b/assets/completions/bat.zsh @@ -1,56 +1,91 @@ #compdef bat -# FIXME: help me with the subcommand `cache`, zsh completion is hard as hell - -local -a args -local state - -args=( - '(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' - {-p,--plain}'[Show plain style (alias for `--style=plain`)]:When `-p` is used twice (`-pp`), it also disables automatic paging (alias for `--style=plain --paging=never`)' - '(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]::->language' - '(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:...' - '(--file-name)'--file-name'[Specify the name to display for a file]:...:_files' - '(-d --diff)'--diff'[Only show lines that have been added/removed/modified]' - '(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]: (lines):()' - '(--tabs)'--tabs'[Set the tab width to T spaces]: (tab width):()' - '(--wrap)'--wrap='[Specify the text-wrapping mode]::(auto never character)' - '(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:' - '(-n --number)'{-n,--number}'[Show line numbers]' - '(--color)'--color='[When to use colors]::(auto never always)' - '(--italic-text)'--italic-text='[Use italics in output]::(always never)' - '(--decorations)'--decorations='[When to show the decorations]::(auto never always)' - '(--paging)'--paging='[Specify when to use the pager]::(auto never always)' - '(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:...' - '(--theme)'--theme='[Set the color theme for syntax highlighting]::->theme' - '(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]' - '(--style)'--style='[Comma-separated list of style elements to display]::->style' - '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:...' - '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' - '(: -)'{-h,--help}'[Print this help message]' - '(: -)'{-V,--version}'[Show version information]' - '*: :_files' -) - -_arguments -S -s $args - -case "$state" in - language) - local IFS=$'\n' - local -a languages - languages=( $(bat --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) - - _describe 'language' languages - ;; - - theme) - local IFS=$'\n' - local -a themes - themes=( $(bat --list-themes | sort) ) - - _values 'theme' $themes - ;; - - style) - _values -s , 'style' auto full plain changes header grid numbers snip - ;; -esac + +local context state state_descr line +typeset -A opt_args + +(( $+functions[_cache_subcommand] )) || +_cache_subcommand() { + local -a args + args=( + '(-b --build -c --clear)'{-b,--build}'[Initialize or update the syntax/theme cache]' + '(-b --build -c --clear)'{-c,--clear}'[Remove the cached syntax definitions and themes]' + '(--source)'--source='[Use a different directory to load syntaxes and themes from]:directory:_files -/' + '(--target)'--target='[Use a different directory to store the cached syntax and theme set]:directory:_files -/' + '(--blank)'--blank'[Create completely new syntax and theme sets)]' + '(: -)'{-h,--help}'[Prints help information]' + '*: :' + ) + + _arguments -S -s $args +} + +(( $+functions[_bat_main] )) || +_bat_main() { + local -a args + args=( + '(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' + {-p,--plain}'[Show plain style (alias for `--style=plain`)]:When `-p` is used twice (`-pp`), it also disables automatic paging (alias for `--style=plain --paging=never`)' + '(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]::->language' + '(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:...' + '(--file-name)'--file-name'[Specify the name to display for a file]:...:_files' + '(-d --diff)'--diff'[Only show lines that have been added/removed/modified]' + '(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]: (lines):()' + '(--tabs)'--tabs'[Set the tab width to T spaces]: (tab width):()' + '(--wrap)'--wrap='[Specify the text-wrapping mode]::(auto never character)' + '(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:' + '(-n --number)'{-n,--number}'[Show line numbers]' + '(--color)'--color='[When to use colors]::(auto never always)' + '(--italic-text)'--italic-text='[Use italics in output]::(always never)' + '(--decorations)'--decorations='[When to show the decorations]::(auto never always)' + '(--paging)'--paging='[Specify when to use the pager]::(auto never always)' + '(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:...' + '(--theme)'--theme='[Set the color theme for syntax highlighting]::->theme' + '(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]' + '(--style)'--style='[Comma-separated list of style elements to display]::->style' + '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:...' + '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' + '(: -)'{-h,--help}'[Print this help message]' + '(: -)'{-V,--version}'[Show version information]' + '*: :_files' + ) + + _arguments -S -s $args + + case "$state" in + language) + local IFS=$'\n' + local -a languages + languages=( $(bat --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) + + _describe 'language' languages + ;; + + theme) + local IFS=$'\n' + local -a themes + themes=( $(bat --list-themes | sort) ) + + _values 'theme' $themes + ;; + + style) + _values -s , 'style' auto full plain changes header grid numbers snip + ;; + esac +} + +# first positional argument +if (( ${#words} == 2 )); then + _values subcommand cache + _bat_main +else + case $words[2] in + cache) + _cache_subcommand + ;; + + *) + _bat_main + ;; + esac +fi From dfa8705260ca91d8e8b083e2eb0e8434dea44a5c Mon Sep 17 00:00:00 2001 From: Kienyew Date: Sun, 30 Aug 2020 16:59:35 +0800 Subject: [PATCH 4/8] use _describe instead of _values on subcommand --- assets/completions/bat.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh index 2dc8e2214e..ecfb5ba29e 100644 --- a/assets/completions/bat.zsh +++ b/assets/completions/bat.zsh @@ -76,7 +76,9 @@ _bat_main() { # first positional argument if (( ${#words} == 2 )); then - _values subcommand cache + local -a subcommands + subcommands=('cache:Modify the syntax-definition and theme cache') + _describe subcommand subcommands _bat_main else case $words[2] in From 62149b1f2e280aab57c11743815bf4871e59ca34 Mon Sep 17 00:00:00 2001 From: Kienyew Date: Mon, 31 Aug 2020 16:45:34 +0800 Subject: [PATCH 5/8] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f604c0a847..bc7708e48a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # unreleased +- Added zsh completion script. see #1136 (@Kienyew) + + ## Features From 55c01d613b29572b8f1930a9a059a2f238a0e7c8 Mon Sep 17 00:00:00 2001 From: Kienyew Date: Fri, 4 Sep 2020 18:53:26 +0800 Subject: [PATCH 6/8] Fix typo --- assets/completions/bat.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh index ecfb5ba29e..592463f914 100644 --- a/assets/completions/bat.zsh +++ b/assets/completions/bat.zsh @@ -11,7 +11,7 @@ _cache_subcommand() { '(-b --build -c --clear)'{-c,--clear}'[Remove the cached syntax definitions and themes]' '(--source)'--source='[Use a different directory to load syntaxes and themes from]:directory:_files -/' '(--target)'--target='[Use a different directory to store the cached syntax and theme set]:directory:_files -/' - '(--blank)'--blank'[Create completely new syntax and theme sets)]' + '(--blank)'--blank'[Create completely new syntax and theme sets]' '(: -)'{-h,--help}'[Prints help information]' '*: :' ) From 5333e2386bcdb07686ceb380e08dee6fd5fcb795 Mon Sep 17 00:00:00 2001 From: Kienyew Date: Fri, 4 Sep 2020 19:39:49 +0800 Subject: [PATCH 7/8] Handle zsh completion when packaging --- .github/workflows/CICD.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 884bb624e6..d65fdcb470 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -283,6 +283,7 @@ jobs: # Autocompletion files cp 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.fish "$ARCHIVE_DIR/autocomplete/${{ env.PROJECT_NAME }}.fish" + cp 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.zsh "$ARCHIVE_DIR/autocomplete/${{ env.PROJECT_NAME }}.zsh" # base compressed package pushd '${{ steps.vars.outputs.STAGING }}/' >/dev/null @@ -306,6 +307,7 @@ jobs: # Autocompletion files install -Dm644 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.fish "${DPKG_DIR}/usr/share/fish/vendor_completions.d/${{ env.PROJECT_NAME }}.fish" + install -Dm644 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.zsh "${DPKG_DIR}/usr/share/zsh/site-functions/_${{ env.PROJECT_NAME }}" # README and LICENSE install -Dm644 "README.md" "${DPKG_DIR}/usr/share/doc/${{ env.PROJECT_NAME }}/README.md" From bb56fc738a2c1b658ccad4a21db1fdcfea03360b Mon Sep 17 00:00:00 2001 From: Kienyew Date: Tue, 8 Sep 2020 18:34:42 +0800 Subject: [PATCH 8/8] Fix packaging and Use parameterized names for zsh completion --- .gitignore | 1 + assets/completions/{bat.zsh => bat.zsh.in} | 14 +++++++------- build.rs | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) rename assets/completions/{bat.zsh => bat.zsh.in} (92%) diff --git a/.gitignore b/.gitignore index 3c788cfd79..ea15e472c5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ # Generated files /assets/completions/bat.fish +/assets/completions/bat.zsh /assets/manual/bat.1 /assets/metadata.yaml diff --git a/assets/completions/bat.zsh b/assets/completions/bat.zsh.in similarity index 92% rename from assets/completions/bat.zsh rename to assets/completions/bat.zsh.in index 592463f914..cef5daaaa9 100644 --- a/assets/completions/bat.zsh +++ b/assets/completions/bat.zsh.in @@ -1,4 +1,4 @@ -#compdef bat +#compdef {{PROJECT_EXECUTABLE}} local context state state_descr line typeset -A opt_args @@ -19,8 +19,8 @@ _cache_subcommand() { _arguments -S -s $args } -(( $+functions[_bat_main] )) || -_bat_main() { +(( $+functions[_main] )) || +_main() { local -a args args=( '(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' @@ -55,7 +55,7 @@ _bat_main() { language) local IFS=$'\n' local -a languages - languages=( $(bat --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) + languages=( $({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) _describe 'language' languages ;; @@ -63,7 +63,7 @@ _bat_main() { theme) local IFS=$'\n' local -a themes - themes=( $(bat --list-themes | sort) ) + themes=( $({{PROJECT_EXECUTABLE}} --list-themes | sort) ) _values 'theme' $themes ;; @@ -79,7 +79,7 @@ if (( ${#words} == 2 )); then local -a subcommands subcommands=('cache:Modify the syntax-definition and theme cache') _describe subcommand subcommands - _bat_main + _main else case $words[2] in cache) @@ -87,7 +87,7 @@ else ;; *) - _bat_main + _main ;; esac fi diff --git a/build.rs b/build.rs index 40c4c01771..a84755f1cd 100644 --- a/build.rs +++ b/build.rs @@ -53,6 +53,11 @@ fn main() -> Result<(), Box> { "assets/completions/bat.fish.in", out_dir.join("assets/completions/bat.fish"), )?; + template( + &variables, + "assets/completions/bat.zsh.in", + out_dir.join("assets/completions/bat.zsh"), + )?; Ok(()) }