package argvpb // This is where the actual autocomplete happens // lots of the fun magic is in here import ( "errors" "fmt" "os" "strings" "time" "go.wit.com/lib/ENV" "go.wit.com/lib/cobol" "go.wit.com/lib/config" durationpb "google.golang.org/protobuf/types/known/durationpb" timestamppb "google.golang.org/protobuf/types/known/timestamppb" ) func Autocomplete(dest any) *Argv { var err error me = new(AutoArgs) // todo: redo this me.pb = new(Argv) // set the start time of the binary now := time.Now() me.pb.Ctime = timestamppb.New(now) // makes sure the application has the // needed functions defined, otherwise panics verifyApplication(dest) // gets APPNAME, BUILDTIME and VERSION from the application initAppname() // parses os.Args into the protobuf me.pb.parseOsArgs() // initializes the lib/ENV library ENV.Init(me.pb.AppInfo.APPNAME, me.pb.AppInfo.VERSION, cobol.Time(me.pb.AppInfo.BUILDTIME), me.pb.Real, GoodExit, BadExit) // loads the argv autocomplete history file all := NewArgvs() fullname := config.MakeCacheFilename("argv", me.pb.AppInfo.APPNAME) err = config.LoadFromFilename(all, fullname) if err != nil { // there is no history. // todo: initialize the history file // todo: check if this is automatically done already } // try to register bash args for go-args // arg.Register(&ArgvBash) // todo: figure this out if me.guiFunc != nil { // register gui args me.guiFunc() // fmt.Println("gui init") } else { // fmt.Println("no gui init") } // user is trying to setup bash or zsh autocomplete // --bash or --zsh is the first os.Args if me.setupAuto { // --bash or --zsh was passed. try to configure bash-completion MakeAutocompleteFiles(me.pb.AppInfo.APPNAME) // never forget to run this our you will hate yourself and the choices you have made os.Exit(0) } ENV.SetGlobal("argv", "real", fmt.Sprintf("%v", me.pb.Real)) for _, a := range me.pb.Real { if strings.HasPrefix(a, "--") { ENV.SetGlobal("argv", a, "true") } } // not autocompleting. return go-arg & the application if !me.isAuto { // save the pb & history all.Clone(me.pb) errors.Join(err, all.Save()) // me.pp = arg.MustParse(dest) me.err = me.mustParseFunc() return me.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 me.last = new(Argv) me.last.Ctime = timestamppb.New(time.Now().Add(-time.Second)) for found := range all.IterAll() { me.last = found } dur := time.Since(me.last.Ctime.AsTime()) me.pb.Duration = durationpb.New(dur) if me.debug { // dump debug info me.pb.PrintDebug() all.PrintHistory() } // roll the autocomplete file if all.Len() > 15 { me.pb.Debugf("DEBUG: trim() history is over 100 len=%d vs new=%d", all.Len(), all.Len()-90) all.Argvs = all.Argvs[all.Len()-10:] // newall.Autos = all.Autos[0:10] // for _, found := range all.Autos[0:10] { // newall.Append(found) // } } // turn on debugging if duration < 200 milliseconds dur = me.pb.Duration.AsDuration() if dur < time.Millisecond*200 { me.debug = true me.pb.Fast = true // me.fastcmd = me.pb.GetCmd() if me.last.Fast { if me.pb.GetCmd() == me.last.GetCmd() { // do the smart something here } } } flags := []string{} for _, s := range me.pb.Real { if s == "--autodebug" { continue } if strings.TrimSpace(s) == "" { // skip anything blank continue } flags = append(flags, s) } if strings.HasPrefix(me.pb.Partial, "--argv") { me.pb.SendString("--argvdebug --argvhelp") me.pb.Stderr += fmt.Sprintln("argv override") me.debug = true me.pb.PrintStderrExit() } // highjack "--gui" if len(me.pb.Real) > 1 { lastarg := me.pb.Real[len(me.pb.Real)-1] // this is a work in progress if lastarg == "--gui" { me.pb.Debugf("DEBUG: real=(%v) found --gui", me.pb.Real) me.pb.PrintStderr() me.pb.SendString("andlabs gogui") os.Exit(0) } else { // me.pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", me.last, key, val) } } // use go-args to parse the structs so we can use them here // me.pp, err = arg.ParseFlags(flags, dest) if me.parseFlagsFunc == nil { me.pb.Stderr += fmt.Sprintln("argv.parseFlags() is nil") me.debug = true me.pb.PrintStderrExit() } if err := me.parseFlagsFunc(flags); err != nil { me.pb.Stderr += fmt.Sprintf("application parseFlags() err(%v)\n", err) me.pb.Stderr += fmt.Sprintln("argv.parseFlags() err. probably cmd doesn't really exist in struct") me.debug = true me.pb.PrintStderrExit() } if len(flags) == 0 { // error is normal if there are no command line args } else { if err != nil { // users has command line arguments that won't parse with go-args me.pb.Debugf("DEBUG: Parse error: %v flags%v", err, flags) } } // save now. this is near the end probably all.Clone(me.pb) errors.Join(err, all.Save()) if me.pb.Fast { if me.last.Fast { me.debug = true me.pb.PrintStderr() } else { // this means the user is pressing tab. no longer doing stderr if me.pb.GetCmd() == "" { // me.pp.WriteHelp(os.Stderr) me.writeHelpFunc() } else { // me.pp.WriteHelpForSubcommand(os.Stderr, me.pb.Cmd) me.writeHelpForSubcommandFunc(me.pb.GetCmd()) } } } else { } if me.autoFunc == nil { me.pb.SubCommand(me.pb.Real...) } else { me.autoFunc(me.pb) // run the autocomplete function the user made for their application } if me.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 }