This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
86 lines (78 loc) · 1.75 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
package main
import (
"database/sql"
"hs9001/liner"
"io"
"log"
"path/filepath"
"strings"
)
type history struct {
conn *sql.DB
}
func createSearchOpts(query string, mode int) searchopts {
opts := searchopts{}
o := "DESC"
opts.order = &o
lim := 100
opts.limit = &lim
opts.command = &query
switch mode {
case liner.ModeGlobal:
break
case liner.ModeWorkdir:
workdir, err := filepath.Abs(".")
if err != nil {
panic(err)
}
opts.workdir = &workdir
default:
panic("Invalid mode supplied")
}
return opts
}
func (h *history) GetHistoryByPrefix(prefix string, mode int) (ph []string) {
cmdquery := prefix + "%"
opts := createSearchOpts(cmdquery, mode)
results := search(h.conn, opts)
for e := results.Back(); e != nil; e = e.Prev() {
entry, ok := e.Value.(*HistoryEntry)
if !ok {
log.Panic("Failed to retrieve entries")
}
ph = append(ph, entry.cmd)
}
return
}
func (h *history) GetHistoryByPattern(pattern string, mode int) (ph []string, pos []int) {
cmdquery := "%" + pattern + "%"
opts := createSearchOpts(cmdquery, mode)
results := search(h.conn, opts)
for e := results.Back(); e != nil; e = e.Prev() {
entry, ok := e.Value.(*HistoryEntry)
if !ok {
log.Panic("Failed to retrieve entries")
}
ph = append(ph, entry.cmd)
pos = append(pos, strings.Index(strings.ToLower(entry.cmd), strings.ToLower(pattern)))
}
return
}
func (h *history) ReadHistory(r io.Reader) (num int, err error) {
panic("not implemented")
}
func (h *history) WriteHistory(w io.Writer) (num int, err error) {
panic("not implemented")
}
func (h *history) AppendHistory(item string) {
panic("not implemented")
}
func (h *history) ClearHistory() {
panic("not implemented")
}
func (h *history) RLock() {
//noop
}
func (h *history) RUnlock() {
//noop
}