aboutsummaryrefslogtreecommitdiffstats
path: root/commands/history.go
diff options
context:
space:
mode:
authorGalen Abell <galen@galenabell.com>2019-07-23 12:52:33 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-26 14:29:34 -0400
commit8635c70fda20b91f97c42f4e23e97bc01a14a89d (patch)
treeea70a40f7617782ca28060965ad253fa0e686161 /commands/history.go
parent67fb0938a66605a0b6a837005804637b348b250d (diff)
downloadaerc-8635c70fda20b91f97c42f4e23e97bc01a14a89d.tar.gz
Add command history and cycling
Aerc will keep track of the previous 1000 commands, which the user can cycle through using the arrow keys while in the ex-line. Pressing up will move backwards in history while pressing down will move forward.
Diffstat (limited to 'commands/history.go')
-rw-r--r--commands/history.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/commands/history.go b/commands/history.go
new file mode 100644
index 00000000..77bb1553
--- /dev/null
+++ b/commands/history.go
@@ -0,0 +1,62 @@
+package commands
+
+type cmdHistory struct {
+ // rolling buffer of prior commands
+ //
+ // most recent command is at the end of the list,
+ // least recent is index 0
+ cmdList []string
+
+ // current placement in list
+ current int
+}
+
+// number of commands to keep in history
+const cmdLimit = 1000
+
+// CmdHistory is the history of executed commands
+var CmdHistory = cmdHistory{}
+
+func (h *cmdHistory) Add(cmd string) {
+ // if we're at cap, cut off the first element
+ if len(h.cmdList) >= cmdLimit {
+ h.cmdList = h.cmdList[1:]
+ }
+
+ h.cmdList = append(h.cmdList, cmd)
+
+ // whenever we add a new command, reset the current
+ // pointer to the "beginning" of the list
+ h.Reset()
+}
+
+// Prev returns the previous command in history.
+// Since the list is reverse-order, this will return elements
+// increasingly towards index 0.
+func (h *cmdHistory) Prev() string {
+ if h.current <= 0 || len(h.cmdList) == 0 {
+ h.current = -1
+ return "(Already at beginning)"
+ }
+ h.current--
+
+ return h.cmdList[h.current]
+}
+
+// Next returns the next command in history.
+// Since the list is reverse-order, this will return elements
+// increasingly towards index len(cmdList).
+func (h *cmdHistory) Next() string {
+ if h.current >= len(h.cmdList)-1 || len(h.cmdList) == 0 {
+ h.current = len(h.cmdList)
+ return "(Already at end)"
+ }
+ h.current++
+
+ return h.cmdList[h.current]
+}
+
+// Reset the current pointer to the beginning of history.
+func (h *cmdHistory) Reset() {
+ h.current = len(h.cmdList)
+}