Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zsh completion #1136

Merged
merged 8 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

# Generated files
/assets/completions/bat.fish
/assets/completions/bat.zsh
/assets/manual/bat.1
/assets/metadata.yaml
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# unreleased
- Added zsh completion script. see #1136 (@Kienyew)



## Features

Expand Down
93 changes: 93 additions & 0 deletions assets/completions/bat.zsh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#compdef {{PROJECT_EXECUTABLE}}

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[_main] )) ||
_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>:->language'
'(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:<N\:M>...'
'(--file-name)'--file-name'[Specify the name to display for a file]:<name>...:_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`]:<N> (lines):()'
'(--tabs)'--tabs'[Set the tab width to T spaces]:<T> (tab width):()'
'(--wrap)'--wrap='[Specify the text-wrapping mode]:<when>:(auto never character)'
'(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:<width>'
'(-n --number)'{-n,--number}'[Show line numbers]'
'(--color)'--color='[When to use colors]:<when>:(auto never always)'
'(--italic-text)'--italic-text='[Use italics in output]:<when>:(always never)'
'(--decorations)'--decorations='[When to show the decorations]:<when>:(auto never always)'
'(--paging)'--paging='[Specify when to use the pager]:<when>:(auto never always)'
'(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:<glob\:syntax>...'
'(--theme)'--theme='[Set the color theme for syntax highlighting]:<theme>:->theme'
'(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]'
'(--style)'--style='[Comma-separated list of style elements to display]:<components>:->style'
'(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:<N\: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=( $({{PROJECT_EXECUTABLE}} --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=( $({{PROJECT_EXECUTABLE}} --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
local -a subcommands
subcommands=('cache:Modify the syntax-definition and theme cache')
_describe subcommand subcommands
_main
else
case $words[2] in
cache)
_cache_subcommand
;;

*)
_main
;;
esac
fi
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"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(())
}
Expand Down