diff --git a/sys/apis/util.lua b/sys/apis/util.lua index 54a992c..40f3cb2 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -221,6 +221,7 @@ function Util.findAll(t, name, value) end function Util.shallowCopy(t) + if not t then error('Util.shallowCopy: invalid table', 2) end local t2 = { } for k,v in pairs(t) do t2[k] = v diff --git a/sys/apps/Package.lua b/sys/apps/Package.lua index d30a77d..1f0d212 100644 --- a/sys/apps/Package.lua +++ b/sys/apps/Package.lua @@ -4,21 +4,16 @@ local Git = require('git') local Packages = require('packages') local Util = require('util') -local fs = _G.fs -local term = _G.term +local fs = _G.fs +local term = _G.term -local args = { ... } -local action = table.remove(args, 1) +local args = { ... } +local action = table.remove(args, 1) local function Syntax(msg) - error(msg) -end - -if action == 'list' then - for k in pairs(Packages:list()) do - Util.print('[%s] %s', Packages:isInstalled(k) and 'x' or ' ', k) - end - return + _G.printError(msg) + print('\nSyntax: Package list | install [name] ... | update [name] | uninstall [name]') + error(0) end local function progress(max) @@ -61,6 +56,13 @@ local function install(name) return end +if action == 'list' then + for k in pairs(Packages:list()) do + Util.print('[%s] %s', Packages:isInstalled(k) and 'x' or ' ', k) + end + return +end + if action == 'install' then local name = args[1] or Syntax('Invalid package') if Packages:isInstalled(name) then @@ -92,4 +94,4 @@ if action == 'uninstall' then return end -error('Syntax: Package [list | install [name] ... | update [name] | uninstall [name]') +Syntax('Invalid command') diff --git a/sys/apps/PackageManager.lua b/sys/apps/PackageManager.lua new file mode 100644 index 0000000..717c167 --- /dev/null +++ b/sys/apps/PackageManager.lua @@ -0,0 +1,159 @@ +_G.requireInjector(_ENV) + +local Ansi = require('ansi') +local Packages = require('packages') +local UI = require('ui') +local Util = require('util') + +local colors = _G.colors +local shell = _ENV.shell +local term = _G.term + +UI:configure('PackageManager', ...) + +local page = UI.Page { + grid = UI.ScrollingGrid { + y = 2, ey = 7, x = 2, ex = -6, + values = { }, + columns = { + { heading = 'Package', key = 'name' }, + }, + sortColumn = 'name', + autospace = true, + help = 'Select a package', + }, + add = UI.Button { + x = -4, y = 4, + text = '+', + event = 'action', + help = 'Install or update', + }, + remove = UI.Button { + x = -4, y = 6, + text = '-', + event = 'action', + operation = 'uninstall', + operationText = 'Remove', + help = 'Remove', + }, + description = UI.TextArea { + x = 2, y = 9, ey = -2, + --backgroundColor = colors.white, + }, + statusBar = UI.StatusBar { }, + action = UI.SlideOut { + backgroundColor = colors.cyan, + titleBar = UI.TitleBar { + event = 'hide-action', + }, + button = UI.Button { + ex = -4, y = 4, width = 7, + text = 'Begin', event = 'begin', + }, + output = UI.Embedded { + y = 6, ey = -2, x = 2, ex = -2, + }, + statusBar = UI.StatusBar { + backgroundColor = colors.cyan, + }, + }, +} + +function page.grid:getRowTextColor(row, selected) + if row.installed then + return colors.yellow + end + return UI.Grid.getRowTextColor(self, row, selected) +end + +function page.action:show() + UI.SlideOut.show(self) + self.output:draw() + self.output.win.redraw() +end + +function page:run(operation, name) + local oterm = term.redirect(self.action.output.win) + self.action.output:clear() + local cmd = string.format('Package %s %s', operation, name) + --for _ = 1, 3 do + -- print(cmd .. '\n') + -- os.sleep(1) + --end + term.setCursorPos(1, 1) + term.clear() + term.setTextColor(colors.yellow) + print(cmd .. '\n') + term.setTextColor(colors.white) + shell.run(cmd) + term.redirect(oterm) + self.action.output:draw() +end + +function page:updateSelection(selected) + self.add.operation = selected.installed and 'update' or 'install' + self.add.operationText = selected.installed and 'Update' or 'Install' +end + +function page:eventHandler(event) + if event.type == 'focus_change' then + self.statusBar:setStatus(event.focused.help) + + elseif event.type == 'grid_focus_row' then + local manifest = event.selected.manifest + + self.description.value = string.format('%s%s\n\n%s%s', + Ansi.yellow, manifest.title, + Ansi.white, manifest.description) + self.description:draw() + self:updateSelection(event.selected) + + elseif event.type == 'action' then + local selected = self.grid:getSelected() + if selected then + self.operation = event.button.operation + self.action.button.text = event.button.operationText + self.action.titleBar.title = selected.manifest.title + self.action.button.text = 'Begin' + self.action.button.event = 'begin' + self.action:show() + end + + elseif event.type == 'hide-action' then + self.action:hide() + + elseif event.type == 'begin' then + local selected = self.grid:getSelected() + self:run(self.operation, selected.name) + selected.installed = Packages:isInstalled(selected.name) + + self:updateSelection(selected) + self.action.button.text = 'Done' + self.action.button.event = 'hide-action' + self.action.button:draw() + + elseif event.type == 'quit' then + UI:exitPullEvents() + end + UI.Page.eventHandler(self, event) +end + +for k in pairs(Packages:list()) do + local manifest = Packages:getManifest(k) + if not manifest then + manifest = { + invalid = true, + description = 'Unable to download manifest', + title = '', + } + end + table.insert(page.grid.values, { + installed = not not Packages:isInstalled(k), + name = k, + manifest = manifest, + }) +end +page.grid:update() + +UI:setPage(page) +UI:pullEvents() diff --git a/sys/etc/app.db b/sys/etc/app.db index 68b9abc..19611c5 100644 --- a/sys/etc/app.db +++ b/sys/etc/app.db @@ -1,11 +1,16 @@ { + [ "0a999012ffb87b3edac99adbdfc498b12831a1e2" ] = { + title = "Packages", + category = "System", + run = "PackageManager.lua", + }, [ "53ebc572b4a44802ba114729f07bdaaf5409a9d7" ] = { title = "Network", category = "Apps", icon = "\0304 \030 \ \030f \0304 \0307 \030 \031 \031f)\ \030f \0304 \0307 \030 \031f)", - iconExt = "\030 \031f \0305\031f\140\030f\0315\137\144\ + iconExt = "\030 \031f \0305\031f\140\030f\0315\137\144\ \030 \031f\030f\0314\131\131\0304\031f\148\030 \0305\155\150\149\ \030 \031f\030f\0310\147\0300\031f\141\0304\149\0307\0318\149\030 ", run = "Network.lua", @@ -16,7 +21,7 @@ icon = "\0304\031f \030f\0310o..\0304\031f \ \0304\031f \030f\0310.o.\0304\031f \ \0304\031f - ", - iconExt = "\0307\031f\135\0300\0317\159\0307\0310\144\031f\139\ + iconExt = "\0307\031f\135\0300\0317\159\0307\0310\144\031f\139\ \0300\0317\131\0307\0310\147\0300\0317\156\131\ \130\143\143\129", run = "rom/programs/reboot", @@ -27,7 +32,7 @@ icon = "\0301\03171\03180\030 \031 \ \0301\03181\030 \031 \ \0301\03170\03180\03171\0307\031f>", - iconExt = "\031f\128\0313\152\131\131\132\031f\128\ + iconExt = "\031f\128\0313\152\131\131\132\031f\128\ \0313\139\159\129\0303\031f\159\129\139\ \031f\128\0313\136\0303\031f\143\143\030f\0313\134\031f\128", run = "http://pastebin.com/raw/UzGHLbNC", @@ -38,7 +43,7 @@ icon = " \031f?\031 \ \031f?\031 \ \031f?", - iconExt = "\0300\031f\129\030f\0310\131\0300\031f\148\030f\0310\148\ + iconExt = "\0300\031f\129\030f\0310\131\0300\031f\148\030f\0310\148\ \030 \031 \0300\031f\131\030f\0310\142\129\ \030 \031 \0300\031f\131\030f\128", run = "Help.lua", @@ -49,7 +54,7 @@ icon = "\030f \ \030f\0310lua>\031 \ \030f ", - iconExt = "\0300\031f\151\030f\128\0300\159\159\159\030f\0310\144\0304\031f\159\030f\128\ + iconExt = "\0300\031f\151\030f\128\0300\159\159\159\030f\0310\144\0304\031f\159\030f\128\ \0300\031f\149\030f\128\0300\149\149\151\145\030f\128\0314\153\ \130\131\130\131\130\131\0314\130\031f\128", run = "sys/apps/Lua.lua", @@ -60,7 +65,7 @@ icon = "\0304 \030 \ \030f \0304 \0307 \030 \031 \031f_\ \030f \0304 \0307 \030 \031f/", - iconExt = "\031f\128\128\128\0308\159\143\0300\0317\151\0307\0310\140\148\ + iconExt = "\031f\128\128\128\0308\159\143\0300\0317\151\0307\0310\140\148\ \0314\151\131\0304\031f\148\030f\0318\138\148\0307\0310\138\131\129\ \0304\031f\138\143\133\030f\0318\131\129\031f\128\128\128", run = "Devices.lua", @@ -71,7 +76,7 @@ icon = " \0307\031f| \ \0307\031f---o\030 \031 \ \0307\031f| ", - iconExt = "\0318\138\0308\031f\130\0318\128\031f\129\030f\0318\133\ + iconExt = "\0318\138\0308\031f\130\0318\128\031f\129\030f\0318\133\ \0318\143\0308\128\0317\143\0318\128\030f\143\ \0318\138\135\143\139\133", run = "System.lua", @@ -82,7 +87,7 @@ icon = "\0304\031f \030 \0311e\ \030f\031f \0304 \030 \0311ee\031f \ \030f\031f \0304 \030 \0311e\031f ", - iconExt = "\0300\031f\159\135\030f\0310\156\0301\031f\159\030f\0311\144\0300\031f\147\139\030f\0310\144\ + iconExt = "\0300\031f\159\135\030f\0310\156\0301\031f\159\030f\0311\144\0300\031f\147\139\030f\0310\144\ \0300\128\128\030f\149\0311\157\142\0300\031f\149\0310\128\128\ \130\139\141\0311\130\131\0310\142\135\129", run = "Events.lua", @@ -93,7 +98,7 @@ icon = "\030f\031f \0315/\ \030f\031f \0315/\\/ \ \030f\0315/\031f ", - iconExt = "\031f\128\128\0305\159\030f\128\0305\159\030f\0315\134\031f\128\ + iconExt = "\031f\128\128\0305\159\030f\128\0305\159\030f\0315\134\031f\128\ \031f\128\0315\152\129\137\0305\031f\158\139\030f\0317 \ \0315\134\031f\128\128\128\128\0305\154\030f\0317 ", run = "Tasks.lua", @@ -104,7 +109,7 @@ icon = "\0300\0317==\031 \0307 \ \0300\0317====\ \0300\0317====", - iconExt = "\030 \031f\0300\031f\136\140\132\0308\130\030f\0318\144\ + iconExt = "\030 \031f\0300\031f\136\140\132\0308\130\030f\0318\144\ \030 \031f\030f\0310\157\0300\031f\147\030f\0310\142\143\149\ \030 \031f\0300\031f\136\140\132\140\030f\0310\149", run = "Files.lua", @@ -115,7 +120,7 @@ icon = "\0304\031f \ \0304\031f \030f\0310zz\031 \ \0304\031f \030f ", - iconExt = "\030e\031f\135\030f\031e\148\030e\128\031f\151\139\ + iconExt = "\030e\031f\135\030f\031e\148\030e\128\031f\151\139\ \030e\031e\128\030f\031f\128\031e\143\031f\128\030e\031e\128\ \031e\139\030e\031f\130\131\129\030f\031e\135", run = "/rom/programs/shutdown", @@ -163,7 +168,7 @@ icon = "\030d \030 \030e \030 \ \030d \030 \ \030d ", - iconExt = "\030 \031f\0305\031f\151\030f\0315\135\131\0305\031f\146\ + iconExt = "\030 \031f\0305\031f\151\030f\0315\135\131\0305\031f\146\ \030 \031f\030f\0315\130\141\0305\031f\139\030f\0315\130\ \030 \031f\0305\031f\146\143\030f\0315\158\031e\130", run = "/rom/programs/fun/worm",