-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrepl.lua
executable file
·67 lines (54 loc) · 1.25 KB
/
repl.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env resty
-- USAGE:
-- PUT INTO CODE:
-- require('resty.repl').start()
--
-- OR:
-- run: resty -e "require('resty.repl').start()"
--
-- TODO: better completion
-- TODO: color output
-- TODO: add specs
local new_binding = require('resty.repl.binding').new
local new_ui = require('resty.repl.ui').new
local formatter = require 'resty.repl.formatter'
local running = false
local binding, ui
local function stop()
running = false
end
local function exit(exit_code)
stop()
return os.exit(exit_code or 0)
end
local function handle_input(input)
if input.exit then exit(0) end
if input.stop then return stop() end
if input.code then
local result = binding:eval(input.code)
formatter.print(result, #input.code)
return result
end
end
local function start()
running = true
local caller_info = debug.getinfo(2)
binding = new_binding(caller_info)
ui = new_ui(binding)
while running do
local input = ui:readline()
local result = handle_input(input)
if result then
ui:add_to_history(input.code)
end
end
end
return {
_VERSION = '0.1',
start = start,
stop = stop,
handle_input = handle_input,
running = running,
ui = ui,
binding = binding,
}