package prep // essentially .bash_history, but in a protobuf and for any shell // An elegant weapon... for a more civilized age. // stores files ./cache/argv/.pb import ( "fmt" "time" "go.wit.com/lib/cobol" "go.wit.com/lib/config" durationpb "google.golang.org/protobuf/types/known/durationpb" ) func (pb *Auto) getHistoryPB() error { all := NewAutos() err := config.LoadCache(all, "argv", pb.Argname) // loads ~/.cache/argv/forge.pb if err != nil { // pb.History = false return err } var last *Auto // find the last entry & print out debugging history for found := range all.IterAll() { cmd := fmt.Sprintf("cmd='%s'", found.Cmd) arglast := fmt.Sprintf("last='%s'", found.Last) partial := fmt.Sprintf("p='%s'", found.Partial) dur := time.Since(found.Ctime.AsTime()) age := fmt.Sprintf("age=(%s)", cobol.FormatDuration(dur)) pb.Debugf("AUTO HISTORY: %s %-18.18s %-18.18s %-12.12s argv='%v' goargs='%v'", age, cmd, arglast, partial, found.Argv, found.Goargs) last = found } // set the duration since the last auto complete dur := time.Since(last.Ctime.AsTime()) pb.Duration = durationpb.New(dur) // 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) // } } // need this for the first time the user runs autocomplete if last == nil { last = new(Auto) } all.Clone(pb) err = all.Save() pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d config.Save().err=%v", all.Filename, all.Len(), err) return err }