-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryline.go
103 lines (84 loc) · 1.67 KB
/
queryline.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
package civ
type QueryLine struct {
query []rune
curCursor int // starts from 0
mode int
}
// Modes
const (
MODE_VIEW = 1
MODE_COMMAND = 2
MODE_SEARCH = 3
MODE_FILTER = 4
)
func NewQueryLine() *QueryLine {
q := &QueryLine{
query: nil,
curCursor: 0,
mode: MODE_VIEW,
}
return q
}
func (q *QueryLine) ClearQuery() {
q.query = nil
q.curCursor = 0
}
func (q *QueryLine) ClearAll() {
q.query = nil
q.curCursor = 0
q.mode = MODE_VIEW
}
func (q *QueryLine) QueryLen() int {
return len(q.query)
}
func (q *QueryLine) InputChar(r rune) {
if len(q.query) <= q.curCursor {
q.query = append(q.query, r)
} else {
_q := make([]rune, q.curCursor)
copy(_q, q.query[:q.curCursor])
q.query = append(append(_q, r), q.query[q.curCursor:]...)
}
q.curCursor++
}
func (q *QueryLine) BackwardChar() {
if q.curCursor == 0 {
return
}
_q := make([]rune, q.curCursor-1)
copy(_q, q.query[:q.curCursor])
q.query = append(_q, q.query[q.curCursor:]...)
q.curCursor--
}
func (q *QueryLine) DeleteChar() {
if q.curCursor >= len(q.query) {
return
}
_q := make([]rune, q.curCursor)
copy(_q, q.query[:q.curCursor])
q.query = append(_q, q.query[(q.curCursor+1):]...)
}
func (q *QueryLine) TruncateChars() {
if q.curCursor >= len(q.query) {
return
}
_q := make([]rune, q.curCursor)
copy(_q, q.query[:q.curCursor])
q.query = _q
}
func (q *QueryLine) MoveForward() {
if len(q.query) > q.curCursor {
q.curCursor++
}
}
func (q *QueryLine) MoveBackward() {
if len(q.query) >= q.curCursor && q.curCursor > 0 {
q.curCursor--
}
}
func (q *QueryLine) MoveToTop() {
q.curCursor = 0
}
func (q *QueryLine) MoveToEnd() {
q.curCursor = len(q.query)
}