-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: autocomplete bash and zsh (#93)
* chore: Fix autocomplete Signed-off-by: Ce Gao <cegao@tensorchord.ai> * fix: Update Signed-off-by: Ce Gao <cegao@tensorchord.ai>
- Loading branch information
Showing
6 changed files
with
209 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package autocomplete | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/cockroachdb/errors" | ||
|
||
"github.com/tensorchord/MIDI/pkg/util/fileutil" | ||
) | ||
|
||
var autocompleteBASH = ` | ||
#! /bin/bash | ||
$PROG=midi | ||
: ${PROG:=$(basename ${BASH_SOURCE})} | ||
_cli_bash_autocomplete() { | ||
if [[ "${COMP_WORDS[0]}" != "source" ]]; then | ||
local cur opts base | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion ) | ||
else | ||
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) | ||
fi | ||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||
return 0 | ||
fi | ||
} | ||
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG | ||
unset PROG | ||
` | ||
|
||
func InsertBashCompleteEntry() error { | ||
var path string | ||
if runtime.GOOS == "darwin" { | ||
path = "/usr/local/etc/bash_completion.d/midi" | ||
} else { | ||
path = "/usr/share/bash-completion/completions/midi" | ||
} | ||
dirPath := filepath.Dir(path) | ||
|
||
dirPathExists, err := fileutil.DirExists(dirPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed checking if %s exists", dirPath) | ||
} | ||
if !dirPathExists { | ||
fmt.Fprintf(os.Stderr, "Warning: unable to enable bash-completion: %s does not exist\n", dirPath) | ||
return nil // bash-completion isn't available, silently fail. | ||
} | ||
|
||
pathExists, err := fileutil.FileExists(path) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed checking if %s exists", path) | ||
} | ||
if pathExists { | ||
return nil // file already exists, don't update it. | ||
} | ||
|
||
// create the completion file | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
bashEntry, err := bashCompleteEntry() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Warning: unable to enable bash-completion: %s\n", err) | ||
return nil // bash-completion isn't available, silently fail. | ||
} | ||
|
||
_, err = f.Write([]byte(bashEntry)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed writing to %s", path) | ||
} | ||
return nil | ||
} | ||
|
||
func bashCompleteEntry() (string, error) { | ||
return autocompleteBASH, nil | ||
} |
Oops, something went wrong.