From 42bd4b2b69eff2a610c2fcdd53cb500f3af7afab Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 15 Jun 2020 19:54:42 -0600 Subject: [PATCH] remove preferred apps/alternates - same can be accomplished using aliases --- sys/apps/Files.lua | 3 +- sys/apps/Overview.lua | 7 ++-- sys/apps/ShellLauncher.lua | 4 +- sys/apps/network/telnet.lua | 4 +- sys/apps/shell.lua | 40 +++++++++++------- sys/apps/system/alternate.lua | 77 ---------------------------------- sys/modules/opus/alternate.lua | 48 --------------------- sys/modules/opus/fs/ramfs.lua | 32 +++++++++++++- 8 files changed, 64 insertions(+), 151 deletions(-) delete mode 100644 sys/apps/system/alternate.lua delete mode 100644 sys/modules/opus/alternate.lua diff --git a/sys/apps/Files.lua b/sys/apps/Files.lua index 8cb09cc..288d0a5 100644 --- a/sys/apps/Files.lua +++ b/sys/apps/Files.lua @@ -1,4 +1,3 @@ -local Alt = require('opus.alternate') local Config = require('opus.config') local Event = require('opus.event') local pastebin = require('opus.http.pastebin') @@ -351,7 +350,7 @@ function Browser:eventHandler(event) self:setStatus('Started cloud edit') elseif event.type == 'shell' then - self:run(Alt.get('shell')) + self:run('shell') elseif event.type == 'refresh' then self:updateDirectory(self.dir) diff --git a/sys/apps/Overview.lua b/sys/apps/Overview.lua index d8cb0ef..722127e 100644 --- a/sys/apps/Overview.lua +++ b/sys/apps/Overview.lua @@ -1,4 +1,3 @@ -local Alt = require('opus.alternate') local Array = require('opus.array') local class = require('opus.class') local Config = require('opus.config') @@ -469,13 +468,13 @@ function page:eventHandler(event) shell.switchTab(shell.openTab(event.button.app.run)) elseif event.type == 'shell' then - shell.switchTab(shell.openTab(Alt.get('shell'))) + shell.switchTab(shell.openTab('shell')) elseif event.type == 'lua' then - shell.switchTab(shell.openTab(Alt.get('lua'))) + shell.switchTab(shell.openTab('Lua')) elseif event.type == 'files' then - shell.switchTab(shell.openTab(Alt.get('files'))) + shell.switchTab(shell.openTab('Files')) elseif event.type == 'network' then shell.switchTab(shell.openTab('Network')) diff --git a/sys/apps/ShellLauncher.lua b/sys/apps/ShellLauncher.lua index 570234b..293bced 100644 --- a/sys/apps/ShellLauncher.lua +++ b/sys/apps/ShellLauncher.lua @@ -1,5 +1,3 @@ -local Alt = require('opus.alternate') - local kernel = _G.kernel local os = _G.os local shell = _ENV.shell @@ -20,7 +18,7 @@ kernel.hook('kernel_focus', function(_, eventData) end end if nextTab == launcherTab then - shell.switchTab(shell.openTab(Alt.get('shell'))) + shell.switchTab(shell.openTab('shell')) else shell.switchTab(nextTab.uid) end diff --git a/sys/apps/network/telnet.lua b/sys/apps/network/telnet.lua index ff92298..53a1ce1 100644 --- a/sys/apps/network/telnet.lua +++ b/sys/apps/network/telnet.lua @@ -1,9 +1,9 @@ -local Alt = require('opus.alternate') local Event = require('opus.event') local Socket = require('opus.socket') local Util = require('opus.util') local kernel = _G.kernel +local shell = _ENV.shell local term = _G.term local window = _G.window @@ -46,7 +46,7 @@ local function telnetHost(socket, mode) title = mode .. ' client', hidden = true, fn = function() - Util.run(kernel.makeEnv(_ENV), Alt.get('shell'), table.unpack(termInfo.program)) + Util.run(kernel.makeEnv(_ENV), shell.resolveProgram('shell'), table.unpack(termInfo.program)) if socket.queue then socket:write(socket.queue) end diff --git a/sys/apps/shell.lua b/sys/apps/shell.lua index febcde1..9672d6d 100644 --- a/sys/apps/shell.lua +++ b/sys/apps/shell.lua @@ -131,7 +131,14 @@ function shell.exit() end function shell.dir() return DIR end -function shell.setDir(d) DIR = d end +function shell.setDir(d) + d = fs.combine(d, '') + if not fs.isDir(d) then + error("Not a directory", 2) + end + DIR = d +end + function shell.path() return PATH end function shell.setPath(p) PATH = p end @@ -155,24 +162,29 @@ function shell.resolveProgram(_sCommand) local function inPath() -- Otherwise, look on the path variable - if not _sCommand:find('/') then - for sPath in string.gmatch(PATH or '', "[^:]+") do - sPath = fs.combine(sPath, _sCommand ) - if fs.exists(sPath) and not fs.isDir(sPath) then - return sPath - end - if fs.exists(sPath .. '.lua') then - return sPath .. '.lua' - end + for sPath in string.gmatch(PATH or '', "[^:]+") do + sPath = fs.combine(sPath, _sCommand ) + if check(sPath) then + return sPath + end + if check(sPath .. '.lua') then + return sPath .. '.lua' end end end - return check(_sCommand) - or check(_sCommand .. '.lua') - or check(shell.resolve(_sCommand)) + if not _sCommand:find('/') then + return inPath() + end + + -- so... even if you are in the rom directory and you run: + -- 'packages/common/edit.lua', allow this even though it + -- does not use a leading slash. Ideally, fs.combine would + -- provide the leading slash... but it does not. + return check(shell.resolve(_sCommand)) or check(shell.resolve(_sCommand) .. '.lua') - or inPath() + or check(_sCommand) + or check(_sCommand .. '.lua') end function shell.programs(_bIncludeHidden) diff --git a/sys/apps/system/alternate.lua b/sys/apps/system/alternate.lua deleted file mode 100644 index 35bd601..0000000 --- a/sys/apps/system/alternate.lua +++ /dev/null @@ -1,77 +0,0 @@ -local Array = require('opus.array') -local Config = require('opus.config') -local UI = require('opus.ui') - -local tab = UI.Tab { - title = 'Preferred', - description = 'Select preferred applications', - apps = UI.ScrollingGrid { - x = 2, y = 2, - ex = 12, ey = -3, - columns = { - { key = 'name' }, - }, - sortColumn = 'name', - disableHeader = true, - }, - choices = UI.Grid { - x = 14, y = 2, - ex = -2, ey = -3, - disableHeader = true, - columns = { - { key = 'file' }, - }, - getRowTextColor = function(self, row) - if row == self.values[1] then - return 'yellow' - end - return UI.Grid.getRowTextColor(self, row) - end, - }, - statusBar = UI.StatusBar { - values = 'Double-click to set as preferred' - }, -} - -function tab:updateChoices() - local app = self.apps:getSelected().name - local choices = { } - for _, v in pairs(self.config[app]) do - table.insert(choices, { file = v }) - end - self.choices:setValues(choices) - self.choices:draw() -end - -function tab:enable() - self.config = Config.load('alternate') - - local apps = { } - for k, _ in pairs(self.config) do - table.insert(apps, { name = k }) - end - self.apps:setValues(apps) - - self:updateChoices() - - UI.Tab.enable(self) -end - -function tab:eventHandler(event) - if event.type == 'grid_focus_row' and event.element == self.apps then - self:updateChoices() - - elseif event.type == 'grid_select' and event.element == self.choices then - local app = self.apps:getSelected().name - Array.removeByValue(self.config[app], event.selected.file) - table.insert(self.config[app], 1, event.selected.file) - self:updateChoices() - Config.update('alternate', self.config) - - else - return UI.Tab.eventHandler(self, event) - end - return true -end - -return tab diff --git a/sys/modules/opus/alternate.lua b/sys/modules/opus/alternate.lua deleted file mode 100644 index 101de58..0000000 --- a/sys/modules/opus/alternate.lua +++ /dev/null @@ -1,48 +0,0 @@ -local Array = require('opus.array') -local Config = require('opus.config') -local Util = require('opus.util') - -local function getConfig() - return Config.load('alternate', { - shell = { - 'sys/apps/shell.lua', - 'rom/programs/shell.lua', - }, - lua = { - 'sys/apps/Lua.lua', - 'rom/programs/lua.lua', - }, - files = { - 'sys/apps/Files.lua', - } - }) -end - -local Alt = { } - -function Alt.get(key) - return getConfig()[key][1] -end - -function Alt.set(key, value) - local config = getConfig() - Array.removeByValue(config[key], value) - table.insert(config[key], 1, value) - Config.update('alternate', config) -end - -function Alt.remove(key, value) - local config = getConfig() - Array.removeByValue(config[key], value) - Config.update('alternate', config) -end - -function Alt.add(key, value) - local config = getConfig() - if not Util.contains(config[key], value) then - table.insert(config[key], value) - Config.update('alternate', config) - end -end - -return Alt diff --git a/sys/modules/opus/fs/ramfs.lua b/sys/modules/opus/fs/ramfs.lua index 9c5d718..9e8f613 100644 --- a/sys/modules/opus/fs/ramfs.lua +++ b/sys/modules/opus/fs/ramfs.lua @@ -79,10 +79,40 @@ function ramfs.list(node, dir) end function ramfs.open(node, fn, fl) - if fl ~= 'r' and fl ~= 'w' and fl ~= 'rb' and fl ~= 'wb' then + local modes = Util.transpose { 'r', 'w', 'rb', 'wb', 'a' } + if not modes[fl] then error('Unsupported mode') end + if fl == 'a' then + if node.mountPoint ~= fn then + fl = 'w' + else + local c = type(node.contents) == 'table' + and string.char(table.unpack(node.contents)) + or node.contents + or '' + + return { + write = function(str) + c = c .. str + end, + writeLine = function(str) + c = c .. str .. '\n' + end, + flush = function() + node.contents = c + node.size = #c + end, + close = function() + node.contents = c + node.size = #c + c = nil + end, + } + end + end + if fl == 'r' then if node.mountPoint ~= fn then return