Skip to content

Commit 69cc18b

Browse files
committedApr 6, 2015
In the meantime... this was easier than I thought, after remembering I could just getmetatable my way up...

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed
 

Diff for: ‎src/main/resources/assets/opencomputers/loot/OpenOS/bin/lua.lua

+69-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,77 @@ if #args == 0 or options.i then
2727
return module
2828
end
2929
end
30-
setmetatable(env, {__index = function(t, k)
31-
return _ENV[k] or optrequire(k)
32-
end})
30+
setmetatable(env, {
31+
__index = function(t, k)
32+
_ENV[k] = _ENV[k] or optrequire(k)
33+
return _ENV[k]
34+
end,
35+
__pairs = function(self)
36+
local t = self
37+
return function(_, key)
38+
local k, v = next(t, key)
39+
if not k and t == env then
40+
t = _ENV
41+
k, v = next(t)
42+
end
43+
if not k and t == _ENV then
44+
t = package.loaded
45+
k, v = next(t)
46+
end
47+
return k, v
48+
end
49+
end
50+
})
3351

3452
local history = {}
3553

54+
local function findTable(t, path)
55+
if type(t) ~= "table" then return nil end
56+
if not path or #path == 0 then return t end
57+
local name = string.match(path, "[^.]+")
58+
for k, v in pairs(t) do
59+
if k == name then
60+
return findTable(v, string.sub(path, #name + 2))
61+
end
62+
end
63+
local mt = getmetatable(t)
64+
if t == env then mt = {__index=_ENV} end
65+
if mt then
66+
return findTable(mt.__index, path)
67+
end
68+
return nil
69+
end
70+
local function findKeys(t, r, prefix, name)
71+
if type(t) ~= "table" then return end
72+
for k, v in pairs(t) do
73+
if string.match(k, "^"..name) then
74+
local postfix = ""
75+
if type(v) == "function" then postfix = "()"
76+
elseif type(v) == "table" and getmetatable(v) and getmetatable(v).__call then postfix = "()"
77+
elseif type(v) == "table" then postfix = "."
78+
end
79+
table.insert(r, prefix..k..postfix)
80+
end
81+
end
82+
local mt = getmetatable(t)
83+
if t == env then mt = {__index=_ENV} end
84+
if mt then
85+
return findKeys(mt.__index, r, prefix, name)
86+
end
87+
end
88+
local function hint(line, index)
89+
local path = string.match(line, "[a-zA-Z_][a-zA-Z0-9_.]*$")
90+
if not path then return nil end
91+
local suffix = string.match(path, "[^.]+$") or ""
92+
local prefix = string.sub(path, 1, #path - #suffix)
93+
local t = findTable(env, prefix)
94+
if not t then return nil end
95+
local r = {}
96+
findKeys(t, r, string.sub(line, 1, #line - #suffix), suffix)
97+
table.sort(r)
98+
return r
99+
end
100+
36101
component.gpu.setForeground(0xFFFFFF)
37102
term.write("Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio\n")
38103
component.gpu.setForeground(0xFFFF00)
@@ -45,7 +110,7 @@ if #args == 0 or options.i then
45110
local foreground = component.gpu.setForeground(0x00FF00)
46111
term.write(tostring(env._PROMPT or "lua> "))
47112
component.gpu.setForeground(foreground)
48-
local command = term.read(history)
113+
local command = term.read(history, nil, hint)
49114
if command == nil then -- eof
50115
return
51116
end

Diff for: ‎src/main/resources/assets/opencomputers/loot/OpenOS/boot/04_component.lua

+21-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@ local primaries = {}
1010

1111
-- This allows writing component.modem.open(123) instead of writing
1212
-- component.getPrimary("modem").open(123), which may be nicer to read.
13-
setmetatable(component, { __index = function(_, key)
14-
return component.getPrimary(key)
15-
end })
13+
setmetatable(component, {
14+
__index = function(_, key)
15+
return component.getPrimary(key)
16+
end,
17+
__pairs = function(self)
18+
local parent = false
19+
return function(_, key)
20+
if parent then
21+
return next(primaries, key)
22+
else
23+
local k, v = next(self, key)
24+
if not k then
25+
parent = true
26+
return next(primaries)
27+
else
28+
return k, v
29+
end
30+
end
31+
end
32+
end
33+
})
1634

1735
function component.get(address, componentType)
1836
checkArg(1, address, "string")

0 commit comments

Comments
 (0)