1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-10 18:59:55 +00:00
opus/sys/apps/Lua.lua

388 lines
8.0 KiB
Lua
Raw Normal View History

2019-02-12 22:03:50 +00:00
-- Lua may be called from outside of shell - inject a require
_G.requireInjector(_ENV)
local History = require('opus.history')
local UI = require('opus.ui')
local Util = require('opus.util')
2016-12-11 19:24:52 +00:00
2017-10-14 07:41:54 +00:00
local colors = _G.colors
local os = _G.os
2017-10-13 20:30:47 +00:00
local textutils = _G.textutils
2018-01-22 03:08:30 +00:00
local term = _G.term
2017-10-08 21:45:01 +00:00
2018-01-24 22:39:38 +00:00
local _exit
2017-10-08 21:45:01 +00:00
local sandboxEnv = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
2018-01-24 22:39:38 +00:00
sandboxEnv.exit = function() _exit = true end
2017-10-14 07:41:54 +00:00
sandboxEnv._echo = function( ... ) return { ... } end
2018-01-21 10:44:13 +00:00
_G.requireInjector(sandboxEnv)
2016-12-11 19:24:52 +00:00
UI:configure('Lua', ...)
local command = ''
local counter = 1
2017-05-20 22:27:26 +00:00
local history = History.load('usr/.lua_history', 25)
2016-12-11 19:24:52 +00:00
2017-10-03 04:50:54 +00:00
local page = UI.Page {
2018-01-22 03:08:30 +00:00
menuBar = UI.MenuBar {
buttons = {
{ text = 'Local', event = 'local' },
{ text = 'Global', event = 'global' },
{ text = 'Device', event = 'device', name = 'Device' },
},
},
prompt = UI.TextEntry {
y = 2,
shadowText = 'enter command',
2019-03-29 13:56:56 +00:00
limit = 1024,
2018-01-22 03:08:30 +00:00
accelerators = {
enter = 'command_enter',
up = 'history_back',
down = 'history_forward',
mouse_rightclick = 'clear_prompt',
[ 'control-space' ] = 'autocomplete',
},
},
tabs = UI.Tabs {
y = 3,
[1] = UI.Tab {
tabTitle = 'Formatted',
grid = UI.ScrollingGrid {
columns = {
{ heading = 'Key', key = 'name' },
{ heading = 'Value', key = 'value' },
},
sortColumn = 'name',
autospace = true,
},
},
[2] = UI.Tab {
tabTitle = 'Output',
output = UI.Embedded {
visible = true,
maxScroll = 1000,
backgroundColor = colors.black,
},
2018-01-22 03:08:30 +00:00
},
},
2017-10-03 04:50:54 +00:00
}
2016-12-11 19:24:52 +00:00
page.grid = page.tabs[1].grid
page.output = page.tabs[2].output
2016-12-18 19:38:48 +00:00
function page:setPrompt(value, focus)
2018-01-22 03:08:30 +00:00
self.prompt:setValue(value)
if value:sub(-1) == ')' then
self.prompt:setPosition(#value - 1)
2019-03-29 13:56:56 +00:00
else
self.prompt:setPosition(#value)
2018-01-22 03:08:30 +00:00
end
self.prompt:draw()
if focus then
page:setFocus(self.prompt)
end
2016-12-11 19:24:52 +00:00
end
2016-12-18 19:38:48 +00:00
function page:enable()
2018-01-22 03:08:30 +00:00
UI.Page.enable(self)
self:setFocus(self.prompt)
2016-12-11 19:24:52 +00:00
end
2016-12-27 21:01:06 +00:00
local function autocomplete(env, oLine, x)
2018-01-22 03:08:30 +00:00
local sLine = oLine:sub(1, x)
local nStartPos = sLine:find("[a-zA-Z0-9_%.]+$")
if nStartPos then
sLine = sLine:sub(nStartPos)
end
if #sLine > 0 then
local results = textutils.complete(sLine, env)
if #results == 1 then
return Util.insertString(oLine, results[1], x + 1)
elseif #results > 1 then
local prefix = results[1]
for n = 1, #results do
local result = results[n]
while #prefix > 0 do
if result:find(prefix, 1, true) == 1 then
break
end
prefix = prefix:sub(1, #prefix - 1)
end
end
if #prefix > 0 then
return Util.insertString(oLine, prefix, x + 1)
end
end
end
return oLine
2016-12-27 03:26:43 +00:00
end
2016-12-18 19:38:48 +00:00
function page:eventHandler(event)
2018-01-22 03:08:30 +00:00
if event.type == 'global' then
2018-01-24 22:39:38 +00:00
self:setPrompt('_G', true)
2018-01-22 03:08:30 +00:00
self:executeStatement('_G')
command = nil
elseif event.type == 'local' then
2018-01-24 22:39:38 +00:00
self:setPrompt('_ENV', true)
2018-01-22 03:08:30 +00:00
self:executeStatement('_ENV')
command = nil
elseif event.type == 'hide_output' then
self.output:disable()
self.titleBar.oy = -1
self.titleBar.event = 'show_output'
self.titleBar.closeInd = '^'
self.titleBar:resize()
self.grid.ey = -2
self.grid:resize()
self:draw()
elseif event.type == 'tab_select' then
self:setFocus(self.prompt)
2018-01-22 03:08:30 +00:00
elseif event.type == 'show_output' then
self.tabs:selectTab(self.tabs[2])
2018-01-22 03:08:30 +00:00
elseif event.type == 'autocomplete' then
local sz = #self.prompt.value
2019-03-29 13:56:56 +00:00
local pos = self.prompt.entry.pos
self:setPrompt(autocomplete(sandboxEnv, self.prompt.value, self.prompt.entry.pos))
2018-01-22 03:08:30 +00:00
self.prompt:setPosition(pos + #self.prompt.value - sz)
self.prompt:updateCursor()
elseif event.type == 'device' then
self:setPrompt('device', true)
self:executeStatement('device')
elseif event.type == 'history_back' then
local value = history:back()
if value then
self:setPrompt(value)
end
elseif event.type == 'history_forward' then
self:setPrompt(history:forward() or '')
elseif event.type == 'clear_prompt' then
self:setPrompt('')
history:reset()
elseif event.type == 'command_enter' then
local s = tostring(self.prompt.value)
if #s > 0 then
self:executeStatement(s)
else
local t = { }
for k = #history.entries, 1, -1 do
table.insert(t, {
name = #t + 1,
value = history.entries[k],
isHistory = true,
pos = k,
})
end
history:reset()
command = nil
self.grid:setValues(t)
self.grid:setIndex(1)
self.grid:adjustWidth()
self:draw()
end
return true
else
return UI.Page.eventHandler(self, event)
end
return true
2016-12-11 19:24:52 +00:00
end
2016-12-18 19:38:48 +00:00
function page:setResult(result)
2018-01-22 03:08:30 +00:00
local t = { }
local function safeValue(v)
if type(v) == 'string' or type(v) == 'number' then
return v
end
return tostring(v)
end
if type(result) == 'table' then
for k,v in pairs(result) do
local entry = {
name = safeValue(k),
rawName = k,
value = safeValue(v),
rawValue = v,
}
if type(v) == 'table' then
if Util.size(v) == 0 then
entry.value = 'table: (empty)'
else
entry.value = tostring(v)
end
end
table.insert(t, entry)
end
else
table.insert(t, {
name = type(result),
value = tostring(result),
rawValue = result,
})
end
self.grid:setValues(t)
self.grid:setIndex(1)
self.grid:adjustWidth()
self:draw()
2016-12-11 19:24:52 +00:00
end
2016-12-18 19:38:48 +00:00
function page.grid:eventHandler(event)
2018-01-22 03:08:30 +00:00
local entry = self:getSelected()
local function commandAppend()
if entry.isHistory then
--history.setPosition(entry.pos)
return entry.value
end
if type(entry.rawValue) == 'function' then
if command then
return command .. '.' .. entry.name .. '()'
end
return entry.name .. '()'
end
if command then
if type(entry.rawName) == 'number' then
return command .. '[' .. entry.name .. ']'
end
if entry.name:match("%W") or
entry.name:sub(1, 1):match("%d") then
return command .. "['" .. tostring(entry.name) .. "']"
end
return command .. '.' .. entry.name
end
return entry.name
end
if event.type == 'grid_focus_row' then
if self.focused then
page:setPrompt(commandAppend())
end
elseif event.type == 'grid_select' then
page:setPrompt(commandAppend(), true)
page:executeStatement(commandAppend())
elseif event.type == 'copy' then
if entry then
os.queueEvent('clipboard_copy', entry.rawValue)
end
else
return UI.ScrollingGrid.eventHandler(self, event)
end
return true
2016-12-11 19:24:52 +00:00
end
2016-12-18 19:38:48 +00:00
function page:rawExecute(s)
2018-01-22 03:08:30 +00:00
local fn, m
local wrapped
2017-10-14 07:41:54 +00:00
2018-01-22 03:08:30 +00:00
fn = load('return (' ..s.. ')', 'lua', nil, sandboxEnv)
2017-10-14 07:41:54 +00:00
2018-01-22 03:08:30 +00:00
if fn then
fn = load('return {' ..s.. '}', 'lua', nil, sandboxEnv)
wrapped = true
2018-01-22 03:08:30 +00:00
end
2017-10-14 07:41:54 +00:00
2019-06-18 19:19:24 +00:00
local t = os.clock()
2018-01-22 03:08:30 +00:00
if fn then
fn, m = pcall(fn)
if #m <= 1 and wrapped then
2018-01-22 03:08:30 +00:00
m = m[1]
end
else
fn, m = load(s, 'lua', nil, sandboxEnv)
if fn then
2019-06-18 19:19:24 +00:00
t = os.clock()
fn, m = pcall(fn)
end
2018-01-22 03:08:30 +00:00
end
2016-12-11 19:24:52 +00:00
2018-01-22 03:08:30 +00:00
if fn then
2019-06-18 19:19:24 +00:00
t = os.clock() - t
local bg, fg = term.getBackgroundColor(), term.getTextColor()
term.setTextColor(colors.cyan)
term.setBackgroundColor(colors.black)
term.write(string.format('out [%.2f]: ', t))
term.setBackgroundColor(bg)
term.setTextColor(fg)
if m or wrapped then
Util.print(m or 'nil')
2019-06-18 19:19:24 +00:00
else
print()
end
else
_G.printError(m)
2018-01-22 03:08:30 +00:00
end
2016-12-11 19:24:52 +00:00
2018-01-22 03:08:30 +00:00
return fn, m
2016-12-11 19:24:52 +00:00
end
2016-12-18 19:38:48 +00:00
function page:executeStatement(statement)
2018-01-22 03:08:30 +00:00
command = statement
history:add(statement)
history:back()
2018-01-24 22:39:38 +00:00
local s, m
2018-01-22 03:08:30 +00:00
local oterm = term.redirect(self.output.win)
2019-02-06 04:03:57 +00:00
self.output.win.scrollBottom()
local bg, fg = term.getBackgroundColor(), term.getTextColor()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.green)
term.write(string.format('in [%d]: ', counter))
term.setBackgroundColor(bg)
term.setTextColor(fg)
print(tostring(statement))
2018-01-24 22:39:38 +00:00
pcall(function()
s, m = self:rawExecute(command)
end)
2018-01-22 03:08:30 +00:00
term.redirect(oterm)
counter = counter + 1
2018-01-22 03:08:30 +00:00
if s and m then
self:setResult(m)
else
self.grid:setValues({ })
self.grid:draw()
2018-01-24 22:39:38 +00:00
if m and not self.output.enabled then
self:emit({ type = 'show_output' })
2018-01-22 03:08:30 +00:00
end
end
2018-01-24 22:39:38 +00:00
if _exit then
UI:exitPullEvents()
end
2016-12-11 19:24:52 +00:00
end
2016-12-23 04:22:04 +00:00
local args = { ... }
if args[1] then
2018-01-22 03:08:30 +00:00
command = 'args[1]'
sandboxEnv.args = args
page:setResult(args[1])
2016-12-18 19:38:48 +00:00
end
UI:setPage(page)
2018-01-24 22:39:38 +00:00
UI:pullEvents()