forked from nemith/goline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
128 lines (110 loc) · 2.58 KB
/
handlers.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
package goline
func findLastWord(t []rune, start int) int {
for start > 0 && t[start-1] == ' ' {
start--
}
for start > 0 && t[start-1] != ' ' {
start--
}
return start
}
func findNextWord(t []rune, end, max int) int {
for end < max && t[end] == ' ' {
end++
}
for end < max && t[end] != ' ' {
end++
}
return end
}
func Finish(l *GoLine) (bool, error) {
return true, nil
}
func UserTerminated(l *GoLine) (bool, error) {
return true, UserTerminatedError
}
func Backspace(l *GoLine) (bool, error) {
if l.Len > 0 && l.Position > 0 {
l.CurLine = append(l.CurLine[:l.Position-1], l.CurLine[l.Position:]...)
l.Len--
l.Position--
l.CurLine[l.Len] = 0
}
return false, nil
}
func MoveBackOneWord(l *GoLine) (bool, error) {
l.Position = findLastWord(l.CurLine, l.Position)
return false, nil
}
func MoveForwardOneWord(l *GoLine) (bool, error) {
l.Position = findNextWord(l.CurLine, l.Position, l.Len)
return false, nil
}
func MoveLeft(l *GoLine) (bool, error) {
if l.Position > 0 {
l.Position--
}
return false, nil
}
func MoveRight(l *GoLine) (bool, error) {
if l.Position != l.Len {
l.Position++
}
return false, nil
}
func DeleteLine(l *GoLine) (bool, error) {
l.CurLine = make([]rune, MAX_LINE)
l.Position = 0
l.Len = 0
return false, nil
}
func DeleteRestofLine(l *GoLine) (bool, error) {
copy(l.CurLine, l.CurLine[:l.Position])
l.Len = l.Position
return false, nil
}
func DeleteLastWord(l *GoLine) (bool, error) {
prev_position := l.Position
l.Position = findLastWord(l.CurLine, l.Position)
copy(l.CurLine, append(l.CurLine[:l.Position], l.CurLine[prev_position:l.Len]...))
l.Len -= prev_position - l.Position
return false, nil
}
func DeleteNextWord(l *GoLine) (bool, error) {
end := findNextWord(l.CurLine, l.Position, l.Len)
copy(l.CurLine, append(l.CurLine[:l.Position], l.CurLine[end:l.Len]...))
l.Len -= end - l.Position
return false, nil
}
func DeleteCurrentChar(l *GoLine) (bool, error) {
if l.Position < l.Len {
l.CurLine = append(l.CurLine[:l.Position], l.CurLine[l.Position+1:]...)
l.Len--
}
return false, nil
}
func SwapWithPreviousChar(l *GoLine) (bool, error) {
if l.Position > 0 && l.Position <= l.Len {
x := l.Position
if l.Position == l.Len {
x--
}
l.CurLine[x], l.CurLine[x-1] = l.CurLine[x-1], l.CurLine[x]
if l.Position != l.Len {
l.Position++
}
}
return false, nil
}
func MoveStartofLine(l *GoLine) (bool, error) {
l.Position = 0
return false, nil
}
func MoveEndofLine(l *GoLine) (bool, error) {
l.Position = l.Len
return false, nil
}
func ClearScreen(l *GoLine) (bool, error) {
l.ClearScreen()
return false, nil
}