2019-01-01 06:07:01 +03:00
|
|
|
package info
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"os"
|
2020-02-11 21:09:17 +03:00
|
|
|
"path/filepath"
|
2019-01-01 06:07:01 +03:00
|
|
|
|
2020-05-04 17:16:15 +03:00
|
|
|
"github.com/zyedidia/micro/v2/internal/config"
|
2019-01-01 06:07:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// LoadHistory attempts to load user history from configDir/buffers/history
|
|
|
|
// into the history map
|
|
|
|
// The savehistory option must be on
|
2019-01-02 06:36:12 +03:00
|
|
|
func (i *InfoBuf) LoadHistory() {
|
2019-01-01 06:07:01 +03:00
|
|
|
if config.GetGlobalOption("savehistory").(bool) {
|
2020-02-11 21:09:17 +03:00
|
|
|
file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", "history"))
|
2019-01-01 06:07:01 +03:00
|
|
|
defer file.Close()
|
|
|
|
var decodedMap map[string][]string
|
|
|
|
if err == nil {
|
|
|
|
decoder := gob.NewDecoder(file)
|
|
|
|
err = decoder.Decode(&decodedMap)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
i.Error("Error loading history:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if decodedMap != nil {
|
|
|
|
i.History = decodedMap
|
|
|
|
} else {
|
|
|
|
i.History = make(map[string][]string)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i.History = make(map[string][]string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveHistory saves the user's command history to configDir/buffers/history
|
|
|
|
// only if the savehistory option is on
|
2019-01-02 06:36:12 +03:00
|
|
|
func (i *InfoBuf) SaveHistory() {
|
2019-01-01 06:07:01 +03:00
|
|
|
if config.GetGlobalOption("savehistory").(bool) {
|
|
|
|
// Don't save history past 100
|
|
|
|
for k, v := range i.History {
|
|
|
|
if len(v) > 100 {
|
|
|
|
i.History[k] = v[len(i.History[k])-100:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:09:17 +03:00
|
|
|
file, err := os.Create(filepath.Join(config.ConfigDir, "buffers", "history"))
|
2019-01-01 06:07:01 +03:00
|
|
|
defer file.Close()
|
|
|
|
if err == nil {
|
|
|
|
encoder := gob.NewEncoder(file)
|
|
|
|
|
|
|
|
err = encoder.Encode(i.History)
|
|
|
|
if err != nil {
|
|
|
|
i.Error("Error saving history:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 01:07:28 +03:00
|
|
|
|
|
|
|
// UpHistory fetches the previous item in the history
|
|
|
|
func (i *InfoBuf) UpHistory(history []string) {
|
2019-06-17 18:58:39 +03:00
|
|
|
if i.HistoryNum > 0 && i.HasPrompt && !i.HasYN {
|
2019-01-04 01:07:28 +03:00
|
|
|
i.HistoryNum--
|
2019-01-17 06:32:33 +03:00
|
|
|
i.Replace(i.Start(), i.End(), history[i.HistoryNum])
|
2019-01-04 01:07:28 +03:00
|
|
|
i.Buffer.GetActiveCursor().GotoLoc(i.End())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownHistory fetches the next item in the history
|
|
|
|
func (i *InfoBuf) DownHistory(history []string) {
|
2019-06-17 18:58:39 +03:00
|
|
|
if i.HistoryNum < len(history)-1 && i.HasPrompt && !i.HasYN {
|
2019-01-04 01:07:28 +03:00
|
|
|
i.HistoryNum++
|
2019-01-17 06:32:33 +03:00
|
|
|
i.Replace(i.Start(), i.End(), history[i.HistoryNum])
|
2019-01-04 01:07:28 +03:00
|
|
|
i.Buffer.GetActiveCursor().GotoLoc(i.End())
|
|
|
|
}
|
|
|
|
}
|