summaryrefslogtreecommitdiff
path: root/history.go
blob: 74dc823d672902e70b134bbd12d36e1677ab192a (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
59
60
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"
)

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
}