forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
70 lines (55 loc) · 1.45 KB
/
Copy pathinput.go
File metadata and controls
70 lines (55 loc) · 1.45 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
68
69
70
package readline
import (
"os"
"regexp"
)
// InputMode - The shell input mode.
type InputMode string
const (
// Vim - Vim editing mode.
Vim InputMode = "vim"
// Emacs - Emacs (classic) editing mode.
Emacs InputMode = "emacs"
)
// readInput reads input from stdin and returns the result, length or an error.
func (rl *Instance) readInput() (b []byte, i int, err error) {
rl.undoSkipAppend = false
b = make([]byte, 1024)
if !rl.skipStdinRead {
i, err = os.Stdin.Read(b)
if err != nil {
return
}
}
rl.skipStdinRead = false
return
}
// readOperator reads a key required by some (rare) widgets that directly read/need
// their argument/operator, without going though operator pending mode first.
// If all is true, we return all keys, including numbers (instead of adding them as iterations.)
func (rl *Instance) readOperator(all bool) (key string, ret bool) {
rl.enterVioppMode("")
rl.updateCursor()
defer func() {
rl.exitVioppMode()
rl.updateCursor()
}()
b, i, _ := rl.readInput()
key = string(b[:i])
// If the last key is a number, add to iterations instead,
// and read another key input.
if !all {
numMatcher, _ := regexp.Compile(`^[1-9][0-9]*$`)
for numMatcher.MatchString(string(key[len(key)-1])) {
rl.iterations += string(key[len(key)-1])
b, i, _ = rl.readInput()
key = string(b[:i])
}
}
// If the key is an escape key for the current mode.
if len(key) == 1 &&
(key[0] == charEscape) {
ret = true
}
return
}