1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 08:26:16 +00:00
opus/sys/apps/PackageManager.lua

186 lines
4.4 KiB
Lua
Raw Normal View History

2018-11-06 21:43:24 +00:00
local Ansi = require('ansi')
local Packages = require('packages')
local UI = require('ui')
2019-01-08 16:38:19 +00:00
local Util = require('util')
2018-11-06 21:43:24 +00:00
local colors = _G.colors
local term = _G.term
UI:configure('PackageManager', ...)
local page = UI.Page {
grid = UI.ScrollingGrid {
2019-01-11 15:01:06 +00:00
x = 2, ex = 14, y = 2, ey = -5,
2018-11-06 21:43:24 +00:00
values = { },
columns = {
{ heading = 'Package', key = 'name' },
},
sortColumn = 'name',
autospace = true,
help = 'Select a package',
},
add = UI.Button {
2019-01-11 15:01:06 +00:00
x = 2, y = -3,
2019-01-08 16:38:19 +00:00
text = 'Install',
2018-11-06 21:43:24 +00:00
event = 'action',
help = 'Install or update',
},
remove = UI.Button {
2019-01-11 15:01:06 +00:00
x = 12, y = -3,
2019-01-08 16:38:19 +00:00
text = 'Remove ',
2018-11-06 21:43:24 +00:00
event = 'action',
operation = 'uninstall',
operationText = 'Remove',
help = 'Remove',
},
description = UI.TextArea {
2019-01-11 15:01:06 +00:00
x = 16, y = 3, ey = -5,
marginRight = 0, marginLeft = 0,
2018-11-06 21:43:24 +00:00
--backgroundColor = colors.white,
},
2019-01-09 11:59:19 +00:00
load = UI.Button {
2019-01-11 15:01:06 +00:00
x = 22, y = -3,
text = 'Update packages',
2019-01-09 11:59:19 +00:00
event = 'reload',
help = 'Download the latest package list',
},
2018-11-06 21:43:24 +00:00
action = UI.SlideOut {
backgroundColor = colors.cyan,
titleBar = UI.TitleBar {
event = 'hide-action',
},
button = UI.Button {
2019-01-11 21:04:31 +00:00
x = -10, y = 3,
2019-01-08 16:38:19 +00:00
text = ' Begin ', event = 'begin',
2018-11-06 21:43:24 +00:00
},
output = UI.Embedded {
2019-01-11 21:04:31 +00:00
y = 5, ey = -2, x = 2, ex = -2,
2018-11-06 21:43:24 +00:00
},
statusBar = UI.StatusBar {
backgroundColor = colors.cyan,
},
},
2019-01-09 11:59:19 +00:00
statusBar = UI.StatusBar { },
2018-11-06 21:43:24 +00:00
}
2019-01-09 11:59:19 +00:00
function page:loadPackages()
self.grid.values = { }
self.statusBar:setStatus('Downloading...')
self:sync()
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(self.grid.values, {
installed = not not Packages:isInstalled(k),
name = k,
manifest = manifest,
})
end
self.grid:update()
self.grid:setIndex(1)
self.grid:emit({
type = 'grid_focus_row',
selected = self.grid:getSelected(),
element = self.grid,
})
self.statusBar:setStatus('Updated packages')
end
2018-11-06 21:43:24 +00:00
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()
2018-11-09 20:07:55 +00:00
local cmd = string.format('package %s %s', operation, name)
2018-11-06 21:43:24 +00:00
term.setCursorPos(1, 1)
term.clear()
term.setTextColor(colors.yellow)
print(cmd .. '\n')
term.setTextColor(colors.white)
2019-01-08 16:38:19 +00:00
local s, m = Util.run(_ENV, '/sys/apps/package.lua', operation, name)
if not s and m then
_G.printError(m)
end
2018-11-06 21:43:24 +00:00
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'
2019-01-08 16:38:19 +00:00
self.add.text = selected.installed and 'Update' or 'Install'
self.remove.inactive = not selected.installed
self.add:draw()
self.remove:draw()
2018-11-06 21:43:24 +00:00
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)
2019-01-09 11:59:19 +00:00
elseif event.type == 'reload' then
Packages:downloadList()
self:loadPackages()
2018-11-06 21:43:24 +00:00
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
2019-01-08 16:38:19 +00:00
self.action.button.text = ' Begin '
2018-11-06 21:43:24 +00:00
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)
2019-01-08 16:38:19 +00:00
self.action.button.text = ' Done '
2018-11-06 21:43:24 +00:00
self.action.button.event = 'hide-action'
self.action.button:draw()
elseif event.type == 'quit' then
UI:exitPullEvents()
end
UI.Page.eventHandler(self, event)
end
2019-01-09 11:59:19 +00:00
page:loadPackages()
2018-11-06 21:43:24 +00:00
UI:setPage(page)
UI:pullEvents()