1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-30 17:14:46 +00:00

support open os command line programs

This commit is contained in:
kepler155c@gmail.com 2019-04-07 10:09:47 -04:00
parent 737ac095de
commit 4f9bd8eb0f
9 changed files with 146 additions and 65 deletions

View File

@ -2,6 +2,8 @@ local fs = _G.fs
local linkfs = { } local linkfs = { }
-- TODO: implement broken links
local methods = { 'exists', 'getFreeSpace', 'getSize', local methods = { 'exists', 'getFreeSpace', 'getSize',
'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' } 'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' }

View File

@ -17,9 +17,11 @@ end
function Peripheral.addDevice(deviceList, side) function Peripheral.addDevice(deviceList, side)
local name = side local name = side
pcall(function()
local ptype = Peripheral.getType(side) local ptype = Peripheral.getType(side)
local dev = Peripheral.wrap(side)
if not ptype then if not ptype or not dev then
return return
end end
@ -28,6 +30,11 @@ function Peripheral.addDevice(deviceList, side)
-- ptype = 'wireless_modem' -- ptype = 'wireless_modem'
-- else -- else
ptype = 'wired_modem' ptype = 'wired_modem'
if dev.getMetadata then
-- avoid open computer relays being registered
-- as 'wired_modem'
ptype = dev.getMetadata().name or 'wired_modem'
end
end end
end end
@ -52,9 +59,7 @@ function Peripheral.addDevice(deviceList, side)
-- this can randomly fail -- this can randomly fail
if not deviceList[name] then if not deviceList[name] then
pcall(function() deviceList[name] = dev
deviceList[name] = Peripheral.wrap(side)
end)
if deviceList[name] then if deviceList[name] then
Util.merge(deviceList[name], { Util.merge(deviceList[name], {
@ -64,6 +69,7 @@ function Peripheral.addDevice(deviceList, side)
}) })
end end
end end
end)
return deviceList[name] return deviceList[name]
end end

View File

@ -659,6 +659,37 @@ function Util.wordWrap(str, limit)
return lines return lines
end end
-- https://github.com/MightyPirates/OpenComputers
function Util.parse(...)
local params = table.pack(...)
local args = {}
local options = {}
local doneWithOptions = false
for i = 1, params.n do
local param = params[i]
if not doneWithOptions and type(param) == "string" then
if param == "--" then
doneWithOptions = true -- stop processing options at `--`
elseif param:sub(1, 2) == "--" then
local key, value = param:match("%-%-(.-)=(.*)")
if not key then
key, value = param:sub(3), true
end
options[key] = value
elseif param:sub(1, 1) == "-" and param ~= "-" then
for j = 2, string.len(param) do
options[string.sub(param, j, j)] = true
end
else
table.insert(args, param)
end
else
table.insert(args, param)
end
end
return args, options
end
function Util.args(arg) function Util.args(arg)
local options, args = { }, { } local options, args = { }, { }

View File

