From 95f6bd7fca2cfe523c9a6c99816238469cf4dc00 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 7 Jan 2022 23:36:42 +0200 Subject: [PATCH] tools: change default for `v complete` (autodetect shell) --- cmd/tools/vcomplete.v | 126 +++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 56 deletions(-) diff --git a/cmd/tools/vcomplete.v b/cmd/tools/vcomplete.v index 9e4e061fe4..342db79e0c 100644 --- a/cmd/tools/vcomplete.v +++ b/cmd/tools/vcomplete.v @@ -229,6 +229,16 @@ const ( fn auto_complete(args []string) { if args.len <= 1 || args[0] != 'complete' { 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.') } else { eprintln('auto completion failed for "$args".') @@ -244,62 +254,7 @@ fn auto_complete(args []string) { exit(1) } shell := sub_args[1] - 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 {} - } - println(setup) + println(setup_for_shell(shell)) } 'bash' { if sub_args.len <= 1 { @@ -474,6 +429,65 @@ fn auto_complete_request(args []string) []string { 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() { args := os.args[1..] // println('"$args"')