Files
diple/cli.go
2026-07-28 20:45:05 +02:00

239 lines
8.9 KiB
Go

package main
import (
"fmt"
"io"
"strings"
)
var completionShells = []string{"bash", "zsh", "fish"}
func handleCompletionCommand(args []string, output io.Writer) (bool, error) {
if len(args) == 0 || args[0] != "completion" {
return false, nil
}
if len(args) == 2 && (args[1] == "-h" || args[1] == "--help") {
_, err := fmt.Fprintln(output, completionHelp)
return true, err
}
if len(args) != 2 {
return true, fmt.Errorf("usage: diple completion <bash|zsh|fish>")
}
var script string
switch args[1] {
case "bash":
script = bashCompletion
case "zsh":
script = zshCompletion
case "fish":
script = fishCompletion
default:
return true, fmt.Errorf(
"unsupported shell %q; choose %s",
args[1], strings.Join(completionShells, ", "),
)
}
_, err := io.WriteString(output, script)
return true, err
}
const completionHelp = `Generate a shell completion script.
Usage:
diple completion <shell>
Available shells:
bash
zsh
fish
Run 'diple completion <shell>' and source or install the generated script.
See the README for shell-specific installation paths.`
func writeCLIHelp(output io.Writer, defaults Config, configPath string) {
fmt.Fprintf(output, `diple — review and manage GitHub pull requests from the terminal
Usage:
diple [options]
diple completion <bash|zsh|fish>
diple help
Pull-request selection:
--repo OWNER/REPOSITORY Limit results to one repository (or use GH_REPO).
--all[=BOOL] Include every open PR in --repo. Default: %t.
--limit NUMBER Maximum PRs to load, from 1 to 1000. Default: %d.
GitHub and refresh:
--poll DURATION Base refresh interval, at least 2s. Default: %s.
--endpoint URL GitHub GraphQL endpoint.
Default: %s.
Appearance and navigation:
--theme NAME dark, light, catppuccin, catppuccin-latte,
gruvbox, gruvbox-light, one-dark-pro, github,
github-light, high-contrast, no-color, or custom.
Default: %s.
--dashboard-mode MODE hotkey or intermediate. Default: %s.
--thread-list-width N List width in percent, from 20 to 60. Default: %d.
--fold-resolved[=BOOL] Start resolved threads folded. Default: %t.
--compact-reviews[=BOOL] Aggregate submitted-review history. Default: %t.
--path-scroll[=BOOL] Scroll truncated file paths. Default: %t.
--path-scroll-interval D Path scroll step interval. Default: %s.
--editor-mode MODE vim or standard. Default: %s.
Local state:
--config FILE TOML configuration file.
Default: %s.
--cache[=BOOL] Enable instant cached startup and offline fallback.
Default: %t.
--cache-max-age DURATION Maximum offline cache age; 0 disables expiry.
Default: %s.
--cache-dir DIRECTORY Override the operating-system cache directory.
Other:
-h, --help Show this help and exit.
Boolean options accept explicit values, for example --cache=false.
Command-line options override TOML settings. GH_REPO is used only when
--repo is absent. DIPLE_CONFIG selects a configuration file; GH_THREADS_CONFIG
is retained as a migration fallback.
Authentication uses GH_TOKEN or GITHUB_TOKEN when set, otherwise the active
credential from 'gh auth login'. Run 'diple completion --help' for completion
installation guidance.
`,
defaults.ShowAll,
defaults.Limit,
defaults.RefreshInterval.Duration,
defaults.Endpoint,
defaults.Theme,
defaults.Display.DashboardMode,
defaults.Display.ThreadListWidthPercent,
defaults.Display.FoldResolved,
defaults.Display.CompactReviews,
defaults.Paths.Scroll,
defaults.Paths.ScrollInterval.Duration,
defaults.Editing.Mode,
configPath,
defaults.Cache.Enabled,
defaults.Cache.MaxAge.Duration,
)
}
const bashCompletion = `# bash completion for diple
_diple_completion() {
local current previous
current="${COMP_WORDS[COMP_CWORD]}"
previous="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${COMP_CWORD} -eq 1 && ${current} != -* ]]; then
COMPREPLY=($(compgen -W "completion help" -- "${current}"))
return
fi
if [[ ${COMP_WORDS[1]} == completion ]]; then
COMPREPLY=($(compgen -W "bash zsh fish" -- "${current}"))
return
fi
if [[ ${COMP_WORDS[1]} == help ]]; then
COMPREPLY=()
return
fi
case "${previous}" in
--theme)
COMPREPLY=($(compgen -W "dark light catppuccin catppuccin-mocha catppuccin-latte gruvbox gruvbox-dark gruvbox-light one-dark-pro github github-dark github-light high-contrast no-color custom" -- "${current}"))
return
;;
--dashboard-mode)
COMPREPLY=($(compgen -W "hotkey intermediate" -- "${current}"))
return
;;
--editor-mode)
COMPREPLY=($(compgen -W "vim standard" -- "${current}"))
return
;;
--config|--cache-dir)
COMPREPLY=($(compgen -f -- "${current}"))
return
;;
esac
local options="--repo --all --limit --poll --endpoint --theme --dashboard-mode --thread-list-width --fold-resolved --compact-reviews --path-scroll --path-scroll-interval --editor-mode --config --cache --cache-max-age --cache-dir --help -h"
COMPREPLY=($(compgen -W "${options}" -- "${current}"))
}
complete -F _diple_completion diple
`
const zshCompletion = `#compdef diple
_diple() {
local -a themes
themes=(dark light catppuccin catppuccin-mocha catppuccin-latte gruvbox gruvbox-dark gruvbox-light one-dark-pro github github-dark github-light high-contrast no-color custom)
if (( CURRENT == 2 )) && [[ ${PREFIX} != -* ]]; then
_values 'command' \
'completion[generate shell completion]' \
'help[show command-line help]'
return
fi
if [[ ${words[2]} == completion ]]; then
_values 'shell' bash zsh fish
return
fi
if [[ ${words[2]} == help ]]; then
return
fi
_arguments -s \
'--repo[limit results to one repository]:owner/repository:' \
'--all=[include every open PR in the repository]:boolean:(true false)' \
'--limit[maximum pull requests to load]:number:' \
'--poll[base GitHub refresh interval]:duration:' \
'--endpoint[GitHub GraphQL endpoint]:url:' \
'--theme[UI and syntax color theme]:theme:($themes)' \
'--dashboard-mode[dashboard navigation mode]:mode:(hotkey intermediate)' \
'--thread-list-width[thread-list width percentage]:percent:' \
'--fold-resolved=[start resolved threads folded]:boolean:(true false)' \
'--compact-reviews=[aggregate submitted reviews]:boolean:(true false)' \
'--path-scroll=[scroll truncated paths]:boolean:(true false)' \
'--path-scroll-interval[path scrolling interval]:duration:' \
'--editor-mode[description editor mode]:mode:(vim standard)' \
'--config[TOML configuration file]:file:_files' \
'--cache=[enable local read cache]:boolean:(true false)' \
'--cache-max-age[maximum offline cache age]:duration:' \
'--cache-dir[local read-cache directory]:directory:_directories' \
'(-h --help)'{-h,--help}'[show help]'
}
if (( ! ${+functions[compdef]} )); then
autoload -Uz compinit
compinit -D
fi
compdef _diple diple
`
const fishCompletion = `# fish completion for diple
complete -c diple -f
complete -c diple -n '__fish_use_subcommand' -a completion -d 'Generate shell completion'
complete -c diple -n '__fish_use_subcommand' -a help -d 'Show help'
complete -c diple -n '__fish_seen_subcommand_from completion' -a 'bash zsh fish'
complete -c diple -l repo -d 'Limit results to owner/repository'
complete -c diple -l all -d 'Include every open PR in --repo'
complete -c diple -l limit -x -d 'Maximum pull requests to load'
complete -c diple -l poll -x -d 'Base GitHub refresh interval'
complete -c diple -l endpoint -x -d 'GitHub GraphQL endpoint'
complete -c diple -l theme -x -a 'dark light catppuccin catppuccin-mocha catppuccin-latte gruvbox gruvbox-dark gruvbox-light one-dark-pro github github-dark github-light high-contrast no-color custom' -d 'UI and syntax color theme'
complete -c diple -l dashboard-mode -x -a 'hotkey intermediate' -d 'Dashboard navigation mode'
complete -c diple -l thread-list-width -x -d 'Thread-list width percentage'
complete -c diple -l fold-resolved -d 'Start resolved threads folded'
complete -c diple -l compact-reviews -d 'Aggregate submitted reviews'
complete -c diple -l path-scroll -d 'Scroll truncated paths'
complete -c diple -l path-scroll-interval -x -d 'Path scrolling interval'
complete -c diple -l editor-mode -x -a 'vim standard' -d 'Description editor mode'
complete -c diple -l config -r -F -d 'TOML configuration file'
complete -c diple -l cache -d 'Enable local read cache'
complete -c diple -l cache-max-age -x -d 'Maximum offline cache age'
complete -c diple -l cache-dir -r -a '(__fish_complete_directories)' -d 'Local read-cache directory'
complete -c diple -s h -l help -d 'Show help'
`