summaryrefslogtreecommitdiff
path: root/doHandlePB.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-12 02:23:55 -0500
committerJeff Carr <[email protected]>2025-10-12 02:23:55 -0500
commit23b1865b5d9b879b543116beaf68d80fe76c5534 (patch)
treebe609ce8821335b651ca0b6ec14409f132a68fd7 /doHandlePB.go
parent90524977ef3603b50f3744884b6906d2216e1b28 (diff)
essentially a modern .bash_history
Diffstat (limited to 'doHandlePB.go')
-rw-r--r--doHandlePB.go83
1 files changed, 0 insertions, 83 deletions
diff --git a/doHandlePB.go b/doHandlePB.go
deleted file mode 100644
index 1a9f044..0000000
--- a/doHandlePB.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package prep
-
-// essentially .bash_history, but in a protobuf and for any shell
-// stores files ./cache/argv/<appname>.pb
-
-import (
- "fmt"
- "os"
- "path/filepath"
- "time"
-
- "go.wit.com/lib/config"
- durationpb "google.golang.org/protobuf/types/known/durationpb"
- timestamppb "google.golang.org/protobuf/types/known/timestamppb"
-)
-
-func (pb *Auto) doHandlePB() error {
- return pb.readConfigPB()
-}
-
-// makes a bash autocomplete file for your command
-func (pb *Auto) readConfigPB() error {
- cacheDir, err := os.UserCacheDir()
- if err != nil {
- return err
- }
- basedir := filepath.Join(cacheDir, "argv")
- os.MkdirAll(basedir, os.ModePerm)
- fullname := filepath.Join(basedir, pb.Argname+".pb")
-
- all := NewAutos()
- var last *Auto
- data, err := os.ReadFile(fullname)
- if err == nil {
- err = all.Unmarshal(data)
- if err == nil {
- for found := range all.IterAll() {
- dur := time.Since(found.Ctime.AsTime())
- pb.Duration = durationpb.New(dur)
- // found.PrintDebug()
- cmd := fmt.Sprintf("cmd='%s'", found.Cmd)
- arglast := fmt.Sprintf("last='%s'", found.Last)
- partial := fmt.Sprintf("p='%s'", found.Partial)
- age := fmt.Sprintf("age='%-6.6s'", config.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
- }
- }
- }
-
- 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)
- }
-
- now := time.Now()
- pb.Ctime = timestamppb.New(now)
- duration := time.Since(last.Ctime.AsTime())
- all.Append(pb)
-
- data, err = all.Marshal()
- if err != nil {
- return err
- }
-
- f, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
- defer f.Close()
- if err != nil {
- return err
- }
- _, err = f.Write(data)
- pb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d len(data)=%d dur=%v err=%v", fullname, all.Len(), len(data), duration, err)
- return err
-}