package shell
import (
"fmt"
"os/exec"
"sort"
"strings"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/shell/bash"
"github.com/carapace-sh/carapace/internal/shell/bash_ble"
"github.com/carapace-sh/carapace/internal/shell/cmd_clink"
"github.com/carapace-sh/carapace/internal/shell/elvish"
"github.com/carapace-sh/carapace/internal/shell/export"
"github.com/carapace-sh/carapace/internal/shell/fish"
"github.com/carapace-sh/carapace/internal/shell/ion"
"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"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
)
func Snippet (cmd *cobra .Command , shell string ) (string , error ) {
if shell == "" {
shell = ps .DetermineShell ()
}
shellSnippets := map [string ]func (cmd *cobra .Command ) string {
"bash" : bash .Snippet ,
"bash-ble" : bash_ble .Snippet ,
"cmd-clink" : cmd_clink .Snippet ,
"export" : export .Snippet ,
"fish" : fish .Snippet ,
"elvish" : elvish .Snippet ,
"ion" : ion .Snippet ,
"nushell" : nushell .Snippet ,
"oil" : oil .Snippet ,
"powershell" : powershell .Snippet ,
"tcsh" : tcsh .Snippet ,
"xonsh" : xonsh .Snippet ,
"zsh" : zsh .Snippet ,
}
if s , ok := shellSnippets [shell ]; ok {
return s (cmd .Root ()), nil
}
expected := make ([]string , 0 )
for key := range shellSnippets {
expected = append (expected , key )
}
sort .Strings (expected )
return "" , fmt .Errorf ("expected one of '%v' [was: %v]" , strings .Join (expected , "', '" ), shell )
}
func Value (shell string , value string , meta common .Meta , values common .RawValues ) string {
shellFuncs := map [string ]func (currentWord string , meta common .Meta , values common .RawValues ) string {
"bash" : bash .ActionRawValues ,
"bash-ble" : bash_ble .ActionRawValues ,
"cmd-clink" : cmd_clink .ActionRawValues ,
"fish" : fish .ActionRawValues ,
"elvish" : elvish .ActionRawValues ,
"export" : export .ActionRawValues ,
"ion" : ion .ActionRawValues ,
"nushell" : nushell .ActionRawValues ,
"oil" : oil .ActionRawValues ,
"powershell" : powershell .ActionRawValues ,
"tcsh" : tcsh .ActionRawValues ,
"xonsh" : xonsh .ActionRawValues ,
"zsh" : zsh .ActionRawValues ,
}
if f , ok := shellFuncs [shell ]; ok {
if env .ColorDisabled () {
style .Carapace .Value = style .Default
style .Carapace .Description = style .Default
style .Carapace .Error = style .Underlined
style .Carapace .Usage = style .Italic
values = values .Decolor ()
}
if !env .Unfiltered () {
values = values .FilterPrefix (value )
}
switch shell {
case "elvish" , "export" , "zsh" :
default :
values = meta .Messages .Integrate (values , value )
}
if shell != "export" {
switch {
case !meta .Messages .IsEmpty ():
meta .Nospace .Add ('*' )
case env .Nospace () != "" :
meta .Nospace .Add ([]rune (env .Nospace ())...)
}
}
sort .Sort (common .ByDisplay (values ))
if env .Experimental () {
if _ , err := exec .LookPath ("tabdance" ); err == nil {
return f (value , meta , values )
}
}
for index := range values {
values [index ].Uid = ""
}
return f (value , meta , values )
}
return ""
}
The pages are generated with Golds v0.8.2 . (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 .