Skip to content

Commit

Permalink
fix(completion): Put the completion file somewhere bash can automatic…
Browse files Browse the repository at this point in the history
…ally load it. (#39)
  • Loading branch information
JeffFaer authored Apr 20, 2024
1 parent ad527a7 commit ad7ab41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ go.work
go.work.sum

tmux-vcs-sync
tvs_completion.*
tmux-vcs-sync.*

*.swp
33 changes: 13 additions & 20 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
Expand All @@ -30,7 +31,7 @@ func (Build) Build() error {
func (Build) Completion(shell string) error {
mg.Deps(Build.Build)
fmt.Printf("Generating %s completion...\n", shell)
return os.WriteFile(fmt.Sprintf("tvs_completion.%s", shell), []byte(fmt.Sprintf("source <(tmux-vcs-sync completion %q)", shell)), 0644)
return os.WriteFile(fmt.Sprintf("tmux-vcs-sync.%s", shell), []byte(fmt.Sprintf("source <(tmux-vcs-sync completion %q)", shell)), 0644)
}

func Test() error {
Expand All @@ -50,34 +51,26 @@ func (Install) Completion(shell string) error {
return err
}
fmt.Printf("Installing %s completion to %s...\n", shell, dir)
name := fmt.Sprintf("tvs_completion.%s", shell)
name := fmt.Sprintf("tmux-vcs-sync.%s", shell)
return sh.Run("cp", name, filepath.Join(dir, name))
}

func determineCompletionDir(shell string) (string, error) {
if shell != "bash" {
return "", fmt.Errorf("the author only uses bash so they don't know where other shell completion files are supposed to go")
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
for _, dir := range []string{
filepath.Join(home, "bash_completion.d"),
filepath.Join(home, "bashrc.d/bash_completion.d"),
"/etc/bash_completion.d",
} {
if stat, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
continue
}
return "", err
} else if stat.IsDir() {
return dir, nil
}
// /usr/share/bash-completion/bash_completion checks
// ${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions
dir := os.Getenv("BASH_COMPLETION_USER_DIR")
if dir == "" {
dir = filepath.Join(xdg.DataHome, "bash-completion")
}
dir = filepath.Join(dir, "completions")

return "", fmt.Errorf("did not find any appropriate directory to install %s completions", shell)
if err := os.MkdirAll(dir, 0775); err != nil {
return "", err
}
return dir, nil
}

func Clean() {
Expand Down

0 comments on commit ad7ab41

Please sign in to comment.