summaryrefslogtreecommitdiff
path: root/theMagicOfAutocomplete.go
diff options
context:
space:
mode:
Diffstat (limited to 'theMagicOfAutocomplete.go')
-rw-r--r--theMagicOfAutocomplete.go74
1 files changed, 35 insertions, 39 deletions
diff --git a/theMagicOfAutocomplete.go b/theMagicOfAutocomplete.go
index fa392a1..8d78c62 100644
--- a/theMagicOfAutocomplete.go
+++ b/theMagicOfAutocomplete.go
@@ -15,38 +15,42 @@ import (
)
func Autocomplete(dest any) *Auto {
- myAuto = new(AutoArgs) // todo: redo this
- findAppInfo(dest) // parses back to main() for argv info
+ myAuto = new(AutoArgs) // todo: redo this
+ findAppInfo(dest) // parses back to main() for argv info
+
+ // load the argv history from the protobuf file
+ all := NewAutos()
+ err := config.LoadCache(all, "argv", myAuto.appName) // loads ~/.cache/argv/forge.pb
+ if err != nil {
+ // there is no history. do something here(?)
+ }
pb := parseArgv(myAuto.appName) // parses os.Args into a protobuf
- // set the start time of the binary
- now := time.Now()
- pb.Ctime = timestamppb.New(now)
+ // not autocompleting. return go-arg & the application
+ if !pb.IsAuto {
+ // save the pb & history
+ all.Clone(pb)
+ errors.Join(err, all.Save())
+
+ arg.Register(&argBash)
+ myAuto.pp = arg.MustParse(dest)
+ myAuto.err = err
+ return pb
+ }
+ // user is trying to setup bash or zsh autocomplete
if pb.SetupAuto {
// --bash was passed. try to configure bash-completion
MakeAutocompleteFiles(myAuto.appName)
os.Exit(0)
}
- if pb.Debug {
- // dump debug info
- pb.PrintDebug()
- }
-
- // all, err := pb.getHistoryPB() // read in the history protobuf file
- // func (newpb *Auto) getHistoryPB() (*Autos, error) {
- all := NewAutos()
- err := config.LoadCache(all, "argv", pb.Argname) // loads ~/.cache/argv/forge.pb
- if err != nil {
- // there is no history
- }
+ // EVERYTHING PAST HERE IS FOR AUTOCOMPLETE
+ // everything sent to STDOUT and STDERR matters past this point
- if all.Len() == 0 {
- // first time
- all.Clone(pb)
- err = all.Save()
- }
+ // set the start time of the binary
+ now := time.Now()
+ pb.Ctime = timestamppb.New(now)
// set the duration since the last auto complete
// find the last entry. this is dumb way to do it
@@ -57,6 +61,12 @@ func Autocomplete(dest any) *Auto {
dur := time.Since(last.Ctime.AsTime())
pb.Duration = durationpb.New(dur)
+ if pb.Debug {
+ // dump debug info
+ pb.PrintDebug()
+ all.PrintHistory()
+ }
+
// roll the autocomplete file
if all.Len() > 15 {
pb.Debugf("DEBUG: trim() history is over 100 len=%d vs new=%d", all.Len(), all.Len()-90)
@@ -67,17 +77,7 @@ func Autocomplete(dest any) *Auto {
// }
}
- // save the history
- all.Clone(pb)
- errors.Join(err, all.Save())
-
pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d config.Save().err=%v", all.Filename, all.Len(), err)
- // return all, err
- //}
-
- if pb.Debug {
- all.PrintHistory()
- }
// turn on debugging if duration < 200 milliseconds
dur = pb.Duration.AsDuration()
@@ -107,13 +107,9 @@ func Autocomplete(dest any) *Auto {
// pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()")
}
- // not autocompleting. just return to the application
- if !pb.IsAuto {
- arg.Register(&argBash)
- myAuto.pp = arg.MustParse(dest)
- return pb
- }
-
+ // save now. this is near the end probably
+ all.Clone(pb)
+ errors.Join(err, all.Save())
// this is a work in progress
if pb.Last == "--gui" {
pb.Debugf("DEBUG: last=%s found --gui", pb.Last)