summaryrefslogtreecommitdiff
path: root/history.go
blob: a2956df5e86e84b43e2f015c27290bec5ccfdd60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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/<appname>.pb

import (
	"fmt"
	"time"

	"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 (pb *Auto) getHistoryPB() error {
	all := NewAutos()
	err := config.LoadCache(all, "argv", pb.Argname) // loads ~/.cache/argv/forge.pb

	var last *Auto
	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'", 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
		}
	}

	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)
	}

	// this is the start time of the binary
	now := time.Now()
	pb.Ctime = timestamppb.New(now)
	all.Append(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
}