-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrow.go
107 lines (91 loc) · 2.15 KB
/
row.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
package terminalparser
import (
"strings"
"github.com/mattn/go-runewidth"
)
type Row struct {
dataRune []rune
currentX int
currentRuneIndex int
// fish shell 补全提示
tipRune []rune
tipRecord bool
}
func (r *Row) String() string {
return strings.TrimSuffix(string(r.dataRune), string(r.tipRune))
}
func (r *Row) appendCharacter(code rune) {
width := runewidth.StringWidth(string(code))
if r.currentRuneIndex < len(r.dataRune) {
r.dataRune[r.currentRuneIndex] = code
} else {
r.dataRune = append(r.dataRune, code)
}
r.currentRuneIndex++
r.currentX += width
r.addTipRune(code)
}
func (r *Row) insertCharacters(data []rune) {
result := make([]rune, len(r.dataRune)+len(data))
copy(result, r.dataRune[:r.currentRuneIndex])
copy(result[r.currentRuneIndex:], data)
copy(result[r.currentRuneIndex+len(data):], r.dataRune[r.currentRuneIndex:])
for i := range data {
r.currentRuneIndex++
r.currentX += runewidth.StringWidth(string(data[i]))
}
r.dataRune = result
}
func (r *Row) eraseRight() {
r.dataRune = r.dataRune[:r.currentRuneIndex]
}
func (r *Row) deleteChars(ps int) {
result := make([]rune, r.currentRuneIndex, len(r.dataRune))
copy(result, r.dataRune[:r.currentRuneIndex])
rest := r.dataRune[r.currentRuneIndex:]
inits := ps
for i := range rest {
inits -= runewidth.StringWidth(string(rest[i]))
if inits == 0 {
result = append(result, rest[i+1:]...)
break
}
}
r.dataRune = result
}
func (r *Row) changeCurrentRuneIndex() {
if r.currentX < 0 {
r.currentX = 0
}
currentRuneIndex := 0
for i := range r.dataRune {
currentRuneIndex += runewidth.StringWidth(string(r.dataRune[i]))
if currentRuneIndex > r.currentX {
r.currentRuneIndex = i
return
}
}
r.currentRuneIndex = len(r.dataRune)
}
func (r *Row) changeCursorToX(x int) {
if r.currentX == x {
return
}
r.currentX = x
r.changeCurrentRuneIndex()
}
func (r *Row) startRecord() {
r.tipRecord = true
r.tipRune = make([]rune, 0, 100)
}
func (r *Row) stopRecord() {
if !r.tipRecord {
r.tipRune = make([]rune, 0, 100)
}
r.tipRecord = false
}
func (r *Row) addTipRune(code rune) {
if r.tipRecord {
r.tipRune = append(r.tipRune, code)
}
}