run autorun programs in shell mode

This commit is contained in:
kepler155c@gmail.com 2019-03-27 15:21:31 -04:00
parent 82f6d3451d
commit 8fede6f507
8 changed files with 127 additions and 96 deletions

70
sys/apps/autorun.lua Normal file
View File

@ -0,0 +1,70 @@
local Packages = require('packages')
local colors = _G.colors
local fs = _G.fs
local keys = _G.keys
local multishell = _ENV.multishell
local os = _G.os
local shell = _ENV.shell
local term = _G.term
local success = true
local function runDir(directory)
if not fs.exists(directory) then
return true
end
local files = fs.list(directory)
table.sort(files)
for _,file in ipairs(files) do
os.sleep(0)
local result, err = shell.run(directory .. '/' .. file)
if result then
if term.isColor() then
term.setTextColor(colors.green)
end
term.write('[PASS] ')
term.setTextColor(colors.white)
term.write(fs.combine(directory, file))
print()
else
if term.isColor() then
term.setTextColor(colors.red)
end
term.write('[FAIL] ')
term.setTextColor(colors.white)
term.write(fs.combine(directory, file))
if err then
_G.printError('\n' .. err)
end
print()
success = false
end
end
end
runDir('sys/autorun')
for name in pairs(Packages:installed()) do
local packageDir = 'packages/' .. name .. '/autorun'
runDir(packageDir)
end
runDir('usr/autorun')
if not success then
if multishell then
--multishell.setFocus(multishell.getCurrent())
end
_G.printError('A startup program has errored')
print('Press enter to continue')
while true do
local e, code = os.pullEventRaw('key')
if e == 'terminate' or e == 'key' and code == keys.enter then
break
end
end
end

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Util = require('util')
local kernel = _G.kernel

View File

