package prep // This is where the actual autocomplete happens // lots of the fun magic is in here import ( "os" "time" "go.wit.com/dev/alexflint/arg" ) func Autocomplete(dest any) *Auto { myAuto = new(AutoArgs) findAppInfo(dest) // parses back to main() for argv info pb := parseArgv(myAuto.appName) // parses os.Args into a protobuf 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() } pb.getHistoryPB() // read in the history protobuf file // turn on debugging if duration < 200 milliseconds dur := pb.Duration.AsDuration() if dur < time.Millisecond*200 { pb.Debug = true pb.Fast = true } arg.Register(&argBash) flags := []string{} for _, s := range pb.Argv { if s == "--autodebug" { continue } flags = append(flags, s) } // pb.Debug = true // pb.Debugf("DEBUG: MustParse(%v)", flags) var err error myAuto.pp, err = arg.ParseFlags(flags, dest) if err != nil { pb.Debugf("DEBUG: Parse error: %v", err) } if myAuto.pp == nil { pb.Debugf("DEBUG: myAuto.pp == nil after ParseFlags()") } else { // 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 } // 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 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 }