package prep // This is where the actual autocomplete happens // lots of the fun magic is in here import ( "errors" "os" "time" "go.wit.com/dev/alexflint/arg" "go.wit.com/lib/config" durationpb "google.golang.org/protobuf/types/known/durationpb" timestamppb "google.golang.org/protobuf/types/known/timestamppb" ) // TODO: rename 'all' to 'pb' and 'pb' to 'pbnew' or 'pbcur' func Autocomplete(dest any) *Auto { myAuto = new(AutoArgs) // todo: redo this findAppInfo(dest) // parses back to main() for argv info if myAuto.guiFunc != nil { // register gui args myAuto.guiFunc() // log.Info("gui init") } else { // log.Info("no gui init") } // parses os.Args into a protobuf pb := parseArgv(myAuto.ARGNAME) // the argv history all := NewAutos() // initializes the application config file config.Init(myAuto.ARGNAME, myAuto.VERSION, myAuto.BUILDTIME, pb.Argv) // loads the autocomplete history file err := config.LoadCache(all, "argv", myAuto.ARGNAME) // if err != nil { // there is no history. // todo: initialize the history file } // set the start time of the binary now := time.Now() pb.Ctime = timestamppb.New(now) // try to register bash args for go-args arg.Register(&argBash) // arg.Register(&argGui) // user is trying to setup bash or zsh autocomplete // --bash or --zsh is the first os.Args if pb.SetupAuto { // --bash was passed. try to configure bash-completion MakeAutocompleteFiles(myAuto.ARGNAME) // never forget to run this our you will hate yourself and the choices you have made os.Exit(0) } // not autocompleting. return go-arg & the application if !pb.IsAuto { // save the pb & history all.Clone(pb) errors.Join(err, all.Save()) myAuto.pp = arg.MustParse(dest) myAuto.err = err return pb } // EVERYTHING PAST HERE IS FOR AUTOCOMPLETE // everything sent to STDOUT and STDERR matters past this point // set the duration since the last auto complete // find the last entry. this is dumb way to do it last := new(Auto) last.Ctime = timestamppb.New(time.Now().Add(-time.Second)) for found := range all.IterAll() { last = found } 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) all.Autos = all.Autos[all.Len()-10:] // newall.Autos = all.Autos[0:10] // for _, found := range all.Autos[0:10] { // newall.Append(found) // } } pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d config.Save().err=%v", all.Filename, all.Len(), err) // turn on debugging if duration < 200 milliseconds dur = pb.Duration.AsDuration() if dur < time.Millisecond*200 { pb.Debug = true pb.Fast = true pb.Fastcmd = pb.Cmd if last.Fast { if pb.Fastcmd == last.Fastcmd { // do the smart something here } } } flags := []string{} for _, s := range pb.Argv { if s == "--autodebug" { continue } flags = append(flags, s) } // use go-args to parse the structs so we can use them here myAuto.pp, err = arg.ParseFlags(flags, dest) if err != nil { // users has command line arguments that won't parse with go-args pb.Debugf("DEBUG: Parse error: %v flags(%v)", err, flags) } if myAuto.pp == nil { pb.Debugf("DEBUG: myAuto.pp == nil after ParseFlags()") } else { // pb.Debugf("DEBUG: myAuto.pp is ok after ParseFlags()") } // 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) pb.Autocomplete2("andlabs gogui") os.Exit(0) } else { // pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", pb.Last, key, val) } if pb.Fast { if last.Fast { os.Exit(0) } else { // this means the user is pressing tab. no longer doing stderr if pb.Cmd == "" { myAuto.pp.WriteHelp(os.Stderr) } else { myAuto.pp.WriteHelpForSubcommand(os.Stderr, pb.Cmd) } } } else { } 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) return nil }