Debian Codegraph @

commands/history.go

raw
1 package commands
2
3 import (
4 "bufio"
5 "bytes"
6 "fmt"
7 "io"
8 "os"
9 "slices"
10 "strings"
11 "sync"
12
13 "git.sr.ht/~rjarry/aerc/lib/log"
14 "git.sr.ht/~rjarry/aerc/lib/xdg"
15 )
16
17 type cmdHistory struct {
18 // rolling buffer of prior commands
19 //
20 // most recent command is at the end of the list,
21 // least recent is index 0
22 cmdList []string
23
24 // current placement in list
25 current int
26
27 // initialize history storage
28 initHistfile sync.Once
29 histfile io.ReadWriter
30 }
31
32 // number of commands to keep in history
33 const cmdLimit = 1000
34
35 // CmdHistory is the history of executed commands
36 var CmdHistory = cmdHistory{}
37
38 // Last executed command that was not :repeat
39 func (h *cmdHistory) Last() string {
40 h.initHistfile.Do(h.initialize)
41 var lastCmd string
42 for _, cmd := range slices.Backward(h.cmdList) {
43 // NOTE: skipping repeat commands avoids getting locked in an
44 // infinite loop when calling repeat
45 if strings.TrimSpace(cmd) != "repeat" {
46 lastCmd = cmd
47 break
48 }
49 }
50 return lastCmd
51 }
52
53 func (h *cmdHistory) Add(cmd string) {
54 h.initHistfile.Do(h.initialize)
55
56 // if we're at cap, cut off the first element
57 if len(h.cmdList) >= cmdLimit {
58 h.cmdList = h.cmdList[1:]
59 }
60
61 if len(h.cmdList) == 0 || h.cmdList[len(h.cmdList)-1] != cmd {
62 h.cmdList = append(h.cmdList, cmd)
63
64 h.writeHistory()
65 }
66
67 // whenever we add a new command, reset the current
68 // pointer to the "beginning" of the list
69 h.Reset()
70 }
71
72 // Prev returns the previous command in history.
73 // Since the list is reverse-order, this will return elements
74 // increasingly towards index 0.
75 func (h *cmdHistory) Prev() string {
76 h.initHistfile.Do(h.initialize)
77
78 if h.current <= 0 || len(h.cmdList) == 0 {
79 h.current = -1
80 return "(Already at beginning)"
81 }
82 h.current--
83
84 return h.cmdList[h.current]
85 }
86
87 // Next returns the next command in history.
88 // Since the list is reverse-order, this will return elements
89 // increasingly towards index len(cmdList).
90 func (h *cmdHistory) Next() string {
91 h.initHistfile.Do(h.initialize)
92
93 if h.current >= len(h.cmdList)-1 || len(h.cmdList) == 0 {
94 h.current = len(h.cmdList)
95 return "(Already at end)"
96 }
97 h.current++
98
99 return h.cmdList[h.current]
100 }
101
102 // Reset the current pointer to the beginning of history.
103 func (h *cmdHistory) Reset() {
104 h.current = len(h.cmdList)
105 }
106
107 func (h *cmdHistory) initialize() {
108 var err error
109 openFlags := os.O_RDWR | os.O_EXCL
110
111 histPath := xdg.StatePath("aerc", "history")
112 if _, err := os.Stat(histPath); os.IsNotExist(err) {
113 _ = os.MkdirAll(xdg.StatePath("aerc"), 0o700) // caught by OpenFile
114 openFlags |= os.O_CREATE
115 }
116
117 // O_EXCL to make sure that only one aerc writes to the file
118 h.histfile, err = os.OpenFile(
119 histPath,
120 openFlags,
121 0o600,
122 )
123 if err != nil {
124 log.Errorf("failed to open history file: %v", err)
125 // basically mirror the old behavior
126 h.histfile = bytes.NewBuffer([]byte{})
127 return
128 }
129
130 s := bufio.NewScanner(h.histfile)
131
132 for s.Scan() {
133 h.cmdList = append(h.cmdList, s.Text())
134 }
135
136 h.Reset()
137 }
138
139 func (h *cmdHistory) writeHistory() {
140 if fh, ok := h.histfile.(*os.File); ok {
141 err := fh.Truncate(0)
142 if err != nil {
143 // if we can't delete it, don't break it.
144 return
145 }
146 _, err = fh.Seek(0, io.SeekStart)
147 if err != nil {
148 // if we can't delete it, don't break it.
149 return
150 }
151 for _, entry := range h.cmdList {
152 fmt.Fprintln(fh, entry)
153 }
154
155 fh.Sync() //nolint:errcheck // if your computer can't sync you're in bigger trouble
156 }
157 }