From c9b0451a33bb996b6b7e6610e84f5c105e0913ed Mon Sep 17 00:00:00 2001 From: Dmitry Maluka Date: Sun, 23 Aug 2020 21:47:14 +0200 Subject: [PATCH] AddToHistory function for plugins (#1830) Add InfoBuf's method AddToHistory function which adds a new item to the history for the prompt type `ptype`. This function is not used by micro itself. It is useful for plugins which add their own items to the history, bypassing the infobar command line. --- internal/info/history.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/info/history.go b/internal/info/history.go index 03391b74..193185ca 100644 --- a/internal/info/history.go +++ b/internal/info/history.go @@ -61,6 +61,30 @@ func (i *InfoBuf) SaveHistory() { } } +// AddToHistory adds a new item to the history for the prompt type `ptype`. +// This function is not used by micro itself. It is useful for plugins +// which add their own items to the history, bypassing the infobar command line. +func (i *InfoBuf) AddToHistory(ptype string, item string) { + if i.HasPrompt && i.PromptType == ptype { + return + } + + if _, ok := i.History[ptype]; !ok { + i.History[ptype] = []string{item} + } else { + i.History[ptype] = append(i.History[ptype], item) + + // avoid duplicates + h := i.History[ptype] + for j := len(h) - 2; j >= 0; j-- { + if h[j] == h[len(h)-1] { + i.History[ptype] = append(h[:j], h[j+1:]...) + break + } + } + } +} + // UpHistory fetches the previous item in the history func (i *InfoBuf) UpHistory(history []string) { if i.HistoryNum > 0 && i.HasPrompt && !i.HasYN {