From 49a99180fed618b62ba773e3aebd8968b044262d Mon Sep 17 00:00:00 2001 From: plustik <70038054+plustik@users.noreply.github.com> Date: Sun, 19 Nov 2023 16:20:52 +0100 Subject: feat: generate completion subcommand (#1561) * generate-completion subcommand * add Makefile target and replace completion scripts in repo with generated output * Add completion to manual --- Cargo.lock | 10 + Cargo.toml | 1 + Makefile | 5 +- etc/completion/completion.bash | 433 +++++++++++++++++++++---- etc/completion/completion.fish | 205 ++++++------ etc/completion/completion.zsh | 145 ++++++++- manual/src/SUMMARY.md | 1 + manual/src/tips-and-tricks/shell-completion.md | 12 + src/cli.rs | 25 +- src/main.rs | 6 +- src/subcommands/generate_completion.rs | 11 + src/subcommands/mod.rs | 1 + 12 files changed, 683 insertions(+), 172 deletions(-) create mode 100644 manual/src/tips-and-tricks/shell-completion.md create mode 100644 src/subcommands/generate_completion.rs diff --git a/Cargo.lock b/Cargo.lock index 1f0374ee..6b47c5d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,6 +302,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bffe91f06a11b4b9420f62103854e90867812cd5d01557f853c5ee8e791b12ae" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.3.12" @@ -559,6 +568,7 @@ dependencies = [ "chrono", "chrono-humanize", "clap", + "clap_complete", "console", "ctrlc", "dirs", diff --git a/Cargo.toml b/Cargo.toml index b54ed5d1..5aa04339 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ unicode-segmentation = "1.10.1" unicode-width = "0.1.10" vte = "0.11.0" xdg = "2.4.1" +clap_complete = "4.4.4" [dependencies.git2] version = "0.17.2" diff --git a/Makefile b/Makefile index 61764277..76be99c6 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,9 @@ end-to-end-test: build ./tests/test_deprecated_options > /dev/null ./tests/test_navigate_less_history_file +shell-completion: + for shell in bash fish zsh; do ./target/release/delta --generate-completion $$shell > etc/completion/completion.$$shell; done + release: @make -f release.Makefile release @@ -44,4 +47,4 @@ flamegraph: build chronologer: chronologer etc/performance/chronologer.yaml -.PHONY: build format lint test unit-test end-to-end-test release version hash benchmark flamegraph chronologer +.PHONY: build format lint test unit-test end-to-end-test release shell-completion version hash benchmark flamegraph chronologer diff --git a/etc/completion/completion.bash b/etc/completion/completion.bash index 03557fbe..5c7de994 100644 --- a/etc/completion/completion.bash +++ b/etc/completion/completion.bash @@ -1,69 +1,378 @@ -#!/bin/bash - -__delta_previous_extglob_setting=$(shopt -p extglob) -shopt -s extglob - -__delta_complete_commands() { - COMPREPLY=( $(compgen -W "${commands[*]}" -- "$cur") ) -} - -_delta_delta() { - __delta_complete_commands -} - _delta() { - local previous_extglob_setting=$(shopt -p extglob) - shopt -s extglob - - local commands=( - --raw - --commit-style - --dark - --file-style - --help - --highlight-removed - --hunk-style - --light - --list-languages - --list-themes - --list-theme-names - --minus-color - --minus-emph-color - --plus-color - --plus-emph-color - --keep-plus-minus-markers - --show-background-colors - --theme - --version - --width - ) - + local i cur prev opts cmd COMPREPLY=() - local cur prev words cword - _get_comp_words_by_ref -n : cur prev words cword + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + cmd="" + opts="" - local command='delta' command_pos=0 - local counter=1 - while [ $counter -lt $cword ]; do - case "${words[$counter]}" in - *) - command="${words[$counter]}" - command_pos=$counter - break - ;; - esac - (( counter++ )) + for i in ${COMP_WORDS[@]} + do + case "${cmd},${i}" in + ",$1") + cmd="delta" + ;; + *) + ;; + esac done - local completions_func=_delta_${command} - - declare -F $completions_func >/dev/null && $completions_func - - eval "$previous_extglob_setting" - return 0 + case "${cmd}" in + delta) + opts="-n -s -w -h -V --blame-code-style --blame-format --blame-palette --blame-separator-format --blame-separator-style --blame-timestamp-format --blame-timestamp-output-format --color-only --config --commit-decoration-style --commit-regex --commit-style --dark --default-language --diff-highlight --diff-so-fancy --diff-stat-align-width --features --file-added-label --file-copied-label --file-decoration-style --file-modified-label --file-removed-label --file-renamed-label --file-style --file-transformation --generate-completion --grep-context-line-style --grep-file-style --grep-header-decoration-style --grep-header-file-style --grep-line-number-style --grep-output-type --grep-match-line-style --grep-match-word-style --grep-separator-symbol --hunk-header-decoration-style --hunk-header-file-style --hunk-header-line-number-style --hunk-header-style --hunk-label --hyperlinks --hyperlinks-commit-link-format --hyperlinks-file-link-format --inline-hint-style --inspect-raw-lines --keep-plus-minus-markers --light --line-buffer-size --line-fill-method --line-numbers --line-numbers-left-format --line-numbers-left-style --line-numbers-minus-style --line-numbers-plus-style --line-numbers-right-format --line-numbers-right-style --line-numbers-zero-style --list-languages --list-syntax-themes --map-styles --max-line-distance --max-line-length --merge-conflict-begin-symbol --merge-conflict-end-symbol --merge-conflict-ours-diff-header-decoration-style --merge-conflict-ours-diff-header-style --merge-conflict-theirs-diff-header-decoration-style --merge-conflict-theirs-diff-header-style --minus-empty-line-marker-style --minus-emph-style --minus-non-emph-style --minus-style --navigate --navigate-regex --no-gitconfig --pager --paging --parse-ansi --plus-emph-style --plus-empty-line-marker-style --plus-non-emph-style --plus-style --raw --relative-paths --right-arrow --show-colors --show-config --show-syntax-themes --show-themes --side-by-side --syntax-theme --tabs --true-color --whitespace-error-style --width --word-diff-regex --wrap-left-symbol --wrap-max-lines --wrap-right-percent --wrap-right-prefix-symbol --wrap-right-symbol --zero-style --24-bit-color --help --version [MINUS_FILE] [PLUS_FILE]" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + --blame-code-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-palette) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-separator-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-separator-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-timestamp-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --blame-timestamp-output-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --config) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --commit-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --commit-regex) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --commit-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --default-language) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --diff-stat-align-width) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --features) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-added-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-copied-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-modified-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-removed-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-renamed-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --file-transformation) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --generate-completion) + COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}")) + return 0 + ;; + --grep-context-line-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-file-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-header-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-header-file-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-line-number-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-output-type) + COMPREPLY=($(compgen -W "ripgrep classic" -- "${cur}")) + return 0 + ;; + --grep-match-line-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-match-word-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --grep-separator-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hunk-header-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hunk-header-file-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hunk-header-line-number-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hunk-header-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hunk-label) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hyperlinks-commit-link-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --hyperlinks-file-link-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --inline-hint-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --inspect-raw-lines) + COMPREPLY=($(compgen -W "true false" -- "${cur}")) + return 0 + ;; + --line-buffer-size) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-fill-method) + COMPREPLY=($(compgen -W "ansi spaces" -- "${cur}")) + return 0 + ;; + --line-numbers-left-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-left-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-minus-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-plus-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-right-format) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-right-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --line-numbers-zero-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --map-styles) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --max-line-distance) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --max-line-length) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-begin-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-end-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-ours-diff-header-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-ours-diff-header-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-theirs-diff-header-decoration-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --merge-conflict-theirs-diff-header-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --minus-empty-line-marker-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --minus-emph-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --minus-non-emph-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --minus-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --navigate-regex) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --pager) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --paging) + COMPREPLY=($(compgen -W "auto always never" -- "${cur}")) + return 0 + ;; + --plus-emph-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --plus-empty-line-marker-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --plus-non-emph-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --plus-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --right-arrow) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --syntax-theme) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --tabs) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --true-color) + COMPREPLY=($(compgen -W "auto always never" -- "${cur}")) + return 0 + ;; + --whitespace-error-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --width) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -w) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --word-diff-regex) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --wrap-left-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --wrap-max-lines) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --wrap-right-percent) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --wrap-right-prefix-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --wrap-right-symbol) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --zero-style) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --24-bit-color) + COMPREPLY=($(compgen -W "auto always never" -- "${cur}")) + return 0 + ;; + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; + esac } -eval "$__delta_previous_extglob_setting" -unset __delta_previous_extglob_setting - -complete -F _delta -A file delta +complete -F _delta -o nosort -o bashdefault -o default delta diff --git a/etc/completion/completion.fish b/etc/completion/completion.fish index 7bea07ed..760f54b2 100644 --- a/etc/completion/completion.fish +++ b/etc/completion/completion.fish @@ -1,99 +1,106 @@ -complete -c delta -l blame-code-style -x -d "Style string for the code section of a git blame line" -complete -c delta -l blame-format -x -d "Format string for git blame commit metadata" -complete -c delta -l blame-palette -x -d "Background colors used for git blame lines" -complete -c delta -l blame-separator-format -x -d "Separator between the blame format and the code section of a git blame line" -complete -c delta -l blame-separator-style -x -d "Style string for the blame-separator-format" -complete -c delta -l blame-timestamp-format -x -d "Format of git blame timestamp in raw git output received by delta" -complete -c delta -l color-only -d "Do not alter the input structurally in any way" -complete -c delta -l commit-decoration-style -x -d "Style string for the commit hash decoration" -complete -c delta -l commit-regex -x -d "Regular expression used to identify the commit line when parsing git output" -complete -c delta -l commit-style -x -d "Style string for the commit hash line" -complete -c delta -l dark -d "Use default colors appropriate for a dark terminal background" -complete -c delta -l default-language -x -d "Default language used for syntax highlighting" -complete -c delta -l diff-highlight -d "Emulate diff-highlight" -complete -c delta -l diff-so-fancy -d "Emulate diff-so-fancy" -complete -c delta -l diff-stat-align-width -x -d "Width allocated for file paths in a diff stat section" -complete -c delta -l features -x -d "Names of delta features to activate" -complete -c delta -l file-added-label -x -d "Text to display before an added file path" -complete -c delta -l file-copied-label -x -d "Text to display before a copied file path" -complete -c delta -l file-decoration-style -x -d "Style string for the file decoration" -complete -c delta -l file-modified-label -x -d "Text to display before a modified file path" -complete -c delta -l file-removed-label -x -d "Text to display before a removed file path" -complete -c delta -l file-renamed-label -x -d "Text to display before a renamed file path" -complete -c delta -l file-style -x -d "Style string for the file section" -complete -c delta -l file-transformation -x -d "Sed-style command transforming file paths for display" -complete -c delta -l grep-context-line-style -x -d "Style string for non-matching lines of grep output" -complete -c delta -l grep-file-style -x -d "Style string for file paths in grep output" -complete -c delta -l grep-line-number-style -x -d "Style string for line numbers in grep output" -complete -c delta -l grep-match-line-style -x -d "Style string for matching lines of grep output" -complete -c delta -l grep-match-word-style -x -d "Style string for the matching substrings within a matching line of grep output" -complete -c delta -l grep-separator-symbol -x -d "Separator symbol printed after the file path and line number in grep output" -complete -c delta -l hunk-header-decoration-style -x -d "Style string for the hunk-header decoration" -complete -c delta -l hunk-header-file-style -x -d "Style string for the file path part of the hunk-header" -complete -c delta -l hunk-header-line-number-style -x -d "Style string for the line number part of the hunk-header" -complete -c delta -l hunk-header-style -x -d "Style string for the hunk-header" -complete -c delta -l hunk-label -x -d "Text to display before a hunk header" -complete -c delta -l hyperlinks -d "Render commit hashes, file names, and line numbers as hyperlinks" -complete -c delta -l hyperlinks-commit-link-format -x -d "Format string for commit hyperlinks" -complete -c delta -l hyperlinks-file-link-format -x -d "Format string for file hyperlinks" -complete -c delta -l inline-hint-style -x -d "Style string for short inline hint text" -complete -c delta -l inspect-raw-lines -xa "true false" -d "Kill-switch for --color-moved support" -complete -c delta -l keep-plus-minus-markers -d "Prefix added/removed lines with a +/- character" -complete -c delta -l light -d "Use default colors appropriate for a light terminal background" -complete -c delta -l line-buffer-size -x -d "Size of internal line buffer" -complete -c delta -l line-fill-method -xa "ansi spaces" -d "Line-fill method in side-by-side mode" -complete -c delta -l line-numbers -s n -d "Display line numbers next to the diff" -complete -c delta -l line-numbers-left-format -x -d "Format string for the left column of line numbers" -complete -c delta -l line-numbers-left-style -x -d "Style string for the left column of line numbers" -complete -c delta -l line-numbers-minus-style -x -d "Style string for line numbers in the old (minus) version of the file" -complete -c delta -l line-numbers-plus-style -x -d "Style string for line numbers in the new (plus) version of the file" -complete -c delta -l line-numbers-right-format -x -d "Format string for the right column of line numbers" -complete -c delta -l line-numbers-right-style -x -d "Style string for the right column of line numbers" -complete -c delta -l line-numbers-zero-style -x -d "Style string for line numbers in unchanged (zero) lines" -complete -c delta -l list-languages -d "List supported languages and associated file extensions" -complete -c delta -l list-syntax-themes -d "List available syntax-highlighting color themes" -complete -c delta -l map-styles -x -d "Map styles encountered in raw input to desired output styles" -complete -c delta -l max-line-distance -x -d "Maximum line pair distance parameter in within-line diff algorithm" -complete -c delta -l max-line-length -x -d "Truncate lines longer than this" -complete -c delta -l merge-conflict-begin-symbol -x -d "String marking the beginning of a merge conflict region" -complete -c delta -l merge-conflict-end-symbol -x -d "String marking the end of a merge conflict region" -complete -c delta -l merge-conflict-ours-diff-header-decoration-style -x -d "Style string for the decoration of the header above the 'ours' merge conflict diff" -complete -c delta -l merge-conflict-ours-diff-header-style -x -d "Style string for the header above the 'ours' branch merge conflict diff" -complete -c delta -l merge-conflict-theirs-diff-header-decoration-style -x -d "Style string for the decoration of the header above the 'theirs' merge conflict diff" -complete -c delta -l merge-conflict-theirs-diff-header-style -x -d "Style string for the header above the 'theirs' branch merge conflict diff" -complete -c delta -l minus-empty-line-marker-style -x -d "Style string for removed empty line marker" -complete -c delta -l minus-emph-style -x -d "Style string for emphasized sections of removed lines" -complete -c delta -l minus-non-emph-style -x -d "Style string for non-emphasized sections of removed lines that have an emphasized section" -complete -c delta -l minus-style -x -d "Style string for removed lines" -complete -c delta -l navigate -d "Activate diff navigation" -complete -c delta -l navigate-regex -x -d "Regular expression defining navigation stop points" -complete -c delta -l no-gitconfig -d "Do not read any settings from git config" -complete -c delta -l pager -x -d "Which pager to use" -complete -c delta -l paging -xa "auto always never" -d "Whether to use a pager when displaying output" -complete -c delta -l parse-ansi -d "Display ANSI color escape sequences in human-readable form" -complete -c delta -l plus-emph-style -x -d "Style string for emphasized sections of added lines" -complete -c delta -l plus-empty-line-marker-style -x -d "Style string for added empty line marker" -complete -c delta -l plus-non-emph-style -x -d "Style string for non-emphasized sections of added lines that have an emphasized section" -complete -c delta -l plus-style -x -d "Style string for added lines" -complete -c delta -l raw -d "Do not alter the input in any way" -complete -c delta -l relative-paths -d "Output all file paths relative to the current directory" -complete -c delta -l right-arrow -x -d "Text to display with a changed file path" -complete -c delta -l show-colors -d "Show available named colors" -complete -c delta -l show-config -d "Display the active values for all Delta options" -complete -c delta -l show-syntax-themes -d "Show example diff for available syntax-highlighting themes" -complete -c delta -l show-themes -d "Show example diff for available delta themes" -complete -c delta -l side-by-side -s s -d "Display diffs in side-by-side layout" -complete -c delta -l syntax-theme -xa "(delta --list-syntax-themes | cut -f 2)" -d "The syntax-highlighting theme to use" -complete -c delta -l tabs -x -d "The number of spaces to replace tab characters with" -complete -c delta -l true-color -xa "auto always never" -d "Whether to emit 24-bit RGB color codes" -complete -c delta -l whitespace-error-style -x -d "Style string for whitespace errors" -complete -c delta -l width -s w -x -d "The width of underline/overline decorations" -complete -c delta -l word-diff-regex -x -d "Regular expression defining a 'word' in within-line diff algorithm" -complete -c delta -l wrap-left-symbol -x -d "End-of-line wrapped content symbol" -complete -c delta -l wrap-max-lines -x -d "How often a line should be wrapped if it does not fit" -complete -c delta -l wrap-right-percent -x -d "Threshold for right-aligning wrapped content" -complete -c delta -l wrap-right-prefix-symbol -x -d "Pre-wrapped content symbol" -complete -c delta -l wrap-right-symbol -x -d "End-of-line wrapped content symbol" -complete -c delta -l zero-style -x -d "Style string for unchanged lines" -complete -c delta -l help -s h -d "Print help information" -complete -c delta -l version -s V -d "Print version information" +complete -c delta -l blame-code-style -d 'Style string for the code section of a git blame line' -r +complete -c delta -l blame-format -d 'Format string for git blame commit metadata' -r +complete -c delta -l blame-palette -d 'Background colors used for git blame lines (space-separated string)' -r +complete -c delta -l blame-separator-format -d 'Separator between the blame format and the code section of a git blame line' -r +complete -c delta -l blame-separator-style -d 'Style string for the blame-separator-format' -r +complete -c delta -l blame-timestamp-format -d 'Format of `git blame` timestamp in raw git output received by delta' -r +complete -c delta -l blame-timestamp-output-format -d 'Format string for git blame timestamp output' -r +complete -c delta -l config -d 'Load the config file at PATH instead of ~/.gitconfig' -r -F +complete -c delta -l commit-decoration-style -d 'Style string for the commit hash decoration' -r +complete -c delta -l commit-regex -d 'Regular expression used to identify the commit line when parsing git output' -r +complete -c delta -l commit-style -d 'Style string for the commit hash line' -r +complete -c delta -l default-language -d 'Default language used for syntax highlighting' -r +complete -c delta -l diff-stat-align-width -d 'Width allocated for file paths in a diff stat section' -r +complete -c delta -l features -d 'Names of delta features to activate (space-separated)' -r +complete -c delta -l file-added-label -d 'Text to display before an added file path' -r +complete -c delta -l file-copied-label -d 'Text to display before a copied file path' -r +complete -c delta -l file-decoration-style -d 'Style string for the file decoration' -r +complete -c delta -l file-modified-label -d 'Text to display before a modified file path' -r +complete -c delta -l file-removed-label -d 'Text to display before a removed file path' -r +complete -c delta -l file-renamed-label -d 'Text to display before a renamed file path' -r +complete -c delta -l file-style -d 'Style string for the file section' -r +complete -c delta -l file-transformation -d 'Sed-style command transforming file paths for display' -r +complete -c delta -l generate-completion -d 'Print completion file for the given shell' -r -f -a "{bash '',elvish '',fish '',powershell '',zsh ''}" +complete -c delta -l grep-context-line-style -d 'Style string for non-matching lines of grep output' -r +complete -c delta -l grep-file-style -d 'Style string for file paths in grep output' -r +complete -c delta -l grep-header-decoration-style -d 'Style string for the header decoration in grep output' -r +complete -c delta -l grep-header-file-style -d 'Style string for the file path part of the header in grep output' -r +complete -c delta -l grep-line-number-style -d 'Style string for line numbers in grep output' -r +complete -c delta -l grep-output-type -d 'Grep output format. Possible values: "ripgrep" - file name printed once, followed by matching lines within that file, each preceded by a line number. "classic" - file name:line number, followed by matching line. Default is "ripgrep" if `rg --json` format is detected, otherwise "classic"' -r -f -a "{ripgrep '',classic ''}" +complete -c delta -l grep-match-line-style -d 'Style string for matching lines of grep output' -r +complete -c delta -l grep-match-word-style -d 'Style string for the matching substrings within a matching line of grep output' -r +complete -c delta -l grep-separator-symbol -d 'Separator symbol printed after the file path and line number in grep output' -r +complete -c delta -l hunk-header-decoration-style -d 'Style string for the hunk-header decoration' -r +complete -c delta -l hunk-header-file-style -d 'Style string for the file path part of the hunk-header' -r +complete -c delta -l hunk-header-line-number-style -d 'Style string for the line number part of the hunk-header' -r +complete -c delta -l hunk-header-style -d 'Style string for the hunk-header' -r +complete -c delta -l hunk-label -d 'Text to display before a hunk header' -r +complete -c delta -l hyperlinks-commit-link-format -d 'Format string for commit hyperlinks (requires --hyperlinks)' -r +complete -c delta -l hyperlinks-file-link-format -d 'Format string for file hyperlinks (requires --hyperlinks)' -r +complete -c delta -l inline-hint-style -d 'Style string for short inline hint text' -r +complete -c delta -l inspect-raw-lines -d 'Kill-switch for --color-moved support' -r -f -a "{true '',false ''}" +complete -c delta -l line-buffer-size -d 'Size of internal line buffer' -r +complete -c delta -l line-fill-method -d 'Line-fill method in side-by-side mode' -r -f -a "{ansi spaces ''}" +complete -c delta -l line-numbers-left-format -d 'Format string for the left column of line numbers' -r +complete -c delta -l line-numbers-left-style -d 'Style string for the left column of line numbers' -r +complete -c delta -l line-numbers-minus-style -d 'Style string for line numbers in the old (minus) version of the file' -r +complete -c delta -l line-numbers-plus-style -d 'Style string for line numbers in the new (plus) version of the file' -r +complete -c delta -l line-numbers-right-format -d 'Format string for the right column of line numbers' -r +complete -c delta -l line-numbers-right-style -d 'Style string for the right column of line numbers' -r +complete -c delta -l line-numbers-zero-style -d 'Style string for line numbers in unchanged (zero) lines' -r +complete -c delta -l map-styles -d 'Map styles encountered in raw input to desired output styles' -r +complete -c delta -l max-line-distance -d 'Maximum line pair distance parameter in within-line diff algorithm' -r +complete -c delta -l max-line-length -d 'Truncate lines longer than this' -r +complete -c delta -l merge-conflict-begin-symbol -d 'String marking the beginning of a merge conflict region' -r +complete -c delta -l merge-conflict-end-symbol -d 'String marking the end of a merge conflict region' -r +complete -c delta -l merge-conflict-ours-diff-header-decoration-style -d 'Style string for the decoration of the header above the \'ours\' merge conflict diff' -r +complete -c delta -l merge-conflict-ours-diff-header-style -d 'Style string for the header above the \'ours\' branch merge conflict diff' -r +complete -c delta -l merge-conflict-theirs-diff-header-decoration-style -d 'Style string for the decoration of the header above the \'theirs\' merge conflict diff' -r +complete -c delta -l merge-conflict-theirs-diff-header-style -d 'Style string for the header above the \'theirs\' branch merge conflict diff' -r +complete -c delta -l minus-empty-line-marker-style -d 'Style string for removed empty line marker' -r +complete -c delta -l minus-emph-style -d 'Style string for emphasized sections of removed lines' -r +complete -c delta -l minus-non-emph-style -d 'Style string for non-emphasized sections of removed lines that have an emphasized section' -r +complete -c delta -l minus-style -d 'Style string for removed lines' -r +complete -c delta -l navigate-regex -d 'Regular expression defining navigation stop points' -r +complete -c delta -l pager -d 'Which pager to use' -r +complete -c delta -l paging -d 'Whether to use a pager when displaying output' -r -f -a "{auto '',always '',never ''}" +complete -c delta -l plus-emph-style -d 'Style string for emphasized sections of added lines' -r +complete -c delta -l plus-empty-line-marker-style -d 'Style string for added empty line marker' -r +complete -c delta -l plus-non-emph-style -d 'Style string for non-emphasized sections of added lines that have an emphasized section' -r +complete -c delta -l plus-style -d 'Style string for added lines' -r +complete -c delta -l right-arrow -d 'Text to display with a changed file path' -r +complete -c delta -l syntax-theme -d 'The syntax-highlighting theme to use' -r +complete -c delta -l tabs -d 'The number of spaces to replace tab characters with' -r +complete -c delta -l true-color -d 'Whether to emit 24-bit ("true color") RGB color codes' -r -f -a "{auto '',always '',never ''}" +complete -c delta -l whitespace-error-style -d 'Style string for whitespace errors' -r +complete -c delta -s w -l width -d 'The width of underline/overline decorations' -r +complete -c delta -l word-diff-regex -d 'Regular expression defining a \'word\' in within-line diff algorithm' -r +complete -c delta -l wrap-left-symbol -d 'End-of-line wrapped content symbol (left-aligned)' -r +complete -c delta -l wrap-max-lines -d 'How often a line should be wrapped if it does not fit' -r +complete -c delta -l wrap-right-percent -d 'Threshold for right-aligning wrapped content' -r +complete -c delta -l wrap-right-prefix-symbol -d 'Pre-wrapped content symbol (right-aligned)' -r +complete -c delta -l wrap-right-symbol -d 'End-of-line wrapped content symbol (right-aligned)' -r +complete -c delta -l zero-style -d 'Style string for unchanged lines' -r +complete -c delta -l 24-bit-color -d 'Deprecated: use --true-color' -r -f -a "{auto '',always '',never ''}" +complete -c delta -l color-only -d 'Do not alter the input structurally in any way' +complete -c delta -l dark -d 'Use default colors appropriate for a dark terminal background' +complete -c delta -l diff-highlight -d 'Emulate diff-highlight' +complete -c delta -l diff-so-fancy -d 'Emulate diff-so-fancy' +complete -c delta -l hyperlinks -d 'Render commit hashes, file names, and line numbers as hyperlinks' +complete -c delta -l keep-plus-minus-markers -d 'Prefix added/removed lines with a +/- character, as git does' +complete -c delta -l light -d 'Use default colors appropriate for a light terminal background' +complete -c delta -s n -l line-numbers -d 'Display line numbers next to the diff' +complete -c delta -l list-languages -d 'List supported languages and associated file extensions' +complete -c delta -l list-syntax-themes -d 'List available syntax-highlighting color themes' +complete -c delta -l navigate -d 'Activate diff navigation' +complete -c delta -l no-gitconfig -d 'Do not read any settings from git config' +complete -c delta -l parse-ansi -d 'Display ANSI color escape sequences in human-readable form' +complete -c delta -l raw -d 'Do not alter the input in any way' +complete -c delta -l relative-paths -d 'Output all file paths relative to the current directory' +complete -c delta -l show-colors -d 'Show available named colors' +complete -c delta -l show-config -d 'Display the active values for all Delta options' +complete -c delta -l show-syntax-themes -d 'Show example diff for available syntax-highlighting themes' +complete -c delta -l show-themes -d 'Show example diff for available delta themes' +complete -c delta -s s -l side-by-side -d 'Display diffs in side-by-side layout' +complete -c delta -s h -l help -d 'Print help (see more with \'--help\')' +complete -c delta -s V -l version -d 'Print version' diff --git a/etc/completion/completion.zsh b/etc/completion/completion.zsh index 0ff59208..a62077ed 100644 --- a/etc/completion/completion.zsh +++ b/etc/completion/completion.zsh @@ -1 +1,144 @@ -compdef _gnu_generic delta +#compdef delta + +autoload -U is-at-least + +_delta() { + typeset -A opt_args + typeset -a _arguments_options + local ret=1 + + if is-at-least 5.2; then + _arguments_options=(-s -S -C) + else + _arguments_options=(-s -C) + fi + + local context curcontext="$curcontext" state line + _arguments "${_arguments_options[@]}" \ +'--blame-code-style=[Style string for the code section of a git blame line]:STYLE: ' \ +'--blame-format=[Format string for git blame commit metadata]:FMT: ' \ +'--blame-palette=[Background colors used for git blame lines (space-separated string)]:COLORS: ' \ +'--blame-separator-format=[Separator between the blame format and the code section of a git blame line]:FMT: ' \ +'--blame-separator-style=[Style string for the blame-separator-format]:STYLE: ' \ +'--blame-timestamp-format=[Format of \`git blame\` timestamp in raw git output received by delta]:FMT: ' \ +'--blame-timestamp-output-format=[Format string for git blame timestamp output]:FMT: ' \ +'--config=[Load the config file at PATH instead of ~/.gitconfig]:PATH:_files' \ +'--commit-decoration-style=[Style string for the commit hash decoration]:STYLE: ' \ +'--commit-regex=[Regular expression used to identify the commit line when parsing git output]:REGEX: ' \ +'--commit-style=[Style string for the commit hash line]:STYLE: ' \ +'--default-language=[Default language used for syntax highlighting]:LANG: ' \ +'--diff-stat-align-width=[Width allocated for file paths in a diff stat section]:N: ' \ +'--features=[Names of delta features to activate (space-separated)]:FEATURES: ' \ +'--file-added-label=[Text to display before an added file path]:STRING: ' \ +'--file-copied-label=[Text to display before a copied file path]:STRING: ' \ +'--file-decoration-style=[Style string for the file decoration]:STYLE: ' \ +'--file-modified-label=[Text to display before a modified file path]:STRING: ' \ +'--file-removed-label=[Text to display before a removed file path]:STRING: ' \ +'--file-renamed-label=[Text to display before a renamed file path]:STRING: ' \ +'--file-style=[Style string for the file section]:STYLE: ' \ +'--file-transformation=[Sed-style command transforming file paths for display]:SED_CMD: ' \ +'--generate-completion=[Print completion file for the given shell]:GENERATE_COMPLETION:(bash elvish fish powershell zsh)' \ +'--grep-context-line-style=[Style string for non-matching lines of grep output]:STYLE: ' \ +'--grep-file-style=[Style string for file paths in grep output]:STYLE: ' \ +'--grep-header-decoration-style=[Style string for the header decoration in grep output]:STYLE: ' \ +'--grep-header-file-style=[Style string for the file path part of the header in grep output]:STYLE: ' \ +'--grep-line-number-style=[Style string for line numbers in grep output]:STYLE: ' \ +'--grep-output-type=[Grep output format. Possible values\: "ripgrep" - file name printed once, followed by matching lines within that file, each preceded by a line number. "classic" - file name\:line number, followed by matching line. Default is "ripgrep" if \`rg --json\` format is detected, otherwise "classic"]:OUTPUT_TYPE:(ripgrep classic)' \ +'--grep-match-line-style=[Style string for matching lines of grep output]:STYLE: ' \ +'--grep-match-word-style=[Style string for the matching substrings within a matching line of grep output]:STYLE: ' \ +'--grep-separator-symbol=[Separator symbol printed after the file path and line number in grep output]:STRING: ' \ +'--hunk-header-decoration-style=[Style string for the hunk-header decoration]:STYLE: ' \ +'--hunk-header-file-style=[Style string for the file path part of the hunk-header]:STYLE: ' \ +'--hunk-header-line-number-style=[Style string for the line number part of the hunk-header]:STYLE: ' \ +'--hunk-header-style=[Style string for the hunk-header]:STYLE: ' \ +'--hunk-label=[Text to display before a hunk header]:STRING: ' \ +'--hyperlinks-commit-link-format=[Format string for commit hyperlinks (requires --hyperlinks)]:FMT: ' \ +'--hyperlinks-file-link-format=[Format string for file hyperlinks (requires --hyperlinks)]:FMT: ' \ +'--inline-hint-style=[Style string for short inline hint text]:STYLE: ' \ +'--inspect-raw-lines=[Kill-switch for --color-moved support]:true|false:(true false)' \ +'--line-buffer-size=[Size of internal line buffer]:N: ' \ +'--line-fill-method=[Line-fill method in side-by-side mode]:STRING:(ansi spaces)' \ +'--line-numbers-left-format=[Format string for the left column of line numbers]:FMT: ' \ +'--line-numbers-left-style=[Style string for the left column of line numbers]:STYLE: ' \ +'--line-numbers-minus-style=[Style string for line numbers in the old (minus) version of the file]:STYLE: ' \ +'--line-numbers-plus-style=[Style string for line numbers in the new (plus) version of the file]:STYLE: ' \ +'--line-numbers-right-format=[Format string for the right column of line numbers]:FMT: ' \ +'--line-numbers-right-style=[Style string for the right column of line numbers]:STYLE: ' \ +'--line-numbers-zero-style=[Style string for line numbers in unchanged (zero) lines]:STYLE: ' \ +'--map-styles=[Map styles encountered in raw input to desired output styles]:STYLES_MAP: ' \ +'--max-line-distance=[Maximum line pair distance parameter in within-line diff algorithm]:DIST: ' \ +'--max-line-length=[Truncate lines longer than this]:N: ' \ +'--merge-conflict-begin-symbol=[String marking the beginning of a merge conflict region]:STRING: ' \ +'--merge-conflict-end-symbol=[String marking the end of a merge conflict region]:STRING: ' \ +'--merge-conflict-ours-diff-header-decoration-style=[Style string for the decoration of the header above the '\''ours'\'' merge conflict diff]:STYLE: ' \ +'--merge-conflict-ours-diff-header-style=[Style string for the header above the '\''ours'\'' branch merge conflict diff]:STYLE: ' \ +'--merge-conflict-theirs-diff-header-decoration-style=[Style string for the decoration of the header above the '\''theirs'\'' merge conflict diff]:STYLE: ' \ +'--merge-conflict-theirs-diff-header-style=[Style string for the header above the '\''theirs'\'' branch merge conflict diff]:STYLE: ' \ +'--minus-empty-line-marker-style=[Style string for removed empty line marker]:STYLE: ' \ +'--minus-emph-style=[Style string for emphasized sections of removed lines]:STYLE: ' \ +'--minus-non-emph-style=[Style string for non-emphasized sections of removed lines that have an emphasized section]:STYLE: ' \ +'--minus-style=[Style string for removed lines]:STYLE: ' \ +'--navigate-regex=[Regular expression defining navigation stop points]:REGEX: ' \ +'--pager=[Which pager to use]:CMD: ' \ +'--paging=[Whether to use a pager when displaying output]:auto|always|never:(auto always never)' \ +'--plus-emph-style=[Style string for emphasized sections of added lines]:STYLE: ' \ +'--plus-empty-line-marker-style=[Style string for added empty line marker]:STYLE: ' \ +'--plus-non-emph-style=[Style string for non-emphasized sections of added lines that have an emphasized section]:STYLE: ' \ +'--plus-style=[Style string for added lines]:STYLE: ' \ +'--right-arrow=[Text to display with a changed file path]:STRING: ' \ +'--syntax-theme=[The syntax-highlighting theme to use]:SYNTAX_THEME: ' \ +'--tabs=[The number of spaces to replace tab characters with]:N: ' \ +'--true-color=[Whether to emit 24-bit ("true color") RGB color codes]:auto|always|never:(auto always never)' \ +'--whitespace-error-style=[Style string for whitespace errors]:STYLE: ' \ +'-w+[The width of underline/overline decorations]:N: ' \ +'--width=[The width of underline/overline decorations]:N: ' \ +'--word-diff-regex=[Regular expression defining a '\''word'\'' in within-line diff algorithm]:REGEX: ' \ +'--wrap-left-symbol=[End-of-line wrapped content symbol (left-aligned)]:STRING: ' \ +'--wrap-max-lines=[How often a line should be wrapped if it does not fit]:N: ' \ +'--wrap-right-percent=[Threshold for right-aligning wrapped content]:PERCENT: ' \ +'--wrap-right-prefix-symbol=[Pre-wrapped content symbol (right-aligned)]:STRING: ' \ +'--wrap-right-symbol=[End-of-line wrapped content symbol (right-aligned)]:STRING: ' \ +'--zero-style=[Style string for unchanged lines]:STYLE: ' \ +'--24-bit-color=[Deprecated\: use --true-color]:auto|always|never:(auto always never)' \ +'--color-only[Do not alter the input structurally in any way]' \ +'--dark[Use default colors appropriate for a dark terminal background]' \ +'--diff-highlight[Emulate diff-highlight]' \ +'--diff-so-fancy[Emulate diff-so-fancy]' \ +'--hyperlinks[Render commit hashes, file names, and line numbers as hyperlinks]' \ +'--keep-plus-minus-markers[Prefix added/removed lines with a +/- character, as git does]' \ +'--light[Use default colors appropriate for a light terminal background]' \ +'-n[Display line numbers next to the diff]' \ +'--line-numbers[Display line numbers next to the diff]' \ +'--list-languages[List supported languages and associated file extensions]' \ +'--list-syntax-themes[List available syntax-highlighting color themes]' \ +'--navigate[Activate diff navigation]' \ +'--no-gitconfig[Do not read any settings from git config]' \ +'--parse-ansi[Display ANSI color escape sequences in human-readable form]' \ +'--raw[Do not alter the input in any way]' \ +'--relative-paths[Output all file paths relative to the current directory]' \ +'--show-colors[Show available named colors]' \ +'--show-config[Display the active values for all Delta options]' \ +'--show-syntax-themes[Show example diff for available syntax-highlighting themes]' \ +'--show-themes[Show example diff for available delta themes]' \ +'-s[Display diffs in side-by-side layout]' \ +'--side-by-side[Display diffs in side-by-side layout]' \ +'-h[Print help (see more with '\''--help'\'')]' \ +'--help[Print help (see more with '\''--help'\'')]' \ +'-V[Print version]' \ +'--version[Print version]' \ +'::minus_file -- First file to be compared when delta is being used in diff mode:_files' \ +'::plus_file -- Second file to be compared when delta is being used in diff mode:_files' \ +&& ret=0 +} + +(( $+functions[_delta_commands] )) || +_delta_commands() { + local commands; commands=() + _describe -t commands 'delta commands' commands "$@" +} + +if [ "$funcstack[1]" = "_delta" ]; then + _delta "$@" +else + compdef _delta delta +fi diff --git a/manual/src/SUMMARY.md b/manual/src/SUMMARY.md index 21bd8360..26e5edf7 100644 --- a/manual/src/SUMMARY.md +++ b/manual/src/SUMMARY.md @@ -29,6 +29,7 @@ - [Using Delta with Magit](./tips-and-tricks/using-delta-with-magit.md) - [Using Delta with tmux](./tips-and-tricks/using-delta-with-tmux.md) - [Using Delta with VSCode](./tips-and-tricks/using-delta-with-vscode.md) + - [Generating shell completion files](./tips-and-tricks/shell-completion.md) - [Comparisons with other tools](./comparisons-with-other-tools.md) - [Build delta from source](./build-delta-from-source.md) - [Related projects](./related-projects.md) diff --git a/manual/src/tips-and-tricks/shell-completion.md b/manual/src/tips-and-tricks/shell-completion.md new file mode 100644 index 00000000..8f5379cf --- /dev/null +++ b/manual/src/tips-and-tricks/shell-completion.md @@ -0,0 +1,12 @@ +# Generating completion files for various shells + +Delta can generate completion files for various shells. +Use the `--generate-completion` subcommand to print the completion script to stdout: + +```sh +delta --generate-completion +``` + should be replaced with the lowercase name of the shell for which the script is to be generated. +Currently bash, elvish, fish, powershell and zsh are supported. + +The completion files in `etc/completion` were also generated with this function and may not be up-to-date. diff --git a/src/cli.rs b/src/cli.rs index ca967f58..d098e736 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -3,7 +3,8 @@ use std::ffi::OsString; use std::path::{Path, PathBuf}; use bat::assets::HighlightingAssets; -use clap::{ColorChoice, CommandFactory, FromArgMatches, Parser}; +use clap::{ColorChoice, CommandFactory, FromArgMatches, Parser, ValueHint}; +use clap_complete::Shell; use lazy_static::lazy_static; use syntect::highlighting::Theme as SyntaxTheme; use syntect::parsing::SyntaxSet; @@ -250,7 +251,7 @@ pub struct Opt { /// intended for other tools that use delta. pub color_only: bool, - #[arg(long = "config", default_value = "", value_name = "PATH")] + #[arg(long = "config", default_value = "", value_name = "PATH", value_hint = ValueHint::FilePath)] /// Load the config file at PATH instead of ~/.gitconfig. pub config: String, @@ -390,6 +391,10 @@ pub struct Opt { /// Sed-style command transforming file paths for display. pub file_regex_replacement: Option, + #[arg(long = "generate-completion")] + /// Print completion file for the given shell. + pub generate_completion: Option, + #[arg(long = "grep-context-line-style", value_name = "STYLE")] /// Style string for non-matching lines of grep output. /// @@ -429,7 +434,7 @@ pub struct Opt { /// See STYLES section. pub grep_line_number_style: String, - #[arg(long = "grep-output-type", value_name = "OUTPUT_TYPE")] + #[arg(long = "grep-output-type", value_name = "OUTPUT_TYPE", value_parser = ["ripgrep", "classic"])] /// Grep output format. Possible values: /// "ripgrep" - file name printed once, followed by matching lines within that file, each preceded by a line number. /// "classic" - file name:line number, followed by matching line. @@ -563,7 +568,8 @@ pub struct Opt { #[arg( long = "inspect-raw-lines", default_value = "true", - value_name = "true|false" + value_name = "true|false", + value_parser = ["true", "false"], )] /// Kill-switch for --color-moved support. /// @@ -595,7 +601,7 @@ pub struct Opt { /// affect delta's performance when entire files are added/removed. pub line_buffer_size: usize, - #[arg(long = "line-fill-method", value_name = "STRING")] + #[arg(long = "line-fill-method", value_name = "STRING", value_parser = ["ansi", "spaces"])] /// Line-fill method in side-by-side mode. /// /// How to extend the background color to the end of the line in side-by-side mode. Can be ansi @@ -846,7 +852,8 @@ pub struct Opt { #[arg( long = "paging", default_value = "auto", - value_name = "auto|always|never" + value_name = "auto|always|never", + value_parser = ["auto", "always", "never"], )] /// Whether to use a pager when displaying output. /// @@ -975,7 +982,8 @@ pub struct Opt { #[arg( long = "true-color", default_value = "auto", - value_name = "auto|always|never" + value_name = "auto|always|never", + value_parser = ["auto", "always", "never"], )] /// Whether to emit 24-bit ("true color") RGB color codes. /// @@ -1066,7 +1074,7 @@ pub struct Opt { /// See STYLES section. pub zero_style: String, - #[arg(long = "24-bit-color", value_name = "auto|always|never")] + #[arg(long = "24-bit-color", value_name = "auto|always|never", value_parser = ["auto", "always", "never"])] /// Deprecated: use --true-color. pub _24_bit_color: Option, @@ -1194,6 +1202,7 @@ impl Opt { // pseudo-flag commands such as --list-languages lazy_static! { static ref IGNORED_OPTION_NAMES: HashSet<&'static str> = vec![ + "generate-completion", "list-languages", "list-syntax-themes", "show-config", diff --git a/src/main.rs b/src/main.rs index a1f12622..67ea0ca3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,7 +82,11 @@ fn run_app() -> std::io::Result { assets, ); - let subcommand_result = if opt.list_languages { + let subcommand_result = if let Some(shell) = opt.generate_completion { + Some(subcommands::generate_completion::generate_completion_file( + shell, + )) + } else if opt.list_languages { Some(list_languages()) } else if opt.list_syntax_themes { Some(subcommands::list_syntax_themes::list_syntax_themes()) diff --git a/src/subcommands/generate_completion.rs b/src/subcommands/generate_completion.rs new file mode 100644 index 00000000..00425b7f --- /dev/null +++ b/src/subcommands/generate_completion.rs @@ -0,0 +1,11 @@ +use clap::CommandFactory; +use clap_complete::{generate, Shell}; + +use crate::cli; + +pub fn generate_completion_file(shell: Shell) -> std::io::Result<()> { + let mut cmd = cli::Opt::command(); + let bin_name = cmd.get_bin_name().unwrap_or(cmd.get_name()).to_string(); + generate(shell, &mut cmd, bin_name, &mut std::io::stdout()); + Ok(()) +} diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs index 26f04233..bb7a1f7c 100644 --- a/src/subcommands/mod.rs +++ b/src/subcommands/mod.rs @@ -1,4 +1,5 @@ pub mod diff; +pub mod generate_completion; pub mod list_syntax_themes; pub mod parse_ansi; mod sample_diff; -- cgit v1.2.3