-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
256 lines (238 loc) · 5.67 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import "bufio"
import "os"
import logpkg "log"
import "github.com/robertkrimen/otto"
import "strings"
import "fmt"
import "io/ioutil"
import "io"
import "strconv"
var COMMANDS = make(map[string]func([]string) error) // Map of native commands.
// JS can add to COMMANDS with def(command, function)
var vm = otto.New() // JS environment
var log = logpkg.New(ioutil.Discard, "", 0) // Dummy logger (logging turned off)
var commandBuffer []string // When something doesn't read as a command, add to buffer
var Line int
type FileBuffer struct {
Pos int
Length int
File io.ReadWriter
Contents []string
Meta map[string]string
}
var FB FileBuffer
// Use from builtin functions made available to JS to signal problem or success
var t, _ = vm.ToValue(true)
var f, _ = vm.ToValue(false)
// addCommand is defined to be invoked from JavaScript as def
// with the first argument being the defined command and the
// second argument being a JavaScript function to be invoked
// on that command.
func addCommand(call otto.FunctionCall) otto.Value {
log.Println("Enter addCommand")
command, err := call.Argument(0).ToString()
if err != nil {
return f
}
log.Println("Adding command ", command)
function := call.Argument(1)
if err != nil {
return f
}
COMMANDS[command] = func(cb []string) error {
cbs := strings.Join(cb, "\n")
_, err := function.Call(f, cbs)
if err != nil {
return err
}
return nil
}
log.Printf("%#v", COMMANDS)
return t
}
func runEditorCommand(call otto.FunctionCall) otto.Value {
log.Println("Entering runEditorCommand")
command, err := call.Argument(0).ToString()
if err != nil {
return f
}
var args = make([]string, len(call.ArgumentList))
for i, arg := range call.ArgumentList {
args[i], err = arg.ToString()
if err != nil {
return f
}
}
if err = COMMANDS[command](args[1:]); err != nil {
v, e := vm.ToValue(err)
if e != nil {
return f
} else {
return v
}
}
return t
}
func openFullFile(filename string) error {
FB = FileBuffer{0, 0, nil, nil, map[string]string{"filename": filename}}
file, err := os.Open(filename)
if err != nil {
return err
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
l := len(bytes)
m := make(map[string]string)
m["filename"] = filename
FB = FileBuffer{0, l, file, []string{string(bytes)}, m}
return nil
}
func saveFullFile(filename string) error {
log.Printf("Metadata: %#v\n", FB.Meta)
if filename == "" {
file, ok := FB.Meta["filename"]
if !ok {
return fmt.Errorf("no filename available")
}
filename = file
}
perm := os.ModePerm
if file, ok := FB.File.(*os.File); ok {
fi, err := file.Stat()
if err != nil {
perm = fi.Mode()
}
}
return ioutil.WriteFile(filename,
[]byte(strings.Join(FB.Contents, "\n")+"\n"),
perm)
}
func printLines(from, many int, page bool) {
log.Printf("Running printLines with %v, %v and %v\n", from, many, page)
mod := many // Page per mod lines
var readall bool
if many == 0 {
mod = 1
readall = true
}
if page {
readall = true
}
to := from + many - 1
FB.Contents = perLine(FB.Contents)
for i := from; i < len(FB.Contents); i++ {
log.Println("Value of i: ", i)
if !readall && i > to {
return
}
fmt.Println(FB.Contents[i])
Line = i
if page && (i+1)%mod == 0 {
if s, _ := bufio.NewReader(os.Stdin).ReadString('\n'); s[:1] == "q" {
return
}
}
}
}
func simpleSearch(keyword string, many int, page bool) {
log.Printf("Running printLines with %v, %v and %v\n", keyword, many, page)
mod := many // Page per mod lines
from := Line
var readall bool
if many == 0 {
mod = 1
readall = true
}
if page {
readall = true
}
FB.Contents = perLine(FB.Contents)
var q int
if many < 0 {
many = -many
from = 0
}
for i, line := range FB.Contents {
log.Println("Value of i: ", i)
if !strings.Contains(line, keyword) || i < from {
continue
}
q++
if !readall && q > many {
return
}
fmt.Println(i, ": ", line)
Line = i
if page && q%mod == 0 {
if s, _ := bufio.NewReader(os.Stdin).ReadString('\n'); s[:1] == "q" {
return
}
}
}
}
func perLine(in []string) (out []string) {
for _, s := range in {
temp := strings.Split(s, "\n")
for _, temps := range temp {
out = append(out, temps)
}
}
return out
}
func NativeCommand(run func([]string) error, cb []string) {
log.Println("Enter native command exec")
err := run(cb)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
} else {
os.Stderr.WriteString("!\n")
}
}
func main() {
if len(os.Args) > 1 {
openFullFile(os.Args[1])
}
vm.Set("def", addCommand)
vm.Set("e", runEditorCommand)
initCommands()
os.Stderr.WriteString("Welcome to " + os.Args[0] + ", a modern line editor with support for extension via JavaScript.\nType 'commands' to see the available commands on your system at any moment.\n")
for {
vm.Set("linenumber", Line)
reader := bufio.NewReader(os.Stdin)
command, _ := reader.ReadString('\n')
command = strings.TrimRight(command, "\n\r")
commands := strings.Split(command, ":")
cb := make([]string, len(commands)-1)
run, ok := COMMANDS[command]
if !ok && len(commands) > 1 {
run, ok = COMMANDS[commands[0]]
copy(cb,commands[1:])
if commands[len(commands)-1] == "" {
log.Println("Clear commandBuffer")
commandBuffer = nil
}
log.Printf("cb is now: %#v\n", cb)
} else {
cb = commandBuffer
}
if len(command) > 0 {
switch command[:1] {
case ".":
command = command[1:]
case ":":
Line, _ = strconv.Atoi(command[1:])
continue
}
}
if ok {
log.Printf("2:cb is now: %#v\n", cb)
NativeCommand(run, cb)
} else {
commandBuffer = append(commandBuffer, command)
log.Printf("%#v", command)
}
}
}