summaryrefslogtreecommitdiff
path: root/history.go
blob: c048c1c2a7585e7b2e174d1efd181f7f73c06b38 (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
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 (
	"time"

	"go.wit.com/lib/config"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

func (newpb *Auto) getHistoryPB() error {
	all := NewAutos()
	err := config.LoadCache(all, "argv", newpb.Argname) // loads ~/.cache/argv/forge.pb
	if err != nil {
		// newpb.History = false
		return err
	}

	if all.Len() == 0 {
		// first time
		all.Clone(newpb)
		err = all.Save()
	}

	if newpb.Debug {
		all.PrintHistory()
	}

	var last *Auto
	// find the last entry. this is dumb way to do it
	for found := range all.IterAll() {
		last = found
	}

	// set the duration since the last auto complete
	dur := time.Since(last.Ctime.AsTime())
	newpb.Duration = durationpb.New(dur)

	// roll the autocomplete file
	if all.Len() > 15 {
		newpb.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)
		// }
	}

	all.Clone(newpb)
	err = all.Save()

	newpb.Debugf("WRITE DEBUG: write PB='%s' len(pb)=%d config.Save().err=%v", all.Filename, all.Len(), err)
	return err
}