opus/sys/apps/System.lua

230 lines
5.6 KiB
Lua
Raw Normal View History

2017-10-08 21:45:01 +00:00
_G.requireInjector()
2016-12-11 19:24:52 +00:00
local Config = require('config')
local UI = require('ui')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local fs = _G.fs
local multishell = _ENV.multishell
local os = _G.os
2017-10-09 04:26:19 +00:00
local settings = _G.settings
2017-10-08 21:45:01 +00:00
local shell = _ENV.shell
2016-12-11 19:24:52 +00:00
multishell.setTitle(multishell.getCurrent(), 'System')
UI:configure('System', ...)
2017-10-09 17:08:38 +00:00
local mcVersion = _G._MC_VERSION or 'unknown'
if _G._HOST then
local version = _G._HOST:match('%S+ %S+ %((%S.+)%)')
if version then
mcVersion = version:match('Minecraft (%S+)') or version
end
end
2016-12-11 19:24:52 +00:00
local env = {
path = shell.path(),
aliases = shell.aliases(),
lua_path = LUA_PATH,
}
2017-09-26 19:18:44 +00:00
Config.load('shell', env)
2016-12-11 19:24:52 +00:00
2016-12-14 18:54:13 +00:00
local systemPage = UI.Page {
tabs = UI.Tabs {
pathTab = UI.Window {
2016-12-11 19:24:52 +00:00
tabTitle = 'Path',
2016-12-14 18:54:13 +00:00
entry = UI.TextEntry {
2017-09-30 02:30:01 +00:00
x = 2, y = 2, ex = -2,
2016-12-14 18:54:13 +00:00
limit = 256,
2016-12-11 19:24:52 +00:00
value = shell.path(),
shadowText = 'enter system path',
accelerators = {
enter = 'update_path',
},
2016-12-14 18:54:13 +00:00
},
grid = UI.Grid {
2016-12-11 19:24:52 +00:00
y = 4,
disableHeader = true,
columns = { { key = 'value' } },
autospace = true,
2016-12-14 18:54:13 +00:00
},
},
2016-12-11 19:24:52 +00:00
2016-12-14 18:54:13 +00:00
aliasTab = UI.Window {
2016-12-11 19:24:52 +00:00
tabTitle = 'Aliases',
2016-12-14 18:54:13 +00:00
alias = UI.TextEntry {
2017-10-08 21:45:01 +00:00
x = 2, y = 2, ex = -2,
2016-12-11 19:24:52 +00:00
limit = 32,
shadowText = 'Alias',
2016-12-14 18:54:13 +00:00
},
path = UI.TextEntry {
2017-09-30 02:30:01 +00:00
y = 3, x = 2, ex = -2,
2016-12-14 18:54:13 +00:00
limit = 256,
2016-12-11 19:24:52 +00:00
shadowText = 'Program path',
accelerators = {
enter = 'new_alias',
},
2016-12-14 18:54:13 +00:00
},
grid = UI.Grid {
y = 5,
sortColumn = 'alias',
2016-12-11 19:24:52 +00:00
columns = {
{ heading = 'Alias', key = 'alias' },
{ heading = 'Program', key = 'path' },
},
accelerators = {
delete = 'delete_alias',
},
2016-12-14 18:54:13 +00:00
},
},
2016-12-11 19:24:52 +00:00
2016-12-14 18:54:13 +00:00
infoTab = UI.Window {
2016-12-11 19:24:52 +00:00
tabTitle = 'Info',
2016-12-14 18:54:13 +00:00
labelText = UI.Text {
x = 3, y = 2,
value = 'Label'
},
label = UI.TextEntry {
2017-09-30 02:30:01 +00:00
x = 9, y = 2, ex = -4,
2016-12-14 18:54:13 +00:00
limit = 32,
value = os.getComputerLabel(),
2016-12-11 19:24:52 +00:00
accelerators = {
enter = 'update_label',
},
2016-12-14 18:54:13 +00:00
},
grid = UI.ScrollingGrid {
2017-09-26 02:49:44 +00:00
y = 3,
2016-12-11 19:24:52 +00:00
values = {
2017-09-26 02:49:44 +00:00
{ name = '', value = '' },
{ name = 'CC version', value = Util.getVersion() },
2016-12-11 19:24:52 +00:00
{ name = 'Lua version', value = _VERSION },
2017-10-09 17:08:38 +00:00
{ name = 'MC version', value = mcVersion },
2016-12-11 19:24:52 +00:00
{ name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) },
{ name = 'Computer ID', value = tostring(os.getComputerID()) },
{ name = 'Day', value = tostring(os.day()) },
},
inactive = true,
2016-12-11 19:24:52 +00:00
columns = {
2017-10-01 00:35:36 +00:00
{ key = 'name', width = 12 },
{ key = 'value' },
2016-12-11 19:24:52 +00:00
},
2016-12-14 18:54:13 +00:00
},
},
},
2016-12-11 19:24:52 +00:00
notification = UI.Notification(),
accelerators = {
q = 'quit',
},
2016-12-14 18:54:13 +00:00
}
2016-12-11 19:24:52 +00:00
2017-10-09 04:26:19 +00:00
if settings then
local values = { }
for _,v in pairs(settings.getNames()) do
table.insert(values, {
name = v,
value = not not settings.get(v),
})
end
systemPage.tabs:add({
systemTab = UI.Window {
tabTitle = 'Settings',
grid = UI.Grid {
y = 1,
values = values,
2017-10-09 17:08:38 +00:00
autospace = true,
2017-10-09 04:26:19 +00:00
sortColumn = 'name',
columns = {
{ heading = 'Setting', key = 'name' },
{ heading = 'Value', key = 'value' },
},
accelerators = {
},
},
}
})
function systemPage.tabs.systemTab:eventHandler(event)
if event.type == 'grid_select' then
event.selected.value = not event.selected.value
settings.set(event.selected.name, event.selected.value)
settings.save('.settings')
self.grid:draw()
return true
end
end
end
2016-12-11 19:24:52 +00:00
function systemPage.tabs.pathTab.grid:draw()
self.values = { }
for _,v in ipairs(Util.split(env.path, '(.-):')) do
table.insert(self.values, { value = v })
end
self:update()
UI.Grid.draw(self)
end
function systemPage.tabs.pathTab:eventHandler(event)
if event.type == 'update_path' then
env.path = self.entry.value
self.grid:setIndex(self.grid:getIndex())
self.grid:draw()
2017-09-26 19:18:44 +00:00
Config.update('shell', env)
2016-12-11 19:24:52 +00:00
systemPage.notification:success('reboot to take effect')
2016-12-14 18:54:13 +00:00
return true
2016-12-11 19:24:52 +00:00
end
end
function systemPage.tabs.aliasTab.grid:draw()
self.values = { }
for k,v in pairs(env.aliases) do
table.insert(self.values, { alias = k, path = v })
end
self:update()
UI.Grid.draw(self)
end
function systemPage.tabs.aliasTab:eventHandler(event)
if event.type == 'delete_alias' then
env.aliases[self.grid:getSelected().alias] = nil
self.grid:setIndex(self.grid:getIndex())
self.grid:draw()
2017-09-26 19:18:44 +00:00
Config.update('shell', env)
2016-12-11 19:24:52 +00:00
systemPage.notification:success('reboot to take effect')
2016-12-14 18:54:13 +00:00
return true
2016-12-11 19:24:52 +00:00
elseif event.type == 'new_alias' then
env.aliases[self.alias.value] = self.path.value
self.alias:reset()
self.path:reset()
self:draw()
self:setFocus(self.alias)
2017-09-26 19:18:44 +00:00
Config.update('shell', env)
2016-12-11 19:24:52 +00:00
systemPage.notification:success('reboot to take effect')
2016-12-14 18:54:13 +00:00
return true
2016-12-11 19:24:52 +00:00
end
end
function systemPage.tabs.infoTab:eventHandler(event)
if event.type == 'update_label' then
os.setComputerLabel(self.label.value)
systemPage.notification:success('Label updated')
return true
end
end
function systemPage:eventHandler(event)
if event.type == 'quit' then
2017-10-08 21:45:01 +00:00
UI:exitPullEvents()
2016-12-11 19:24:52 +00:00
elseif event.type == 'tab_activate' then
event.activated:focusFirst()
else
return UI.Page.eventHandler(self, event)
end
return true
end
UI:setPage(systemPage)
2017-10-08 21:45:01 +00:00
UI:pullEvents()