mirror of
https://github.com/kepler155c/opus
synced 2024-12-28 01:20:27 +00:00
package manager UI
This commit is contained in:
parent
99b2fa1b0b
commit
1a4ef3e581
@ -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
|
||||
|
@ -11,14 +11,9 @@ 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')
|
||||
|
159
sys/apps/PackageManager.lua
Normal file
159
sys/apps/PackageManager.lua
Normal file
@ -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()
|
@ -1,4 +1,9 @@
|
||||
{
|
||||
[ "0a999012ffb87b3edac99adbdfc498b12831a1e2" ] = {
|
||||
title = "Packages",
|
||||
category = "System",
|
||||
run = "PackageManager.lua",
|
||||
},
|
||||
[ "53ebc572b4a44802ba114729f07bdaaf5409a9d7" ] = {
|
||||
title = "Network",
|
||||
category = "Apps",
|
||||
|
Loading…
Reference in New Issue
Block a user