Skip to content

Commit

Permalink
seperating zsh and bash autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
zveinn committed Feb 12, 2024
1 parent 16a6929 commit 8197f92
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cmd/kes/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ func installAutoCompletion() {
}

var filename string
var isZsh bool
switch {
case strings.HasSuffix(shell, "zsh"):
filename = ".zshrc"
isZsh = true
case strings.HasSuffix(shell, "bash"):
filename = ".bashrc"
default:
Expand Down Expand Up @@ -128,8 +130,14 @@ func installAutoCompletion() {
autoloadCmd = "autoload -U +X bashcompinit && bashcompinit"
completeCmd = fmt.Sprintf("complete -o default -C %s %s", binaryPath, os.Args[0])
)

hasAutoloadLine, hasCompleteLine := isCompletionInstalled(filename, autoloadCmd, completeCmd)
if hasAutoloadLine && hasCompleteLine {
if isZsh && (hasAutoloadLine && hasCompleteLine) {
cli.Println("Completion is already installed.")
return
}

if !isZsh && hasCompleteLine {
cli.Println("Completion is already installed.")
return
}
Expand All @@ -140,11 +148,12 @@ func installAutoCompletion() {
}
defer file.Close()

if !hasAutoloadLine {
if isZsh && !hasAutoloadLine {
if _, err = file.WriteString(autoloadCmd + "\n"); err != nil {
cli.Fatalf("failed to add '%s' to '%s': %v", autoloadCmd, filename, err)
}
}

if !hasCompleteLine {
if _, err = file.WriteString(completeCmd + "\n"); err != nil {
cli.Fatalf("failed to add '%s' to '%s': %v", completeCmd, filename, err)
Expand All @@ -157,7 +166,7 @@ func installAutoCompletion() {
cli.Printf("Added completion to '%s'\n", filename)
cli.Println()
cli.Printf("To uninstall completion remove the following lines from '%s':\n", filename)
if !hasAutoloadLine {
if isZsh && !hasAutoloadLine {
cli.Println(" ", autoloadCmd)
}
if !hasCompleteLine {
Expand Down

0 comments on commit 8197f92

Please sign in to comment.