forked from tiborvass/uniline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
60 lines (53 loc) · 1.31 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package uniline
import (
"bufio"
"fmt"
"os"
)
// ClearHistory Clears history.
func (s *Scanner) ClearHistory() {
s.history = history{}
}
// AddToHistory adds a string line to history
func (s *Scanner) AddToHistory(line string) {
s.history.tmp = append(s.history.tmp, line)
s.history.saved = append(s.history.saved, line)
}
// SaveHistory saves the current history to a file specified by filename.
func (s *Scanner) SaveHistory(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for _, line := range s.history.tmp[:len(s.history.tmp)-1] {
if _, err := fmt.Fprintln(f, line); err != nil {
return err
}
}
return nil
}
// LoadHistory loads history from a file specified by filename.
func (s *Scanner) LoadHistory(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
// tmp = saved = loaded History
s.history.saved = lines
s.history.tmp = make([]string, len(s.history.saved))
copy(s.history.tmp, s.history.saved)
// add current line
s.history.tmp = append(s.history.tmp, s.buf.String())
s.history.index = len(s.history.tmp) - 1
return nil
}