summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-12 06:51:14 -0500
committerJeff Carr <[email protected]>2025-10-12 06:51:14 -0500
commit249481dc4bcc50782b094138b1999575c38b6a34 (patch)
tree432b50ad760ac5f4fe391c914991ef77b1941aa5
parenta2c9d4fed68598ebab7a174d51a44af43a7ff768 (diff)
more house cleaning. starting to look better
-rw-r--r--structs.go1
-rw-r--r--theMagicOfAutocomplete.go74
2 files changed, 36 insertions, 39 deletions
diff --git a/structs.go b/structs.go
index 9d47309..04e011d 100644
--- a/structs.go
+++ b/structs.go
@@ -32,6 +32,7 @@ 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
+ err error // store any errors from argv
}
// returns the last command (is blank if the current arg is not blank)
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)