package multi
import (
"fmt"
"sort"
"strings"
shlex "github.com/carapace-sh/carapace-shlex/v2"
"github.com/carapace-sh/carapace/internal/log"
"github.com/carapace-sh/carapace/internal/shell/bash"
"github.com/carapace-sh/carapace/internal/shell/bash_ble"
"github.com/carapace-sh/carapace/internal/shell/elvish"
"github.com/carapace-sh/carapace/internal/shell/fish"
"github.com/carapace-sh/carapace/internal/shell/nushell"
"github.com/carapace-sh/carapace/internal/shell/oil"
"github.com/carapace-sh/carapace/internal/shell/powershell"
"github.com/carapace-sh/carapace/internal/shell/tcsh"
"github.com/carapace-sh/carapace/internal/shell/xonsh"
"github.com/carapace-sh/carapace/internal/shell/zsh"
"github.com/carapace-sh/carapace/pkg/ps"
)
func Snippet (shell string , names []string , defaultName string , snippetFuncs map [string ][]string ) (string , error ) {
if shell == "" {
shell = ps .DetermineShell ()
}
if !shlex .IsBareWord (defaultName , shlex .Format (shell )) {
log .LOG .Printf ("skipped invalid completer name %q" , defaultName )
defaultName = ""
}
valid := make ([]string , 0 , len (names ))
for _ , name := range names {
if !shlex .IsBareWord (name , shlex .Format (shell )) {
log .LOG .Printf ("skipped invalid completer name %q" , name )
continue
}
valid = append (valid , name )
}
names = valid
snippetFuncsStr := snippetFuncsForShell (shell , snippetFuncs )
switch shell {
case "bash" :
return bash .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "bash-ble" :
return bash_ble .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "zsh" :
return zsh .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "fish" :
return fish .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "elvish" :
return elvish .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "nushell" :
return nushell .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "powershell" :
return powershell .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "xonsh" :
return xonsh .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "oil" :
return oil .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "tcsh" :
return tcsh .SnippetMulti (names , defaultName , snippetFuncsStr ), nil
case "export" :
return "" , nil
case "cmd-clink" :
return "" , fmt .Errorf ("unsupported shell for multi-completer: '%v'" , shell )
default :
supported := []string {"bash" , "bash-ble" , "elvish" , "fish" , "nushell" , "oil" , "powershell" , "tcsh" , "xonsh" , "zsh" }
sort .Strings (supported )
return "" , fmt .Errorf ("unsupported shell: '%v' [expected one of '%v']" , shell , strings .Join (supported , "', '" ))
}
}
func SnippetOrEmpty (shell string , names []string , defaultName string , snippetFuncs map [string ][]string ) string {
s , err := Snippet (shell , names , defaultName , snippetFuncs )
if err != nil {
return err .Error()
}
return s
}
func SingleSnippet (shell string , command string , names []string , defaultName string , snippetFuncs map [string ][]string ) (string , error ) {
if shell == "" {
shell = ps .DetermineShell ()
}
if !shlex .IsBareWord (command , shlex .Format (shell )) {
log .LOG .Printf ("skipped invalid completer name %q" , command )
return "" , nil
}
switch shell {
case "bash" :
return bash .SnippetSingle (command , true ), nil
case "bash-ble" :
return bash_ble .SnippetSingle (command , true ), nil
case "zsh" :
return zsh .SnippetSingle (command , true ), nil
case "fish" :
return fish .SnippetSingle (command , true ), nil
case "elvish" :
return elvish .SnippetSingle (command , true ), nil
case "nushell" :
return nushell .SnippetSingle (command , true ), nil
case "powershell" :
return powershell .SnippetSingle (command , true ), nil
case "xonsh" :
return xonsh .SnippetSingle (command , true ), nil
case "oil" :
return oil .SnippetSingle (command , true ), nil
case "tcsh" :
return tcsh .SnippetSingle (command , true ), nil
case "export" :
return "" , nil
case "cmd-clink" :
return "" , fmt .Errorf ("unsupported shell for multi-completer: '%v'" , shell )
default :
supported := []string {"bash" , "bash-ble" , "elvish" , "fish" , "nushell" , "oil" , "powershell" , "tcsh" , "xonsh" , "zsh" }
sort .Strings (supported )
return "" , fmt .Errorf ("unsupported shell: '%v' [expected one of '%v']" , shell , strings .Join (supported , "', '" ))
}
}
func SingleSnippetOrEmpty (shell string , command string , names []string , defaultName string , snippetFuncs map [string ][]string ) string {
s , err := SingleSnippet (shell , command , names , defaultName , snippetFuncs )
if err != nil {
return err .Error()
}
return s
}
func snippetFuncsForShell(shell string , snippetFuncs map [string ][]string ) string {
if snippetFuncs == nil {
return ""
}
parts := snippetFuncs [shell ]
if len (parts ) == 0 {
return ""
}
return strings .Join (parts , "\n" ) + "\n"
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .