Skip to content

Commit 495237f

Browse files
committed
feat(cli): add fish shell completion support
Add a custom completion command that replaces the yargs built-in bash-only .completion() with multi-shell support (bash + fish). Key features: - detectShell(): auto-detects shell from $SHELL env var - completionScript(): generates shell-specific completion scripts - Fish script uses SHELL=zsh trick to get yargs name:description output, parses colon pairs into Fish tab-separated format - Bash script matches official yargs v18 template with mapfile for safe word splitting and $0 placeholder filtering - Both scripts use --get-yargs-completions for dynamic completions Usage: opencode completion --shell fish | source opencode completion --shell fish > ~/.config/fish/completions/opencode.fish eval "$(opencode completion --shell bash)" Closes anomalyco#1515 Assisted-by: Claude (claude-opus-4-6)
1 parent 581f4c4 commit 495237f

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import type { Argv } from "yargs"
2+
import { cmd } from "./cmd"
3+
import path from "path"
4+
5+
export function detectShell(shellEnv: string | undefined): "bash" | "fish" {
6+
if (!shellEnv) return "bash"
7+
if (path.basename(shellEnv) === "fish") return "fish"
8+
return "bash"
9+
}
10+
11+
function bashScript() {
12+
// Matches the official yargs v18 completion script with mapfile for proper
13+
// handling of completions containing spaces or special characters.
14+
// Bash does not support tab-separated descriptions natively (compgen -W
15+
// breaks on tabs), so completions are plain command/option names only.
16+
return `###-begin-opencode-completions-###
17+
#
18+
# yargs command completion script
19+
#
20+
# Installation: opencode completion --shell bash >> ~/.bashrc
21+
# or opencode completion --shell bash >> ~/.bash_profile on OSX.
22+
#
23+
_opencode_yargs_completions()
24+
{
25+
local cur_word args type_list
26+
27+
cur_word="\${COMP_WORDS[COMP_CWORD]}"
28+
args=("\${COMP_WORDS[@]}")
29+
30+
# ask yargs to generate completions, filtering out internal placeholders
31+
# see https://stackoverflow.com/a/40944195/7080036 for the spaces-handling awk
32+
mapfile -t type_list < <(opencode --get-yargs-completions "\${args[@]}" | grep -vxF '\$0')
33+
mapfile -t COMPREPLY < <(compgen -W "$( printf '%q ' "\${type_list[@]}" )" -- "\${cur_word}" |
34+
awk '/ / { print "\\""\$0"\\"" } /^[^ ]+\$/ { print \$0 }')
35+
36+
# if no match was found, fall back to filename completion
37+
if [ \${#COMPREPLY[@]} -eq 0 ]; then
38+
COMPREPLY=()
39+
fi
40+
41+
return 0
42+
}
43+
complete -o bashdefault -o default -F _opencode_yargs_completions opencode
44+
###-end-opencode-completions-###
45+
`
46+
}
47+
48+
function fishScript() {
49+
return `###-begin-opencode-completions-###
50+
#
51+
# Fish shell completions for opencode
52+
#
53+
# Installation:
54+
# opencode completion --shell fish | source # Current session
55+
# opencode completion --shell fish > ~/.config/fish/completions/opencode.fish # Permanent
56+
#
57+
58+
function __opencode_yargs_completions
59+
set -l tokens (commandline -opc)
60+
set -l current (commandline -ct)
61+
62+
# Ask yargs to generate completions.
63+
# Setting SHELL=zsh triggers yargs' built-in "name:description" output format.
64+
# We parse the colon-separated pairs and convert to Fish's tab-separated format.
65+
set -l completions (SHELL=zsh command opencode --get-yargs-completions $tokens $current 2>/dev/null)
66+
67+
for completion in $completions
68+
# Parse yargs "name:description" format into Fish's tab-separated format.
69+
# Yargs escapes literal colons in names as "\\:" so we split on the first
70+
# unescaped colon only. Fish's complete builtin expects "name\\tdescription"
71+
# where \\t is a real tab character -- printf handles this correctly.
72+
set -l parts (string split -m 1 ':' -- $completion)
73+
74+
# Filter out yargs internal placeholders (matched after parsing since
75+
# SHELL=zsh output includes descriptions like "$0:start opencode tui")
76+
switch $parts[1]
77+
case '$0' '_generate_completions'
78+
continue
79+
end
80+
81+
if test (count $parts) -gt 1 -a -n "$parts[2]"
82+
printf '%s\\t%s\\n' $parts[1] $parts[2]
83+
else
84+
echo $completion
85+
end
86+
end
87+
end
88+
89+
complete -c opencode -f -a '(__opencode_yargs_completions)'
90+
###-end-opencode-completions-###
91+
`
92+
}
93+
94+
export function completionScript(shell: "bash" | "fish") {
95+
if (shell === "bash") return bashScript()
96+
if (shell === "fish") return fishScript()
97+
const _exhaustive: never = shell
98+
throw new Error(`Unsupported shell: ${_exhaustive}`)
99+
}
100+
101+
export const CompletionCommand = cmd({
102+
command: "completion",
103+
describe: "generate shell completion script",
104+
builder: (yargs: Argv) =>
105+
yargs.option("shell", {
106+
type: "string",
107+
describe: "shell type (auto-detected from $SHELL if omitted)",
108+
choices: ["bash", "fish"] as const,
109+
}),
110+
handler: async (args) => {
111+
const shell = args.shell ?? detectShell(process.env.SHELL)
112+
process.stdout.write(completionScript(shell))
113+
},
114+
})

0 commit comments

Comments
 (0)