@ -40,7 +40,7 @@ local topicPage = UI.Page {
backgroundColor = colors.black, backgroundColor = colors.black,
titleBar = UI.TitleBar { titleBar = UI.TitleBar {
title = 'text', title = 'text',
previousPage = true, event = 'back',
}, },
helpText = UI.TextArea { helpText = UI.TextArea {
backgroundColor = colors.black, backgroundColor = colors.black,
@ -52,9 +52,18 @@ local topicPage = UI.Page {
}, },
} }
function topicPage:enable(name)
local f = help.lookup(name)
self.titleBar.title = name
self.helpText:setText(f and Util.readFile(f) or 'No help available for ' .. name)
return UI.Page.enable(self)
end
function topicPage:eventHandler(event) function topicPage:eventHandler(event)
if event.type == 'back' then if event.type == 'back' then
UI:setPreviousPage() UI:setPage(page)
end end
return UI.Page.eventHandler(self, event) return UI.Page.eventHandler(self, event)
end end
@ -66,12 +75,8 @@ function page:eventHandler(event)
elseif event.type == 'grid_select' then elseif event.type == 'grid_select' then
if self.grid:getSelected() then if self.grid:getSelected() then
local name = self.grid:getSelected().name local name = self.grid:getSelected().name
local f = help.lookup(name)
topicPage.titleBar.title = name UI:setPage(topicPage, name)
topicPage.helpText:setText(Util.readFile(f))
UI:setPage(topicPage)
end end
elseif event.type == 'text_change' then elseif event.type == 'text_change' then
@ -93,5 +98,6 @@ function page:eventHandler(event)
end end
end end
UI:setPage(page) local args = { ... }
UI:setPage(#args[1] and topicPage or page, args[1])
UI:pullEvents() UI:pullEvents()

View File

@ -55,7 +55,7 @@ runDir('usr/autorun')
if not success then if not success then
if multishell then if multishell then
--multishell.setFocus(multishell.getCurrent()) multishell.setFocus(multishell.getCurrent())
end end
_G.printError('A startup program has errored') _G.printError('A startup program has errored')
print('Press enter to continue') print('Press enter to continue')

View File

@ -1,6 +1,8 @@
local Config = require('config') local Config = require('config')
local UI = require('ui') local UI = require('ui')
local kernel = _G.kernel
local aliasTab = UI.Tab { local aliasTab = UI.Tab {
tabTitle = 'Aliases', tabTitle = 'Aliases',
description = 'Shell aliases', description = 'Shell aliases',
@ -33,8 +35,12 @@ local aliasTab = UI.Tab {
function aliasTab.grid:draw() function aliasTab.grid:draw()
self.values = { } self.values = { }
local env = Config.load('shell') local env = Config.load('shell')
for k in pairs(kernel.getShell().aliases()) do
kernel.getShell().clearAlias(k)
end
for k,v in pairs(env.aliases) do for k,v in pairs(env.aliases) do
table.insert(self.values, { alias = k, path = v }) table.insert(self.values, { alias = k, path = v })
kernel.getShell().setAlias(k, v)
end end
self:update() self:update()
UI.Grid.draw(self) UI.Grid.draw(self)
@ -42,23 +48,23 @@ end
function aliasTab:eventHandler(event) function aliasTab:eventHandler(event)
if event.type == 'delete_alias' then if event.type == 'delete_alias' then
local env = Config.load('shell') local env = Config.load('shell', { aliases = { } })
env.aliases[self.grid:getSelected().alias] = nil env.aliases[self.grid:getSelected().alias] = nil
Config.update('shell', env)
self.grid:setIndex(self.grid:getIndex()) self.grid:setIndex(self.grid:getIndex())
self.grid:draw() self.grid:draw()
Config.update('shell', env) self:emit({ type = 'success_message', message = 'Aliases updated' })
self:emit({ type = 'success_message', message = 'reboot to take effect' })
return true return true
elseif event.type == 'new_alias' then elseif event.type == 'new_alias' then
local env = Config.load('shell') local env = Config.load('shell', { aliases = { } })
env.aliases[self.alias.value] = self.path.value env.aliases[self.alias.value] = self.path.value
Config.update('shell', env)
self.alias:reset() self.alias:reset()
self.path:reset() self.path:reset()
self:draw() self:draw()
self:setFocus(self.alias) self:setFocus(self.alias)
Config.update('shell', env) self:emit({ type = 'success_message', message = 'Aliases updated' })
self:emit({ type = 'success_message', message = 'reboot to take effect' })
return true return true
end end
end end

View File

@ -287,6 +287,7 @@ function fs.mount(path, fstype, ...)
tp.nodes[targetName] = node tp.nodes[targetName] = node
end end
end end
_debug(node)
return node return node
end end

27
sys/init/3.relay.lua Normal file
View File

@ -0,0 +1,27 @@
local device = _G.device
local kernel = _G.kernel
local function register(v)
if v and v.isWireless and v.getMetadata and v.getNamesRemote then
v.children = { }
for _, name in pairs(v.getNamesRemote()) do
local dev = v.getMethodsRemote(name)
if dev then
dev.name = name
dev.side = name
dev.type = v.getTypeRemote(name)
device[name] = dev
table.insert(v._children, dev)
end
end
end
end
for _,v in pairs(device) do
register(v)
end
-- register oc devices as peripherals
kernel.hook('device_attach', function(_, eventData)
register(device[eventData[2]])
end)

View File

@ -123,23 +123,25 @@ function multishell.openTab(tab)
local routine = kernel.newRoutine(tab) local routine = kernel.newRoutine(tab)
routine.co = coroutine.create(function() routine.co = coroutine.create(function()
local result, err, stacktrace local result, err
if tab.fn then if tab.fn then
result, err = Util.runFunction(routine.env, tab.fn, table.unpack(tab.args or { } )) result, err = Util.runFunction(routine.env, tab.fn, table.unpack(tab.args or { } ))
elseif tab.path then elseif tab.path then
result, err, stacktrace = xprun(routine.env, tab.path, table.unpack(tab.args or { } )) result, err = xprun(routine.env, tab.path, table.unpack(tab.args or { } ))
else else
err = 'multishell: invalid tab' err = 'multishell: invalid tab'
end end
if not result and err and err ~= 'Terminated' then if not result and err and err ~= 'Terminated' or (err and err ~= 0) then
if err then tab.terminal.setBackgroundColor(colors.black)
if tonumber(err) then
tab.terminal.setTextColor(colors.orange)
print('Process exited with error code: ' .. err)
elseif err then
printError(tostring(err)) printError(tostring(err))
if stacktrace then -- alternatively log stack to _debug
--print('\n' .. stacktrace)
end
end end
tab.terminal.setTextColor(colors.white)
print('\nPress enter to close') print('\nPress enter to close')
routine.isDead = true routine.isDead = true
routine.hidden = false routine.hidden = false