fix: only add to history of different

if the last item in history is the same as the one being added, skip it.
this reduces the number of history entries. the behaviour is similar to
how most shells maintain history
This commit is contained in:
Michael Yang 2024-12-21 00:08:12 -08:00
parent cb40d60469
commit 7ccdc98a5f

View File

@ -81,6 +81,7 @@ func (h *History) Init() error {
} }
func (h *History) Add(s string) { func (h *History) Add(s string) {
if latest, _ := h.Buf.Get(h.Size() - 1); latest != s {
h.Buf.Add(s) h.Buf.Add(s)
h.Compact() h.Compact()
h.Pos = h.Size() h.Pos = h.Size()
@ -88,6 +89,7 @@ func (h *History) Add(s string) {
_ = h.Save() _ = h.Save()
} }
} }
}
func (h *History) Compact() { func (h *History) Compact() {
s := h.Buf.Size() s := h.Buf.Size()