]> git.lizzy.rs Git - micro.git/commitdiff
AddToHistory function for plugins (#1830)
authorDmitry Maluka <dmitrymaluka@gmail.com>
Sun, 23 Aug 2020 19:47:14 +0000 (21:47 +0200)
committerGitHub <noreply@github.com>
Sun, 23 Aug 2020 19:47:14 +0000 (15:47 -0400)
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

index 03391b74f657a782e5cf6ed68f226d6103350e10..193185ca3f00998817f4576a719331f1177d7798 100644 (file)
@@ -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 {