Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seperating zsh and bash autocomplete #441

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading