tools: change default for `v complete` (autodetect shell)

pull/13090/head
Delyan Angelov 2022-01-07 23:36:42 +02:00
parent bb6aabd430
commit 95f6bd7fca
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 70 additions and 56 deletions

View File

@ -229,6 +229,16 @@ const (
fn auto_complete(args []string) { fn auto_complete(args []string) {
if args.len <= 1 || args[0] != 'complete' { if args.len <= 1 || args[0] != 'complete' {
if args.len == 1 { if args.len == 1 {
shell_path := os.getenv('SHELL')
if shell_path.len > 0 {
shell_name := os.file_name(shell_path).to_lower()
if shell_name in auto_complete_shells {
println(setup_for_shell(shell_name))
exit(0)
}
eprintln('Unknown shell ${shell_name}. Supported shells are: $auto_complete_shells')
exit(1)
}
eprintln('auto completion require arguments to work.') eprintln('auto completion require arguments to work.')
} else { } else {
eprintln('auto completion failed for "$args".') eprintln('auto completion failed for "$args".')
@ -244,62 +254,7 @@ fn auto_complete(args []string) {
exit(1) exit(1)
} }
shell := sub_args[1] shell := sub_args[1]
mut setup := '' println(setup_for_shell(shell))
match shell {
'bash' {
setup = '
_v_completions() {
local src
local limit
# Send all words up to the word the cursor is currently on
let limit=1+\$COMP_CWORD
src=\$($vexe complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit}))
if [[ \$? == 0 ]]; then
eval \${src}
#echo \${src}
fi
}
complete -o nospace -F _v_completions v
'
}
'fish' {
setup = '
function __v_completions
# Send all words up to the one before the cursor
$vexe complete fish (commandline -cop)
end
complete -f -c v -a "(__v_completions)"
'
}
'zsh' {
setup = '
#compdef v
_v() {
local src
# Send all words up to the word the cursor is currently on
src=\$($vexe complete zsh \$(printf "%s\\n" \${(@)words[1,\$CURRENT]}))
if [[ \$? == 0 ]]; then
eval \${src}
#echo \${src}
fi
}
compdef _v v
'
}
'powershell' {
setup = '
Register-ArgumentCompleter -Native -CommandName v -ScriptBlock {
param(\$commandName, \$wordToComplete, \$cursorPosition)
$vexe complete powershell "\$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_)
}
}
'
}
else {}
}
println(setup)
} }
'bash' { 'bash' {
if sub_args.len <= 1 { if sub_args.len <= 1 {
@ -474,6 +429,65 @@ fn auto_complete_request(args []string) []string {
return list return list
} }
fn setup_for_shell(shell string) string {
mut setup := ''
match shell {
'bash' {
setup = '
_v_completions() {
local src
local limit
# Send all words up to the word the cursor is currently on
let limit=1+\$COMP_CWORD
src=\$($vexe complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit}))
if [[ \$? == 0 ]]; then
eval \${src}
#echo \${src}
fi
}
complete -o nospace -F _v_completions v
'
}
'fish' {
setup = '
function __v_completions
# Send all words up to the one before the cursor
$vexe complete fish (commandline -cop)
end
complete -f -c v -a "(__v_completions)"
'
}
'zsh' {
setup = '
#compdef v
_v() {
local src
# Send all words up to the word the cursor is currently on
src=\$($vexe complete zsh \$(printf "%s\\n" \${(@)words[1,\$CURRENT]}))
if [[ \$? == 0 ]]; then
eval \${src}
#echo \${src}
fi
}
compdef _v v
'
}
'powershell' {
setup = '
Register-ArgumentCompleter -Native -CommandName v -ScriptBlock {
param(\$commandName, \$wordToComplete, \$cursorPosition)
$vexe complete powershell "\$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_)
}
}
'
}
else {}
}
return setup
}
fn main() { fn main() {
args := os.args[1..] args := os.args[1..]
// println('"$args"') // println('"$args"')