This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
/
main.go
247 lines (232 loc) · 5.21 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
package main
import (
"bytes"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
"syscall"
"time"
"unicode/utf8"
"github.com/reusee/lgo"
"github.com/reusee/my-editor-gl/core"
"github.com/reusee/my-editor-gl/extra"
"github.com/reusee/my-editor-gl/lib/mimemagic"
)
var debugFlag = flag.Bool("debug", false, "enable debug")
func init() {
runtime.GOMAXPROCS(32)
rand.Seed(time.Now().UnixNano())
fmt.Printf("")
flag.Parse()
// log
if !*debugFlag {
_, path, _, _ := runtime.Caller(0)
logFile, err := os.OpenFile(filepath.Join(filepath.Dir(path), "logs", fmt.Sprintf("%d", time.Now().UnixNano())),
os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
if err != nil {
panic("cannot open log file")
}
syscall.Dup2(int(logFile.Fd()), 1)
syscall.Dup2(int(logFile.Fd()), 2)
}
}
var t0 = time.Now()
func main() {
lua := lgo.NewLua()
lua.RegisterFunctions(map[string]interface{}{
// sys
"Sys_argv": func() []string {
return os.Args[1:]
},
"Sys_program_path": func() string {
_, path, _, _ := runtime.Caller(0)
return filepath.Dir(path)
},
"Sys_home": func() string {
user, err := user.Current()
if err != nil {
return ""
}
return user.HomeDir
},
"Sys_exit": func() {
os.Exit(0)
},
// path
"Path_abs": func(p string) string {
abs, _ := filepath.Abs(p)
return abs
},
"Path_dir": func(p string) string {
return filepath.Dir(p)
},
"Path_base": func(p string) string {
return filepath.Base(p)
},
"Path_split": func(p string) (string, string) {
return filepath.Split(p)
},
"Path_join": func(ps []string) string {
res := ""
for _, part := range ps {
res = filepath.Join(res, part)
}
return res
},
"Path_sep": func() string {
return string(os.PathSeparator)
},
// time
"Time_current_time_in_millisecond": func() int64 {
t := time.Now().UnixNano() / 1000000
return t
},
"Time_tick": func() (ret string) {
ret = fmt.Sprintf("%v", time.Now().Sub(t0))
t0 = time.Now()
return
},
// os
"Os_exists": func(p string) bool {
_, err := os.Stat(p)
return err == nil
},
"Os_mkdir": func(p string) bool {
return os.Mkdir(p, 0755) != nil
},
"Os_list": func(path string) ([]string, bool) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, true
}
var names []string
for _, info := range files {
names = append(names, info.Name())
}
return names, false
},
"Os_isdir": func(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
},
"Os_filemode": func(path string) os.FileMode {
info, err := os.Stat(path)
if err != nil {
return 0
}
return info.Mode()
},
"Os_createwithmode": func(path string, mode uint32) bool {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(mode))
if err != nil {
return true
}
f.Close()
return false
},
"Os_move": func(src, dst string) bool {
info, err := os.Stat(src)
if err != nil {
return true
}
mode := info.Mode()
_, err = os.Stat(dst)
if err == nil { // dst exists
return true
}
f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, mode)
if err != nil {
return true
}
defer f.Close()
content, err := ioutil.ReadFile(src)
if err != nil {
return true
}
f.Write(content)
return false
},
"Os_rename": func(src, dst string) bool {
return os.Rename(src, dst) != nil
},
// text
"Text_escapemarkup": func(s string) string {
buf := new(bytes.Buffer)
err := xml.EscapeText(buf, []byte(s))
if err != nil {
return ""
}
return string(buf.Bytes())
},
"chr": func(r rune) string {
return string(r)
},
"Text_is_valid_utf8": func(input []byte) bool {
return utf8.Valid(input)
},
"Text_guess_type": func(text []byte) string {
t := mimemagic.Match("", text)
if strings.HasPrefix(t, "text/x-") {
return strings.Replace(t, "text/x-", "", -1)
}
return ""
},
// regex
"Regex_index": func(pattern string, content []byte) interface{} {
re, err := regexp.Compile(pattern)
if err != nil {
return false
}
indexes := re.FindAllSubmatchIndex(content, -1)
if indexes == nil {
return false
}
// convert byte index to char index
byte_index := 0
char_index := 0
var size int
for i, index := range indexes {
for byte_index != index[0] {
_, size = utf8.DecodeRune(content[byte_index:])
byte_index += size
char_index += 1
}
indexes[i][0] = char_index
for byte_index != index[1] {
_, size = utf8.DecodeRune(content[byte_index:])
byte_index += size
char_index += 1
}
indexes[i][1] = char_index
}
return indexes
},
"Regex_find": func(pattern, content string) (ret []string) {
re := regexp.MustCompile(pattern)
if words := re.FindAllString(content, -1); words != nil {
ret = words
}
return
},
})
lua.RegisterFunctions(core.Registry)
lua.RegisterFunctions(extra.Registry)
_, path, _, _ := runtime.Caller(0)
lua.RunString(`package.path = '` + filepath.Dir(path) + `' .. '/?.lua;' .. package.path`)
go func() {
runtime.LockOSThread()
lua.RunString(`require 'main'`)
}()
<-(make(chan bool))
}