@ -4,6 +4,10 @@ local kernel = _G.kernel
local keyboard = _G.device.keyboard
local multishell = _ENV.multishell
if not multishell or not multishell.getTabs then
return
end
-- overview
keyboard.addHotkey('control-o', function()
for _,tab in pairs(multishell.getTabs()) do

View File

@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
--[[
Adds a task and the control-d hotkey to view the kernel log.
--]]
@ -13,12 +11,14 @@ local term = _G.term
local function systemLog()
local routine = kernel.getCurrent()
local w, h = kernel.window.getSize()
kernel.window.reposition(1, 2, w, h - 1)
if multishell and multishell.openTab then
local w, h = kernel.window.getSize()
kernel.window.reposition(1, 2, w, h - 1)
routine.terminal = kernel.window
routine.window = kernel.window
term.redirect(kernel.window)
routine.terminal = kernel.window
routine.window = kernel.window
term.redirect(kernel.window)
end
kernel.hook('mouse_scroll', function(_, eventData)
local dir, y = eventData[1], eventData[3]
@ -50,8 +50,15 @@ local function systemLog()
keyboard.removeHotkey('control-d')
end
multishell.openTab({
title = 'System Log',
fn = systemLog,
hidden = true,
})
if multishell and multishell.openTab then
multishell.openTab({
title = 'System Log',
fn = systemLog,
hidden = true,
})
else
kernel.run({
title = 'Syslog',
fn = systemLog,
})
end

View File

@ -3,7 +3,7 @@ local Config = require('config')
local shell = _ENV.shell
local config = Config.load('os')
if not config.welcomed then
if not config.welcomed and shell.openForegroundTab then
config.welcomed = true
Config.update('os', config)

View File

@ -113,11 +113,6 @@ kernel.hook({ 'mouse_click', 'mouse_up', 'mouse_drag' }, function(event, eventDa
end
end)
kernel.hook('kernel_focus', function()
--Util.clear(keyboard.state)
--Util.clear(mouse.state)
end)
function keyboard.addHotkey(code, fn)
keyboard.hotkeys[code] = fn
end

View File

@ -1,7 +1,6 @@
_G.requireInjector(_ENV)
local Config = require('config')
local Packages = require('packages')
local trace = require('trace')
local Util = require('util')
@ -12,7 +11,6 @@ local keys = _G.keys
local os = _G.os
local printError = _G.printError
local shell = _ENV.shell
local term = _G.term
local window = _G.window
local parentTerm = _G.device.terminal
@ -186,23 +184,7 @@ function multishell.getCount()
return #kernel.routines
end
kernel.hook('kernel_focus', function(_, eventData)
local previous = eventData[2]
if previous then
local routine = kernel.find(previous)
if routine and routine.window then
routine.window.setVisible(false)
if routine.hidden then
kernel.lower(previous)
end
end
end
local focused = kernel.find(eventData[1])
if focused and focused.window then
focused.window.setVisible(true)
end
kernel.hook('kernel_focus', function()
redrawMenu()
end)
@ -349,59 +331,6 @@ kernel.hook('mouse_scroll', function(_, eventData)
eventData[3] = eventData[3] - 1
end)
local function startup()
local success = true
local function runDir(directory, open)
if not fs.exists(directory) then
return true
end
local files = fs.list(directory)
table.sort(files)
for _,file in ipairs(files) do
os.sleep(0)
local result, err = open(directory .. '/' .. file)
if result then
if term.isColor() then
term.setTextColor(colors.green)
end
term.write('[PASS] ')
term.setTextColor(colors.white)
term.write(fs.combine(directory, file))
print()
else
if term.isColor() then
term.setTextColor(colors.red)
end
term.write('[FAIL] ')
term.setTextColor(colors.white)
term.write(fs.combine(directory, file))
if err then
_G.printError('\n' .. err)
end
print()
success = false
end
end
end
runDir('sys/autorun', shell.run)
for name in pairs(Packages:installed()) do
local packageDir = 'packages/' .. name .. '/autorun'
runDir(packageDir, shell.run)
end
runDir('usr/autorun', shell.run)
if not success then
multishell.setFocus(multishell.getCurrent())
printError('\nA startup program has errored')
os.pullEvent('terminate')
end
end
kernel.hook('kernel_ready', function()
overviewId = multishell.openTab({
path = 'sys/apps/Overview.lua',
@ -411,7 +340,8 @@ kernel.hook('kernel_ready', function()
})
multishell.openTab({
fn = startup,
path = 'sys/apps/shell.lua',
args = { 'sys/apps/autorun.lua' },
title = 'Autorun',
})
end)

View File

@ -14,6 +14,7 @@ local kernel = _G.kernel
local os = _G.os
local shell = _ENV.shell
local term = _G.term
local window = _G.window
local w, h = term.getSize()
kernel.terminal = term.current()
@ -69,6 +70,23 @@ end
local Routine = { }
local function switch(routine, previous)
if routine then
if previous and previous.window then
previous.window.setVisible(false)
if previous.hidden then
kernel.lower(previous.uid)
end
end
if routine and routine.window then
routine.window.setVisible(true)
end
os.queueEvent('kernel_focus', routine.uid, previous and previous.uid)
end
end
function Routine:resume(event, ...)
if not self.co or coroutine.status(self.co) == 'dead' then
return
@ -97,7 +115,7 @@ function Routine:resume(event, ...)
if coroutine.status(self.co) == 'dead' then
Util.removeByValue(kernel.routines, self)
if #kernel.routines > 0 then
os.queueEvent('kernel_focus', kernel.routines[1].uid)
switch(kernel.routines[1])
end
if self.haltOnExit then
kernel.halt()
@ -174,7 +192,10 @@ function kernel.raise(uid)
Util.removeByValue(kernel.routines, routine)
table.insert(kernel.routines, 1, routine)
end
os.queueEvent('kernel_focus', routine.uid, previous and previous.uid)
switch(routine, previous)
-- local previous = eventData[2]
-- local routine = kernel.find(previous)
return true
end
return false
@ -276,13 +297,19 @@ local function init(...)
if args[1] then
kernel.hook('kernel_ready', function()
term.redirect(kernel.window)
shell.run('sys/apps/autorun.lua')
local shellWindow = window.create(kernel.terminal, 1, 1, w, h, false)
local s, m = kernel.run({
title = args[1],
path = 'sys/apps/shell.lua',
args = args,
haltOnExit = true,
haltOnError = true,
terminal = kernel.terminal,
terminal = shellWindow,
window = shellWindow,
})
if s then
kernel.raise(s.uid)