diff options
| author | ewy <ewy0@protonmail.com> | 2026-05-17 01:37:24 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-05-17 01:37:24 +0200 |
| commit | f5807d9f3a6c96e70912b61fac17120f412b5782 (patch) | |
| tree | d6928795e06b1af000ffba2ae50bb6f8f7b72685 /completion | |
| parent | 7984fd9beaa7c903288142818cb328c584a139a5 (diff) | |
* integration tests with a pik target to run them
* add abstraction for paths to facilitate unit tests
* flesh out completion (--install-completion)
* do sync init before stateless modes so list knows more
Diffstat (limited to 'completion')
| -rw-r--r-- | completion/completion.go | 50 | ||||
| -rw-r--r-- | completion/completion.sh | 3 |
2 files changed, 49 insertions, 4 deletions
diff --git a/completion/completion.go b/completion/completion.go index 6aad9d1..f3d76d7 100644 --- a/completion/completion.go +++ b/completion/completion.go @@ -2,15 +2,61 @@ package completion import ( _ "embed" + "errors" + "fmt" + "github.com/ewy1/pik/paths" "github.com/ewy1/pik/spool" + "os" + "path/filepath" + "strings" ) //go:embed completion.sh var completionCode string +var completionFormat = ` +# %s +%s +` + +var completionComment = "\n# pik completion (installed by `pik --install-completion`)\n" + var completionCodeByShell = map[string]string{ - "bash": ". <(pik --completion)", - "zsh": `autoload -Uz compinit && compinit && source <(pik --completion)`, + "bash": ". <(pik --completion)\n\n", + "zsh": `autoload -Uz compinit && compinit && source <(pik --completion)\n\n`, +} + +var completionFileByShell = map[string]string{ + "bash": ".bashrc", + "zsh": ".zshrc", +} + +var AlreadyInstalledError = errors.New("completion already installed") + +func Add(shell string) error { + f := filepath.Join(paths.HomeDir.String(), completionFileByShell[shell]) + content, err := os.ReadFile(f) + if err != nil { + return err + } + if strings.Contains(string(content), strings.TrimSpace(completionCodeByShell[shell])) { + return AlreadyInstalledError + } + fd, err := os.OpenFile(f, os.O_APPEND|os.O_WRONLY, 0600) + defer fd.Close() + if err != nil { + return err + } + _, err = fd.Write([]byte(fmt.Sprintf(completionFormat, completionComment, completionCodeByShell[shell]))) + if err != nil { + return err + } + successMessage(shell, f) + return nil +} + +func successMessage(shell string, file string) { + _, _ = spool.Print("Installed completion for %s in %s\n", shell, file) } func Echo() error { diff --git a/completion/completion.sh b/completion/completion.sh index 36876cf..a48733f 100644 --- a/completion/completion.sh +++ b/completion/completion.sh @@ -1,9 +1,8 @@ -#/usr/bin/env bash _pik_completions() { QUERY="" for word in COMP_WORDS ; do - if [ ! query = "-"* ] ; then + if [[ ! query = "-"* ]] ; then QUERY="$QUERY $WORD" fi done |
