This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
history.go
205 lines (193 loc) · 4.08 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package history
import (
"errors"
"fmt"
"log"
"os"
"regexp"
"strings"
"text/tabwriter"
"github.com/b4b4r07/zsh-history/db"
"github.com/nsf/termbox-go"
)
const (
Prompt string = "sqlite3> "
DefaultQuery string = "SELECT DISTINCT(command) FROM history WHERE command LIKE '%%' AND status = 0 ORDER BY id DESC"
Wildcard string = "%"
)
type History struct {
DB *db.DBHandler
rows db.Records
}
func NewHistory() *History {
return &History{
DB: db.NewDBHandler(),
rows: db.Records{},
}
}
func (h *History) Insert(cmd string, status int) error {
var cfg config
err := cfg.load()
if err != nil {
errors.New("failed to load config file")
}
for _, ignore := range cfg.IgnoreWords {
if regexp.MustCompile(`\b` + ignore + `\b`).Match([]byte(cmd)) {
// skip to insert
return nil
}
}
return h.DB.Insert(cmd, status)
}
func (h *History) List() error {
list, err := h.DB.QueryList()
if err != nil {
return err
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 8, 0, '\t', 0)
for _, l := range list {
fmt.Fprintf(w, "%s\t%s\t\"%s\"\t%d\n",
l.DateTime, l.Directory, l.Command, l.Status,
)
}
w.Flush()
return nil
}
func (h *History) Query(query string) (db.Records, error) {
return h.DB.Query(query)
}
func (h *History) Screen(args []string) int {
output := ""
err := termbox.Init()
if err != nil {
log.Println(err)
return 1
}
s := NewScreen(strings.Join(args, " "))
s.DrawScreen()
defer func() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
termbox.Close()
if output != "" {
fmt.Println(output)
}
}()
loop:
for {
var (
updatePrompt bool
updateAll bool
updateWithFilter bool
)
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
s.ToggleVimMode()
updatePrompt = true
case termbox.KeyCtrlC, termbox.KeyCtrlG:
break loop
case termbox.KeyCtrlA:
s.MoveCusorBegin()
updatePrompt = true
case termbox.KeyCtrlE:
s.MoveCusorEnd()
updatePrompt = true
case termbox.KeyArrowRight, termbox.KeyCtrlF:
s.MoveCusorForward()
updatePrompt = true
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
s.MoveCusorBackward()
updatePrompt = true
case termbox.KeyArrowDown, termbox.KeyCtrlN:
s.SelectNext()
updateAll = true
case termbox.KeyArrowUp, termbox.KeyCtrlP:
s.SelectPrevious()
updateAll = true
case termbox.KeyEnter:
output = s.GetOutput()
break loop
case termbox.KeyBackspace, termbox.KeyBackspace2:
s.DeleteBackwardChar()
updateWithFilter = true
case termbox.KeyDelete, termbox.KeyCtrlD:
s.DeleteChar()
updateWithFilter = true
case termbox.KeyCtrlU:
s.ClearPrompt()
updateWithFilter = true
case termbox.KeyCtrlW:
s.DeleteBackwardWord()
updateWithFilter = true
default:
if ev.Key == termbox.KeySpace {
ev.Ch = ' '
}
if ev.Ch > 0 {
if s.IsVimMode() {
switch ev.Ch {
case 'j':
s.SelectNext()
updateAll = true
case 'k':
s.SelectPrevious()
updateAll = true
case 'l':
s.MoveCusorForward()
updatePrompt = true
case 'h':
s.MoveCusorBackward()
updatePrompt = true
case '0', '^':
s.MoveCusorBegin()
updatePrompt = true
case '$':
s.MoveCusorEnd()
updatePrompt = true
case 'i':
s.ToggleVimMode()
updatePrompt = true
case 'a':
s.ToggleVimMode()
s.MoveCusorForward()
updatePrompt = true
case 'I':
s.ToggleVimMode()
s.MoveCusorBegin()
updatePrompt = true
case 'A':
s.ToggleVimMode()
s.MoveCusorEnd()
updatePrompt = true
}
} else {
s.InsertChar(ev.Ch)
updateWithFilter = true
}
}
}
case termbox.EventResize:
s.SetSize()
updateAll = true
}
if updatePrompt {
s.DrawPrompt()
}
if updateAll {
s.DrawScreen()
}
if updateWithFilter {
s.DrawPrompt()
go func() {
done := make(chan bool)
s.Filter(done)
if <-done {
s.DrawScreen()
}
}()
}
}
return 0
}