From 8fede6f507ec5a1775dbb655625e3a60a7387c26 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 27 Mar 2019 15:21:31 -0400 Subject: [PATCH] run autorun programs in shell mode --- sys/apps/autorun.lua | 70 ++++++++++++++++++++++++++++++ sys/autorun/clipboard.lua | 2 - sys/autorun/hotkeys.lua | 4 ++ sys/autorun/log.lua | 31 ++++++++------ sys/autorun/welcome.lua | 2 +- sys/extensions/1.device.lua | 5 --- sys/extensions/7.multishell.lua | 76 ++------------------------------- sys/kernel.lua | 33 ++++++++++++-- 8 files changed, 127 insertions(+), 96 deletions(-) create mode 100644 sys/apps/autorun.lua diff --git a/sys/apps/autorun.lua b/sys/apps/autorun.lua new file mode 100644 index 0000000..e7febc1 --- /dev/null +++ b/sys/apps/autorun.lua @@ -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 + diff --git a/sys/autorun/clipboard.lua b/sys/autorun/clipboard.lua index 2dd8017..ad3286f 100644 --- a/sys/autorun/clipboard.lua +++ b/sys/autorun/clipboard.lua @@ -1,5 +1,3 @@ -_G.requireInjector(_ENV) - local Util = require('util') local kernel = _G.kernel diff --git a/sys/autorun/hotkeys.lua b/sys/autorun/hotkeys.lua index 3c97e0e..cf50efb 100644 --- a/sys/autorun/hotkeys.lua +++ b/sys/autorun/hotkeys.lua @@ -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 diff --git a/sys/autorun/log.lua b/sys/autorun/log.lua index fe8de05..4f53ed4 100644 --- a/sys/autorun/log.lua +++ b/sys/autorun/log.lua @@ -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 diff --git a/sys/autorun/welcome.lua b/sys/autorun/welcome.lua index c94e177..df0b2fd 100644 --- a/sys/autorun/welcome.lua +++ b/sys/autorun/welcome.lua @@ -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) diff --git a/sys/extensions/1.device.lua b/sys/extensions/1.device.lua index 2dbdb91..5c57761 100644 --- a/sys/extensions/1.device.lua +++ b/sys/extensions/1.device.lua @@ -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 diff --git a/sys/extensions/7.multishell.lua b/sys/extensions/7.multishell.lua index 9bbfb2c..974afe4 100644 --- a/sys/extensions/7.multishell.lua +++ b/sys/extensions/7.multishell.lua @@ -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) diff --git a/sys/kernel.lua b/sys/kernel.lua index b81fd9e..2f53ddf 100644 --- a/sys/kernel.lua +++ b/sys/kernel.lua @@ -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)