-
Notifications
You must be signed in to change notification settings - Fork 0
/
history_modal.go
166 lines (133 loc) · 3.54 KB
/
history_modal.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package component
import (
"os"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/kopecmaciej/mongui/config"
"github.com/kopecmaciej/mongui/manager"
"github.com/kopecmaciej/mongui/primitives"
)
const (
HistoryModalComponent manager.Component = "HistoryModal"
maxHistory = 10
)
// HistoryModal is a modal with history of queries
type HistoryModal struct {
*Component
*primitives.ListModal
style *config.HistoryStyle
}
func NewHistoryModal() *HistoryModal {
h := &HistoryModal{
Component: NewComponent(HistoryModalComponent),
ListModal: primitives.NewListModal(),
}
h.SetAfterInitFunc(h.init)
return h
}
// Init initializes HistoryModal
func (h *HistoryModal) init() error {
h.setStyle()
h.setKeybindings()
return nil
}
func (h *HistoryModal) setStyle() {
h.style = &h.app.Styles.History
h.SetTitle(" History ")
h.SetBorder(true)
h.ShowSecondaryText(false)
h.SetBackgroundColor(h.style.BackgroundColor.Color())
mainStyle := tcell.StyleDefault.
Foreground(h.style.TextColor.Color()).
Background(h.style.BackgroundColor.Color())
h.SetMainTextStyle(mainStyle)
selectedStyle := tcell.StyleDefault.
Foreground(h.style.SelectedTextColor.Color()).
Background(h.style.SelectedBackgroundColor.Color())
h.SetSelectedStyle(selectedStyle)
}
func (h *HistoryModal) setKeybindings() {
h.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc, tcell.KeyEnter, tcell.KeyCtrlY:
eventKey := manager.EventMsg{EventKey: event, Sender: h.GetIdentifier()}
h.SendToComponent(InputBarComponent, eventKey)
h.app.Root.RemovePage(h.GetIdentifier())
return nil
}
return event
})
}
// Render loads history from file and renders it
func (h *HistoryModal) Render() error {
history, err := h.loadHistory()
if err != nil {
return err
}
h.Clear()
// load in reverse order
for i := len(history) - 1; i >= 0; i-- {
rune := 57 - i
entry := history[i]
h.AddItem(entry, "", int32(rune), nil)
}
h.app.Root.AddPage(h.GetIdentifier(), h, true, true)
return nil
}
// SaveToHistory saves text to history file, if it's not already there.
// It will overwrite oldest entry if history is full.
func (h *HistoryModal) SaveToHistory(text string) error {
history, err := h.loadHistory()
var updatedHistory []string
for _, line := range history {
if line != text {
updatedHistory = append(updatedHistory, line)
if len(updatedHistory) >= maxHistory {
updatedHistory = updatedHistory[1:]
}
}
}
updatedHistory = append(updatedHistory, text)
historyFile, err := os.OpenFile(getHisotryFilePath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer historyFile.Close()
for _, entry := range updatedHistory {
_, err = historyFile.WriteString(entry + "\n")
if err != nil {
return err
}
}
return nil
}
// GetText returns text from selected item
func (h *HistoryModal) GetText() string {
text := h.ListModal.GetText()
return strings.TrimSpace(text)
}
// loadHistory loads history from history file
func (h *HistoryModal) loadHistory() ([]string, error) {
bytes, err := os.ReadFile(getHisotryFilePath())
if err != nil {
if os.IsNotExist(err) {
err = os.WriteFile(getHisotryFilePath(), []byte{}, 0644)
}
return nil, err
}
history := []string{}
lines := strings.Split(string(bytes), "\n")
for _, line := range lines {
if line != "" {
history = append(history, line)
}
}
return history, nil
}
func getHisotryFilePath() string {
configDir, err := config.GetConfigDir()
if err != nil {
return ""
}
return configDir + "/history.txt"
}