-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplacemode.go
More file actions
67 lines (59 loc) · 1.15 KB
/
replacemode.go
File metadata and controls
67 lines (59 loc) · 1.15 KB
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
package main
import (
"fmt"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
)
type ReplaceMode struct {
str string
start bool
olds []string
}
func (m *ReplaceMode) Start() {
nm := tor.normal
if nm.selection.on {
m.str = nm.text.DataInside(nm.selection.MinMax())
}
m.start = true
}
func (m *ReplaceMode) End() {}
func (m *ReplaceMode) Handle(ev *tcell.EventKey) {
switch ev.Key() {
case tcell.KeyEsc, tcell.KeyCtrlK:
if len(m.olds) == 0 {
m.str = ""
} else {
m.str = m.olds[len(m.olds)-1]
}
tor.ChangeMode(tor.normal)
case tcell.KeyEnter:
tor.ChangeMode(tor.normal)
m.olds = append(m.olds, m.str)
saveConfig("replace", m.str)
case tcell.KeyBackspace, tcell.KeyBackspace2:
if m.start {
m.str = ""
m.start = false
return
}
_, rlen := utf8.DecodeLastRuneInString(m.str)
m.str = m.str[:len(m.str)-rlen]
default:
if ev.Modifiers()&tcell.ModAlt != 0 {
return
}
if ev.Rune() != 0 {
if m.start {
m.str = ""
}
m.str += string(ev.Rune())
}
m.start = false
}
}
func (m *ReplaceMode) Status() string {
return fmt.Sprintf("replace : %v", m.str)
}
func (m *ReplaceMode) Error() string {
return ""
}