-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
148 lines (141 loc) · 2.79 KB
/
command.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
)
// Command mode.
type Command struct {
editor *Editor
}
// Show updates mode when switched to.
func (mode *Command) Show() error {
return nil
}
// Hide updates mode when switched from.
func (mode *Command) Hide() error {
return nil
}
// Key handles special key.
func (mode *Command) Key(key Key) error {
var err error
switch key {
case KeyTab:
mode.editor.SwitchMode(mode.editor.Switch)
}
if mode.editor.Empty() {
return nil
}
switch key {
case KeyLeft:
mode.editor.MoveLeft()
case KeyRight:
mode.editor.MoveRight()
case KeyUp:
mode.editor.MoveUp()
case KeyDown:
mode.editor.MoveDown()
case KeyHome:
mode.editor.MoveLineStart()
case KeyEnd:
mode.editor.MoveLineEnd()
case KeyPageUp:
mode.editor.MoveParagraphPrevious()
case KeyPageDown:
mode.editor.MoveParagraphNext()
}
if err != nil {
return fmt.Errorf("error handling key %v: %w", key, err)
}
return nil
}
// Rune handles runes.
func (mode *Command) Rune(rune rune) (err error) {
switch rune {
case 'b':
err = mode.editor.Write()
mode.editor.Pause()
case 'B':
err = mode.editor.Write()
mode.editor.Quit()
}
if mode.editor.Empty() {
return nil
}
switch rune {
case ' ':
mode.editor.SwitchMode(mode.editor.Input)
case 'd':
mode.editor.MoveLeft()
case 'f':
mode.editor.MoveRight()
case 'a':
mode.editor.MoveUp()
case 's':
mode.editor.MoveDown()
case 'D':
mode.editor.MoveLineStart()
case 'F':
mode.editor.MoveLineEnd()
case 'A':
mode.editor.MoveFileStart()
case 'S':
mode.editor.MoveFileEnd()
case 'e':
mode.editor.MoveWordPrevious()
case 'r':
mode.editor.MoveWordNext()
case 'q':
mode.editor.MoveParagraphPrevious()
case 'w':
mode.editor.MoveParagraphNext()
case 'z':
mode.editor.SwitchVersion(Backward)
case 'x':
mode.editor.SwitchVersion(Forward)
case 'c':
mode.editor.SwitchFile(Backward)
case 'v':
mode.editor.SwitchFile(Forward)
case 'j':
mode.editor.Delete()
case 'J':
mode.editor.DeleteLine()
case 'k':
mode.editor.LineBelow()
case 'K':
mode.editor.LineAbove()
case 'H':
mode.editor.SwitchMode(mode.editor.Select)
case 'h':
mode.editor.Paste()
case 'n':
_, err = mode.editor.File.Write()
mode.editor.Files.Close()
case 'N':
_, err = mode.editor.Read(true)
case 'm':
err = mode.editor.Write()
case 'M':
_, err = mode.editor.File.Write()
case '\\':
mode.editor.ToggleVisible()
}
if err != nil {
return fmt.Errorf("error handling rune %v: %w", rune, err)
}
return nil
}
// Render renders mode.
func (mode *Command) Render(view *View) (err error) {
if !mode.editor.Empty() {
err = mode.editor.File.Render(view)
if err != nil {
return fmt.Errorf("error rendering command mode: %w", err)
}
}
view.Color = ColorGreen
view.Prompt = ""
if mode.editor.Empty() {
view.Cursor = CursorNone
}
return nil
}