@@ -27,12 +27,77 @@ if #args == 0 or options.i then
27
27
return module
28
28
end
29
29
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
+ })
33
51
34
52
local history = {}
35
53
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
+
36
101
component .gpu .setForeground (0xFFFFFF )
37
102
term .write (" Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio\n " )
38
103
component .gpu .setForeground (0xFFFF00 )
@@ -45,7 +110,7 @@ if #args == 0 or options.i then
45
110
local foreground = component .gpu .setForeground (0x00FF00 )
46
111
term .write (tostring (env ._PROMPT or " lua> " ))
47
112
component .gpu .setForeground (foreground )
48
- local command = term .read (history )
113
+ local command = term .read (history , nil , hint )
49
114
if command == nil then -- eof
50
115
return
51
116
end
0 commit comments