-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
155 lines (153 loc) · 3.75 KB
/
commands.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
149
150
151
152
153
154
155
package main
import (
"fmt"
"io/ioutil"
logpkg "log"
"os"
"strconv"
"strings"
)
func initCommands() {
COMMANDS["quit"] = func(_ []string) error {
os.Exit(0)
return nil
}
COMMANDS["runjs"] = func(js []string) error {
jss := strings.Join(js, "\n")
v, err := vm.Run(jss)
log.Println(v)
return err
}
COMMANDS["oops!"] = func(_ []string) error {
commandBuffer = nil
return nil
}
COMMANDS["oops"] = func(cb []string) error {
commandBuffer = cb[:len(cb)-1]
return nil
}
COMMANDS["cb"] = func(cb []string) error {
cbs := strings.Join(cb, "\n")
fmt.Println(cbs)
return nil
}
COMMANDS["log on"] = func(_ []string) error {
log = logpkg.New(os.Stderr, "Log: ", 0)
return nil
}
COMMANDS["log off"] = func(_ []string) error {
log = logpkg.New(ioutil.Discard, "", 0)
return nil
}
COMMANDS["commands"] = func(_ []string) error {
fmt.Print("Available commands: ")
var ks []string
for k := range COMMANDS {
ks = append(ks, k)
}
fmt.Println(strings.Join(ks, ", "))
return nil
}
COMMANDS["open"] = func(path []string) error {
paths := strings.Join(path, string(os.PathSeparator))
return openFullFile(paths)
}
COMMANDS["print"] = func(pos []string) error {
switch len(pos) {
case 0:
printLines(0, 0, false)
case 1:
line, _ := strconv.Atoi(pos[0])
printLines(line, 1, false)
case 2:
from, _ := strconv.Atoi(pos[0])
to, _ := strconv.Atoi(pos[1])
printLines(from, to-from+1, false)
default:
os.Stderr.WriteString("?")
log.Println("commandbuffer too full")
}
return nil
}
COMMANDS["page"] = func(pos []string) error {
switch len(pos) {
case 0:
printLines(0, 1, true)
case 1:
pagesize, _ := strconv.Atoi(pos[0])
printLines(0, pagesize, true)
case 2:
from, _ := strconv.Atoi(pos[0])
pagesize, _ := strconv.Atoi(pos[1])
printLines(from, pagesize, true)
default:
os.Stderr.WriteString("?")
log.Println("commandbuffer too full")
}
return nil
}
COMMANDS["r"] = func(line []string) error {
lines := strings.Join(line, "\n")
FB.Contents = perLine(FB.Contents)
if Line < len(FB.Contents) {
FB.Contents[Line] = lines
} else {
FB.Contents = append(FB.Contents, line...)
}
return nil
}
COMMANDS["i"] = func(line []string) error {
FB.Contents = perLine(FB.Contents)
if Line < len(FB.Contents) {
before := FB.Contents[:Line]
after := FB.Contents[Line:]
FB.Contents = append([]string(nil), before...)
FB.Contents = append(FB.Contents, line...)
FB.Contents = append(FB.Contents, after...)
} else {
FB.Contents = append(FB.Contents, line...)
}
return nil
}
COMMANDS["a"] = func(line []string) error {
FB.Contents = perLine(FB.Contents)
if Line+1 < len(FB.Contents) {
before := FB.Contents[:Line+1]
after := FB.Contents[Line+1:]
FB.Contents = append([]string(nil), before...)
FB.Contents = append(FB.Contents, line...)
FB.Contents = append(FB.Contents, after...)
} else {
FB.Contents = append(FB.Contents, line...)
}
return nil
}
COMMANDS["d"] = func(_ []string) error {
FB.Contents = perLine(FB.Contents)
if Line+1 < len(FB.Contents) {
log.Println(Line, Line-1, Line+1)
before := FB.Contents[:Line]
after := FB.Contents[Line+1:]
FB.Contents = append([]string(nil), before...)
FB.Contents = append(FB.Contents, after...)
} else if len(FB.Contents) != 0 {
FB.Contents = FB.Contents[:len(FB.Contents)-1]
}
return nil
}
COMMANDS["search"] = func(contents []string) error {
if len(contents) == 0 {
return fmt.Errorf("keyword missing")
}
var n int
if len(contents) > 1 {
n, _ = strconv.Atoi(contents[1])
}
simpleSearch(contents[0], n, true)
return nil
}
COMMANDS["save"] = func(path []string) error {
filename := strings.Join(path, string(os.PathSeparator))
return saveFullFile(filename)
}
}