diff options
| author | Jeff Carr <[email protected]> | 2025-10-12 02:11:40 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-12 02:11:40 -0500 |
| commit | 0c84559372b1d4c293ac10aa2b6de6e53ebb1163 (patch) | |
| tree | 14c92db2cbe6544c0c6b742e006d4736fe8da1d7 | |
| parent | bf9f85a721d6045e631df9abe91f02d9723fcd19 (diff) | |
more housecleaning
| -rw-r--r-- | auto.Complete.go | 21 | ||||
| -rw-r--r-- | makeAutocompleteFiles.go | 41 | ||||
| -rw-r--r-- | structs.go | 1 | ||||
| -rw-r--r-- | theMagicOfAutocomplete.go | 74 |
4 files changed, 74 insertions, 63 deletions
diff --git a/auto.Complete.go b/auto.Complete.go index f53d282..19ccc30 100644 --- a/auto.Complete.go +++ b/auto.Complete.go @@ -11,17 +11,22 @@ import ( "go.wit.com/log" ) -// this is the user's application sending us strings we need to send to bash -func (pb *Auto) SendStrings(sendthis []string) { - pb.Autocomplete2(strings.Join(sendthis, " ")) -} - -// todo: move everything to this? +// deprecate func (pb *Auto) Autocomplete3(sendthis []string) { - pb.Autocomplete2(strings.Join(sendthis, " ")) + pb.SendStrings(sendthis) } +// deprecate func (pb *Auto) Autocomplete2(sendthis string) { + pb.SendStrings(strings.Split(sendthis, " ")) +} + +func (pb *Auto) SendString(sendthis string) { + pb.SendStrings(strings.Split(sendthis, " ")) +} + +// this is the user's application sending us strings we need to send to bash +func (pb *Auto) SendStrings(parts []string) { dur := pb.Duration.AsDuration() if dur < time.Millisecond*200 { pb.Debug = true @@ -42,7 +47,7 @@ func (pb *Auto) Autocomplete2(sendthis string) { } } - parts := strings.Split(sendthis, " ") + // parts := strings.Split(sendthis, " ") var all []string for _, part := range parts { var found bool diff --git a/makeAutocompleteFiles.go b/makeAutocompleteFiles.go new file mode 100644 index 0000000..647abe3 --- /dev/null +++ b/makeAutocompleteFiles.go @@ -0,0 +1,41 @@ +package prep + +// This is where the actual autocomplete happens +// lots of the fun magic is in here + +import ( + "fmt" + "os" + "path/filepath" + + "go.wit.com/lib/config" + "go.wit.com/log" +) + +// makes a autocomplete file for your command +func makeAutocompleteFiles(argname string) { + fmt.Println(makeBashCompletionText2(argname)) + + homeDir, err := os.UserHomeDir() + if err != nil { + log.Printf("# %v\n", err) + os.Exit(0) + } + filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname) + if config.Exists(filename) { + log.Fprintln(os.Stderr, "# file already exists", filename) + // os.Exit(0) + } + basedir, _ := filepath.Split(filename) + if !config.IsDir(basedir) { + os.MkdirAll(basedir, os.ModePerm) + } + + if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { + f.Write([]byte(makeBashCompletionText2(argname))) + f.Close() + } else { + log.Fprintln(os.Stderr, "# open file error", filename, err) + } + os.Exit(0) +} @@ -35,7 +35,6 @@ type AutoArgs struct { buildtime func() (string, string) // some examples pp *arg.Parser // for parsing the command line args. Yay to alexf lint! autoFunc func(*Auto) // also a function for autocomplete - // match map[string]string // maps for strings } // print out auto complete debugging info diff --git a/theMagicOfAutocomplete.go b/theMagicOfAutocomplete.go index 9de2d7f..e4407a3 100644 --- a/theMagicOfAutocomplete.go +++ b/theMagicOfAutocomplete.go @@ -4,14 +4,10 @@ package prep // lots of the fun magic is in here import ( - "fmt" "os" - "path/filepath" "time" "go.wit.com/dev/alexflint/arg" - "go.wit.com/lib/config" - "go.wit.com/log" ) func Autocomplete(dest any) *Auto { @@ -38,8 +34,6 @@ func Autocomplete(dest any) *Auto { pb.Debug = true } - // prepart["--gui"] = "andlabs gocui" - arg.Register(&argBash) flags := []string{} for _, s := range pb.Argv { @@ -62,60 +56,32 @@ func Autocomplete(dest any) *Auto { // pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()") } - if pb.IsAuto { - // myAuto.match = make(map[string]string) - // myAuto.match["--gui"] = "andlabs gocui" - // for key, val := range myAuto.match { - if pb.Last == "--gui" { - pb.Debugf("DEBUG: last=%s found --gui", pb.Last) - pb.Autocomplete2("andlabs gogui") - os.Exit(0) - } else { - // pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val) - } - // } - if myAuto.autoFunc == nil { - pb.SubCommand(pb.Argv...) - } else { - myAuto.autoFunc(pb) // run the autocomplete function the user made for their application - } - if pb.Debug { - // TODO: - // check here to see if there was any completion text sent - // if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user - } - os.Exit(0) + // not autocompleting. just return to the application + if !pb.IsAuto { + arg.Register(&argBash) + myAuto.pp = arg.MustParse(dest) + return pb } - arg.Register(&argBash) - myAuto.pp = arg.MustParse(dest) - return pb -} - -// makes a autocomplete file for your command -func makeAutocompleteFiles(argname string) { - fmt.Println(makeBashCompletionText2(argname)) - - homeDir, err := os.UserHomeDir() - if err != nil { - log.Printf("# %v\n", err) + // this is a work in progress + if pb.Last == "--gui" { + pb.Debugf("DEBUG: last=%s found --gui", pb.Last) + pb.Autocomplete2("andlabs gogui") os.Exit(0) - } - filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname) - if config.Exists(filename) { - log.Fprintln(os.Stderr, "# file already exists", filename) - // os.Exit(0) - } - basedir, _ := filepath.Split(filename) - if !config.IsDir(basedir) { - os.MkdirAll(basedir, os.ModePerm) + } else { + // pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val) } - if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { - f.Write([]byte(makeBashCompletionText2(argname))) - f.Close() + if myAuto.autoFunc == nil { + pb.SubCommand(pb.Argv...) } else { - log.Fprintln(os.Stderr, "# open file error", filename, err) + myAuto.autoFunc(pb) // run the autocomplete function the user made for their application + } + if pb.Debug { + // TODO: + // check here to see if there was any completion text sent + // if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user } os.Exit(0) + return nil } |
