Skip to content

Commit

Permalink
Add support for gitbash (#85)
Browse files Browse the repository at this point in the history
* Add support for gitbash
  • Loading branch information
buzzy authored Nov 10, 2024
1 parent 0e43756 commit a00b4b6
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func runAutocomplete() {

filename, _ := os.Executable()

filename = filepath.Base(filename)

trimmed := strings.TrimLeft(os.Getenv("COMP_LINE"), filename)
Expand Down Expand Up @@ -250,9 +251,13 @@ func collectFlagsAndOptions() map[string]map[string][]string {

//Collect valid flag values
if attribute.Value.Enum != nil {

for _, enum := range attribute.Value.Enum {
allFlags[command][flagName] = append(allFlags[command][flagName], enum.(string)+" ")
switch v := enum.(type) {
case string:
allFlags[command][flagName] = append(allFlags[command][flagName], v+" ")
case float64:
allFlags[command][flagName] = append(allFlags[command][flagName], fmt.Sprintf("%f ", v))
}
}

}
Expand Down Expand Up @@ -302,6 +307,8 @@ func enableAutocomplete() {
shell := filepath.Base(os.Getenv("SHELL"))
checkErr(err)

fmt.Println("Detected shell: " + shell)

theConfig := ""

switch shell {
Expand All @@ -311,6 +318,9 @@ func enableAutocomplete() {
case "zsh":
//Install auto-complete for zsh
theConfig = autoCompleteZsh(home, fname)
case "bash.exe":
//Install auto-complete for gitbash
theConfig = autoCompleteGitbash(home, fname)
default:
fmt.Println("Could not find any shell that supports the auto-complete feature.")
os.Exit(1)
Expand All @@ -321,6 +331,57 @@ func enableAutocomplete() {

}

func convertPath(windowsPath string) string {
// Replace backslashes with forward slashes
unixPath := strings.ReplaceAll(windowsPath, "\\", "/")
// Add leading slash and convert drive letter to lowercase
if len(unixPath) > 1 && unixPath[1] == ':' {
unixPath = "/" + strings.ToLower(string(unixPath[0])) + unixPath[2:]
}
return unixPath
}

// Install auto-complete for gitbash
func autoCompleteGitbash(home string, fname string) string {

theConfig := home + "\\" + ".bashrc"

// Check if a .bashrc file already exist
_, err := os.Stat(theConfig)
if err != nil {
// Create a new .bashrc file based on the global .bashrc file
input, err := os.ReadFile(os.Getenv("EXEPATH") + "\\etc\\bash.bashrc")
checkErr(err)

err = os.WriteFile(theConfig, input, 0666)
checkErr(err)
}

f, err := os.OpenFile(theConfig, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0666)
checkErr(err)
defer f.Close()

findLine := regexp.MustCompile("^complete (.*) scalr$")

scanner := bufio.NewScanner(f)
for scanner.Scan() {

if findLine.MatchString(scanner.Text()) {
fmt.Println("Looks like auto-complete is already installed in " + theConfig + ". Please restart your shell to enable it.")
os.Exit(0)
}
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}

_, err = f.WriteString("complete -o nospace -C " + convertPath(fname) + " scalr.exe\n")
checkErr(err)

return theConfig
}

// Install auto-complete for bash
func autoCompleteBash(home string, fname string) string {

Expand Down

0 comments on commit a00b4b6

Please sign in to comment.