]> git.lizzy.rs Git - micro.git/blob - cmd/micro/info/history.go
Fix infobar prompt
[micro.git] / cmd / micro / info / history.go
1 package info
2
3 import (
4         "encoding/gob"
5         "os"
6
7         "github.com/zyedidia/micro/cmd/micro/config"
8 )
9
10 // LoadHistory attempts to load user history from configDir/buffers/history
11 // into the history map
12 // The savehistory option must be on
13 func (i *InfoBuf) LoadHistory() {
14         if config.GetGlobalOption("savehistory").(bool) {
15                 file, err := os.Open(config.ConfigDir + "/buffers/history")
16                 defer file.Close()
17                 var decodedMap map[string][]string
18                 if err == nil {
19                         decoder := gob.NewDecoder(file)
20                         err = decoder.Decode(&decodedMap)
21
22                         if err != nil {
23                                 i.Error("Error loading history:", err)
24                                 return
25                         }
26                 }
27
28                 if decodedMap != nil {
29                         i.History = decodedMap
30                 } else {
31                         i.History = make(map[string][]string)
32                 }
33         } else {
34                 i.History = make(map[string][]string)
35         }
36 }
37
38 // SaveHistory saves the user's command history to configDir/buffers/history
39 // only if the savehistory option is on
40 func (i *InfoBuf) SaveHistory() {
41         if config.GetGlobalOption("savehistory").(bool) {
42                 // Don't save history past 100
43                 for k, v := range i.History {
44                         if len(v) > 100 {
45                                 i.History[k] = v[len(i.History[k])-100:]
46                         }
47                 }
48
49                 file, err := os.Create(config.ConfigDir + "/buffers/history")
50                 defer file.Close()
51                 if err == nil {
52                         encoder := gob.NewEncoder(file)
53
54                         err = encoder.Encode(i.History)
55                         if err != nil {
56                                 i.Error("Error saving history:", err)
57                                 return
58                         }
59                 }
60         }
61 }