1package editor
2
3type historyState struct {
4 previouslyScrollingPromptHistory bool
5 promptHistoryIndex int
6 existingValue string
7 historyCache []string
8}
9
10type History interface {
11 ExistingValue() string
12 Value() string
13 ScrollUp()
14 ScrollDown()
15}
16
17func InitialiseHistory(existingValue string, messages []string) History {
18 return &historyState{
19 existingValue: existingValue,
20 historyCache: messages,
21 promptHistoryIndex: len(messages) - 1,
22 }
23}
24
25func (h *historyState) Value() string {
26 if len(h.historyCache) == 0 {
27 return h.existingValue
28 }
29 return h.historyCache[h.promptHistoryIndex]
30}
31
32func (h *historyState) ExistingValue() string {
33 return h.existingValue
34}
35
36func (h *historyState) ScrollUp() {
37 h.promptHistoryIndex--
38 h.clampIndex()
39}
40
41func (h *historyState) ScrollDown() {
42 h.promptHistoryIndex++
43 h.clampIndex()
44}
45
46func (h *historyState) clampIndex() {
47 if h.promptHistoryIndex > len(h.historyCache)-1 {
48 h.promptHistoryIndex = len(h.historyCache) - 1
49 return
50 }
51
52 if h.promptHistoryIndex < 0 {
53 h.promptHistoryIndex = 0
54 }
55}