summaryrefslogtreecommitdiffstats
path: root/src/history.go
blob: 45728d40d9fb76e357e34aa8d7faa0e39cb07869 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package fzf

import (
	"errors"
	"io/ioutil"
	"os"
	"strings"
)

// History struct represents input history
type History struct {
	path     string
	lines    []string
	modified map[int]string
	maxSize  int
	cursor   int
}

// NewHistory returns the pointer to a new History struct
func NewHistory(path string, maxSize int) (*History, error) {
	fmtError := func(e error) error {
		if os.IsPermission(e) {
			return errors.New("permission denied: " + path)
		}
		return errors.New("invalid history file: " + e.Error())
	}

	// Read history file
	data, err := ioutil.ReadFile(path)
	if err != nil {
		// If it doesn't exist, check if we can create a file with the name
		if os.IsNotExist(err) {
			data = []byte{}
			if err := ioutil.WriteFile(path, data, 0600); err != nil {
				return nil, fmtError(err)
			}
		} else {
			return nil, fmtError(err)
		}
	}
	// Split lines and limit the maximum number of lines
	lines := strings.Split(strings.Trim(string(data), "\n"), "\n")
	if len(lines[len(lines)-1]) > 0 {
		lines = append(lines, "")
	}
	return &History{
		path:     path,
		maxSize:  maxSize,
		lines:    lines,
		modified: make(map[int]string),
		cursor:   len(lines) - 1}, nil
}

func (h *History) append(line string) error {
	// We don't append empty lines
	if len(line) == 0 {
		return nil
	}

	lines := append(h.lines[:len(h.lines)-1], line)
	if len(lines) > h.maxSize {
		lines = lines[len(lines)-h.maxSize:]
	}
	h.lines = append(lines, "")
	return ioutil.WriteFile(h.path, []byte(strings.Join(h.lines, "\n")), 0600)
}

func (h *History) override(str string) {
	// You can update the history but they're not written to the file
	if h.cursor == len(h.lines)-1 {
		h.lines[h.cursor] = str
	} else if h.cursor < len(h.lines)-1 {
		h.modified[h.cursor] = str
	}
}

func (h *History) current() string {
	if str, prs := h.modified[h.cursor]; prs {
		return str
	}
	return h.lines[h.cursor]
}

func (h *History) previous() string {
	if h.cursor > 0 {
		h.cursor--
	}
	return h.current()
}

func (h *History) next() string {
	if h.cursor < len(h.lines)-1 {
		h.cursor++
	}
	return h.current()
}