diff options
Diffstat (limited to 'theMagicOfAutocomplete.go')
| -rw-r--r-- | theMagicOfAutocomplete.go | 99 |
1 files changed, 59 insertions, 40 deletions
diff --git a/theMagicOfAutocomplete.go b/theMagicOfAutocomplete.go index 85b89b0..13cf020 100644 --- a/theMagicOfAutocomplete.go +++ b/theMagicOfAutocomplete.go @@ -25,6 +25,7 @@ import ( func Autocomplete(dest any) *Argv { me = new(AutoArgs) // todo: redo this me.pb = new(Argv) + PB = me.pb me.pb.Uuid = uuid.New().String() // set the start time of the binary @@ -74,18 +75,19 @@ func Autocomplete(dest any) *Argv { } } - if me.isAuto { - // do autocomplete and exit + if !me.isAuto { + // not autocompleting. return to the application + // save the pb & history savePB() - doAutocomplete() - panic("blah") + // me.pp = arg.MustParse(dest) + me.Err = errors.Join(me.Err, me.mustParseFunc()) + return me.pb } - // not autocompleting. return to the application - // save the pb & history - savePB() - // me.pp = arg.MustParse(dest) - me.Err = errors.Join(me.Err, me.mustParseFunc()) + prepareStdout() + // print to Stdout & Stderr + saveAndExit() + // never gets here return me.pb } @@ -93,17 +95,7 @@ func Autocomplete(dest any) *Argv { // // everything sent to STDOUT and STDERR matters past this point // any "junk" output, hidden fmt(), print() or log() statements are bad -func doAutocomplete() { - if me.last.Fast { - // user is probably holding down the <TAB> key - me.debug = true - // me.fastcmd = me.pb.GetCmd() - if me.pb.GetCmd() != me.last.GetCmd() { - // do the smart something here - // probably debug == true && printStderr() - } - } - +func prepareStdout() { if me.debug { // add an initial debug line me.pb.PrintDebug(me.last.GetCmd()) @@ -113,8 +105,7 @@ func doAutocomplete() { me.pb.SendString("--argvdebug --argvhelp") me.pb.Stderr += fmt.Sprintln("argv override") me.debug = true - me.pb.PrintStderr() - saveAndExit() + return } // highjack "--gui" @@ -123,9 +114,8 @@ func doAutocomplete() { // 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") - saveAndExit() + return } else { // me.pb.Debugf("DEBUG: NO MATCH last='%s' found key '%s' = %s", last, key, val) } @@ -136,19 +126,15 @@ func doAutocomplete() { if me.parseFlagsFunc == nil { me.pb.Stderr += fmt.Sprintln("argv.parseFlags() is nil") me.debug = true - me.pb.PrintStderr() - me.pb.PrintStdout() - saveAndExit() + return } - if err := me.parseFlagsFunc(me.pb.Real); err != nil { + if err := me.parseFlagsFunc(); 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.all.PrintHistory(me.last.GetCmd()) me.debug = true - me.pb.PrintStderr() - me.pb.PrintStdout() - saveAndExit() + return } if len(me.pb.Real) == 0 { @@ -161,15 +147,20 @@ func doAutocomplete() { } me.pb.HelpCounter = me.last.HelpCounter + /* + if me.last.Fast { + // user is probably holding down the <TAB> key + // me.fastcmd = me.pb.GetCmd() + if me.pb.GetCmd() != me.last.GetCmd() { + // do the smart something here + // probably debug == true && printStderr() + } + } + */ + if me.pb.Fast { if me.last.Fast { - me.debug = true me.pb.HelpCounter = me.last.HelpCounter + 1 - if me.pb.HelpCounter < 3 { - me.pb.PrintStderr() - } else { - saveAndExit() - } } else { // this means the user is pressing tab. no longer doing stderr if me.pb.GetCmd() == "" { @@ -177,7 +168,7 @@ func doAutocomplete() { me.writeHelpFunc() } else { // me.pp.WriteHelpForSubcommand(Stderr, me.pb.Cmd) - me.writeHelpForSubcommandFunc(me.pb.GetCmd()) + me.writeHelpForSubcommandFunc() } me.pb.HelpCounter = 0 } @@ -193,7 +184,7 @@ func doAutocomplete() { // if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user } } - saveAndExit() + return } func savePB() { @@ -206,6 +197,17 @@ func savePB() { } func saveAndExit() { + if me.debug { + me.all.PrintHistory(me.last.GetCmd()) + me.pb.PrintStderr() + } else { + if me.pb.Fast { + if me.pb.HelpCounter < 3 { + me.pb.PrintStderr() + } + } + } + me.pb.PrintStdout() savePB() os.Exit(0) } @@ -230,7 +232,9 @@ func examineArgvHistory() { panic("argvpb.Load() history file failed") } if me.debug { + // use this if you are having trouble debugging this code // me.all.PrintHistory("EARLY") + // me.pb.PrintStderr() } // roll the autocomplete file maxsize := 17 @@ -244,14 +248,28 @@ func examineArgvHistory() { } // drops nil entries from the history + // even gets dup uuids. don't have time to figure this out now + // need to launch the WIT private cloud product // todo: figure out why nil values are happening + // a facinating PITA this is counter := 0 + uuidmap := make(map[string]*Argv) for pb := range me.all.IterAll() { counter += 1 if pb.Ctime == nil { me.all.Delete(pb) continue } + if pb.Uuid == "" { + me.all.Delete(pb) + continue + } + if _, ok := uuidmap[pb.Uuid]; ok { + // already have this uuid, keep the oldest one + me.all.Delete(pb) + continue + } + uuidmap[pb.Uuid] = pb // if me.debug { // hist := fmt.Sprintf("HISTNIL(%d)", counter) // pb.PrintDebugNew(hist, "too soon") @@ -282,8 +300,9 @@ func examineArgvHistory() { // do the smart something here if me.pb.GetCmd() == me.last.GetCmd() { // turn on debugging if duration < 200 milliseconds - // me.debug = false } else { + // reset help counter + me.pb.HelpCounter = 0 } } } |
