-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-control.lua
49 lines (36 loc) · 1.13 KB
/
script-control.lua
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
local js = require"js"
local sleep = require"sleep"
local terminal = require"terminal"
local colors = require"ansicolors"
local user_task = nil
local editor = nil
local _print = terminal.print
local function _report_err(...)
_print(colors("%{bright red}Script error:%{reset} "), ...)
end
local function run_code()
local src = editor:getValue()
local env = {print=_print}
setmetatable(env, {__index = _G})
local maybe_code, compile_err = load(src, "=(user)", 't', env)
if not maybe_code then
_print(colors("%{bright red}Compile error:%{reset}"), compile_err)
else
if user_task then
user_task:cancel()
user_task = nil
end
user_task = sleep.sleeper:new(maybe_code, _report_err)
user_task:launch()
end
end
local function setup_editor()
editor = js.global.ace:edit("editor")
editor:setTheme("ace/theme/monokai")
editor.session:setMode("ace/mode/lua")
js.global.editor = editor
local document = js.global.document
local run_btn = document:getElementById("run-btn")
run_btn.onclick = function () run_code() end
end
setup_editor()