SHOW:
|
|
- or go back to the newest paste.
1 | - | local component = require("component") |
1 | + | local component = require("component") |
2 | - | local package = require("package") |
2 | + | local package = require("package") |
3 | - | local term = require("term") |
3 | + | local term = require("term") |
4 | - | local serialization = require("serialization") |
4 | + | local serialization = require("serialization") |
5 | - | local shell = require("shell") |
5 | + | local shell = require("shell") |
6 | - | |
6 | + | |
7 | - | local args, options = shell.parse(...) |
7 | + | local args, options = shell.parse(...) |
8 | - | local env = setmetatable({}, {__index = _ENV}) |
8 | + | local env = setmetatable({}, {__index = _ENV}) |
9 | - | |
9 | + | |
10 | - | if #args > 0 then |
10 | + | if #args > 0 then |
11 | - | local script, reason = loadfile(args[1], nil, env) |
11 | + | local script, reason = loadfile(args[1], nil, env) |
12 | - | if not script then |
12 | + | if not script then |
13 | - | io.stderr:write(tostring(reason) .. "\n") |
13 | + | io.stderr:write(tostring(reason) .. "\n") |
14 | - | os.exit(false) |
14 | + | os.exit(false) |
15 | - | end |
15 | + | end |
16 | - | local result, reason = pcall(script, table.unpack(args, 2)) |
16 | + | local result, reason = pcall(script, table.unpack(args, 2)) |
17 | - | if not result then |
17 | + | if not result then |
18 | - | io.stderr:write(reason) |
18 | + | io.stderr:write(reason) |
19 | - | os.exit(false) |
19 | + | os.exit(false) |
20 | - | end |
20 | + | end |
21 | end | |
22 | - | |
22 | + | |
23 | - | if #args == 0 or options.i then |
23 | + | if #args == 0 or options.i then |
24 | - | local function optrequire(...) |
24 | + | local function optrequire(...) |
25 | - | local success, module = pcall(require, ...) |
25 | + | local success, module = pcall(require, ...) |
26 | - | if success then |
26 | + | if success then |
27 | - | return module |
27 | + | return module |
28 | - | end |
28 | + | end |
29 | - | end |
29 | + | end |
30 | - | setmetatable(env, {__index = function(t, k) |
30 | + | setmetatable(env, {__index = function(t, k) |
31 | - | return _ENV[k] or optrequire(k) |
31 | + | return _ENV[k] or optrequire(k) |
32 | - | end}) |
32 | + | end}) |
33 | - | |
33 | + | |
34 | - | local history = {} |
34 | + | local history = {} |
35 | - | component.gpu.setForeground(0xFFFF00) |
35 | + | component.gpu.setForeground(0xFFFF00) |
36 | - | term.write("Interactive lua prompt:\nto exit the lua interpeter use: exit()\n") |
36 | + | term.write("Interactive lua prompt:\nto exit the lua interpeter use: exit()\n") |
37 | - | component.gpu.setForeground(0xFFFFFF) |
37 | + | component.gpu.setForeground(0xFFFFFF) |
38 | - | while term.isAvailable() do |
38 | + | while term.isAvailable() do |
39 | - | local foreground = component.gpu.setForeground(0xffff00) |
39 | + | local foreground = component.gpu.setForeground(0xffff00) |
40 | - | term.write(tostring(env._PROMPT or "lua> ")) |
40 | + | term.write(tostring(env._PROMPT or "lua> ")) |
41 | - | component.gpu.setForeground(foreground) |
41 | + | component.gpu.setForeground(foreground) |
42 | - | local command = term.read(history) |
42 | + | local command = term.read(history) |
43 | - | if command == nil then -- eof |
43 | + | if command == nil then -- eof |
44 | - | return |
44 | + | return |
45 | - | end |
45 | + | end |
46 | - | while #history > 10 do |
46 | + | while #history > 10 do |
47 | - | table.remove(history, 1) |
47 | + | table.remove(history, 1) |
48 | - | end |
48 | + | end |
49 | - | local code, reason |
49 | + | local code, reason |
50 | - | if string.find(command,"=") == nil then |
50 | + | if string.find(command,"=") == nil then |
51 | - | command = "="..command |
51 | + | command = "="..command |
52 | - | end |
52 | + | end |
53 | - | res = command == "=exit()\n" |
53 | + | res = command == "=exit()\n" |
54 | - | if res == true then |
54 | + | if res == true then |
55 | - | break |
55 | + | break |
56 | - | else |
56 | + | else |
57 | - | if string.sub(command,1,1) == "=" then |
57 | + | if string.sub(command,1,1) == "=" then |
58 | - | code, reason = load("return " .. string.sub(command, 2), "=stdin", "t", env) |
58 | + | code, reason = load("return " .. string.sub(command, 2), "=stdin", "t", env) |
59 | - | else |
59 | + | else |
60 | - | code, reason = load(command, "=stdin", "t", env) |
60 | + | code, reason = load(command, "=stdin", "t", env) |
61 | - | end |
61 | + | end |
62 | - | end |
62 | + | end |
63 | - | if code then |
63 | + | if code then |
64 | - | local result = table.pack(xpcall(code, debug.traceback)) |
64 | + | local result = table.pack(xpcall(code, debug.traceback)) |
65 | - | if not result[1] then |
65 | + | if not result[1] then |
66 | - | if type(result[2]) == "table" and result[2].reason == "terminated" then |
66 | + | if type(result[2]) == "table" and result[2].reason == "terminated" then |
67 | - | os.exit(result[2].code) |
67 | + | os.exit(result[2].code) |
68 | - | end |
68 | + | end |
69 | - | io.stderr:write(tostring(result[2]) .. "\n") |
69 | + | io.stderr:write(tostring(result[2]) .. "\n") |
70 | - | else |
70 | + | else |
71 | - | for i = 2, result.n do |
71 | + | for i = 2, result.n do |
72 | - | term.write(serialization.serialize(result[i], true) .. "\t", true) |
72 | + | term.write(serialization.serialize(result[i], true) .. "\t", true) |
73 | - | end |
73 | + | end |
74 | - | if term.getCursor() > 1 then |
74 | + | if term.getCursor() > 1 then |
75 | - | term.write("\n") |
75 | + | term.write("\n") |
76 | - | end |
76 | + | end |
77 | - | end |
77 | + | end |
78 | - | else |
78 | + | else |
79 | - | io.stderr:write(tostring(reason) .. "\n") |
79 | + | io.stderr:write(tostring(reason) .. "\n") |
80 | - | end |
80 | + | end |
81 | - | end |
81 | + | end |
82 | end |