1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-04 16:06:16 +00:00
opus/sys/apps/System.lua

331 lines
7.6 KiB
Lua
Raw Normal View History

2018-01-24 22:39:38 +00:00
_G.requireInjector(_ENV)
2017-10-11 15:37:52 +00:00
local Config = require('config')
local Security = require('security')
local SHA1 = require('sha1')
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 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
2017-11-15 05:08:42 +00:00
local turtle = _G.turtle
2017-10-08 21:45:01 +00:00
2016-12-11 19:24:52 +00:00
UI:configure('System', ...)
local env = {
2018-01-24 22:39:38 +00:00
path = shell.path(),
aliases = shell.aliases(),
lua_path = _ENV.LUA_PATH,
2016-12-11 19:24:52 +00:00
}
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 {
2018-01-24 22:39:38 +00:00
tabs = UI.Tabs {
pathTab = UI.Window {
tabTitle = 'Path',
entry = UI.TextEntry {
x = 2, y = 2, ex = -2,
limit = 256,
value = shell.path(),
shadowText = 'enter system path',
accelerators = {
enter = 'update_path',
},
},
grid = UI.Grid {
y = 4,
disableHeader = true,
columns = { { key = 'value' } },
autospace = true,
},
},
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
aliasTab = UI.Window {
tabTitle = 'Alias',
alias = UI.TextEntry {
x = 2, y = 2, ex = -2,
limit = 32,
shadowText = 'Alias',
},
path = UI.TextEntry {
y = 3, x = 2, ex = -2,
limit = 256,
shadowText = 'Program path',
accelerators = {
enter = 'new_alias',
},
},
grid = UI.Grid {
y = 5,
sortColumn = 'alias',
columns = {
{ heading = 'Alias', key = 'alias' },
{ heading = 'Program', key = 'path' },
},
accelerators = {
delete = 'delete_alias',
},
},
},
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
passwordTab = UI.Window {
tabTitle = 'Password',
oldPass = UI.TextEntry {
x = 2, y = 2, ex = -2,
limit = 32,
mask = true,
shadowText = 'old password',
inactive = not Security.getPassword(),
},
newPass = UI.TextEntry {
y = 3, x = 2, ex = -2,
limit = 32,
mask = true,
shadowText = 'new password',
accelerators = {
enter = 'new_password',
},
},
button = UI.Button {
x = 2, y = 5,
text = 'Update',
event = 'update_password',
},
info = UI.TextArea {
x = 2, ex = -2,
y = 7,
inactive = true,
value = 'Add a password to enable other computers to connect to this one.',
}
},
2017-10-11 15:37:52 +00:00
2018-01-24 22:39:38 +00:00
infoTab = UI.Window {
tabTitle = 'Info',
labelText = UI.Text {
x = 3, y = 2,
value = 'Label'
},
label = UI.TextEntry {
x = 9, y = 2, ex = -4,
limit = 32,
value = os.getComputerLabel(),
accelerators = {
enter = 'update_label',
},
},
grid = UI.ScrollingGrid {
y = 3,
values = {
{ name = '', value = '' },
{ name = 'CC version', value = Util.getVersion() },
{ name = 'Lua version', value = _VERSION },
{ name = 'MC version', value = Util.getMinecraftVersion() },
{ name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) },
{ name = 'Computer ID', value = tostring(os.getComputerID()) },
{ name = 'Day', value = tostring(os.day()) },
},
inactive = true,
columns = {
{ key = 'name', width = 12 },
{ key = 'value' },
},
},
},
},
notification = UI.Notification(),
accelerators = {
q = 'quit',
},
2016-12-14 18:54:13 +00:00
}
2016-12-11 19:24:52 +00:00
2017-11-15 05:08:42 +00:00
if turtle then
2018-11-10 18:00:22 +00:00
pcall(function()
local Home = require('turtle.home')
-- TODO: dont rely on turtle.home
local values = { }
Config.load('gps', values.home and { values.home } or { })
2017-11-15 05:08:42 +00:00
2018-11-10 18:00:22 +00:00
systemPage.tabs:add({
gpsTab = UI.Window {
tabTitle = 'GPS',
labelText = UI.Text {
x = 3, y = 2,
value = 'On restart, return to this location'
},
grid = UI.Grid {
x = 3, ex = -3, y = 4,
height = 2,
values = values,
inactive = true,
columns = {
{ heading = 'x', key = 'x' },
{ heading = 'y', key = 'y' },
{ heading = 'z', key = 'z' },
},
},
button1 = UI.Button {
x = 3, y = 7,
text = 'Set home',
event = 'gps_set',
},
button2 = UI.Button {
ex = -3, y = 7, width = 7,
text = 'Clear',
event = 'gps_clear',
2018-01-24 22:39:38 +00:00
},
},
2018-11-10 18:00:22 +00:00
})
function systemPage.tabs.gpsTab:eventHandler(event)
if event.type == 'gps_set' then
systemPage.notification:info('Determining location', 10)
systemPage:sync()
if Home.set() then
Config.load('gps', values)
self.grid:setValues(values.home and { values.home } or { })
self.grid:draw()
systemPage.notification:success('Location set')
else
systemPage.notification:error('Unable to determine location')
end
return true
elseif event.type == 'gps_clear' then
fs.delete('usr/config/gps')
self.grid:setValues({ })
2018-01-24 22:39:38 +00:00
self.grid:draw()
2018-11-10 18:00:22 +00:00
return true
2018-01-24 22:39:38 +00:00
end
end
2018-11-10 18:00:22 +00:00
end)
2017-11-15 05:08:42 +00:00
end
2017-10-09 04:26:19 +00:00
if settings then
2018-01-24 22:39:38 +00:00
local values = { }
for _,v in pairs(settings.getNames()) do
local value = settings.get(v)
if not value then
value = false
end
table.insert(values, {
name = v,
value = value,
})
end
2017-10-09 04:26:19 +00:00
2018-01-24 22:39:38 +00:00
systemPage.tabs:add({
settingsTab = UI.Window {
tabTitle = 'Settings',
grid = UI.Grid {
y = 1,
values = values,
autospace = true,
sortColumn = 'name',
columns = {
{ heading = 'Setting', key = 'name' },
{ heading = 'Value', key = 'value' },
},
},
}
})
function systemPage.tabs.settingsTab:eventHandler(event)
if event.type == 'grid_select' then
if not event.selected.value or type(event.selected.value) == 'boolean' then
event.selected.value = not event.selected.value
end
settings.set(event.selected.name, event.selected.value)
settings.save('.settings')
self.grid:draw()
return true
end
end
2017-10-09 04:26:19 +00:00
end
2016-12-11 19:24:52 +00:00
function systemPage.tabs.pathTab.grid:draw()
2018-01-24 22:39:38 +00:00
self.values = { }
for _,v in ipairs(Util.split(env.path, '(.-):')) do
table.insert(self.values, { value = v })
end
self:update()
UI.Grid.draw(self)
2016-12-11 19:24:52 +00:00
end
function systemPage.tabs.pathTab:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'update_path' then
env.path = self.entry.value
self.grid:setIndex(self.grid:getIndex())
self.grid:draw()
Config.update('shell', env)
systemPage.notification:success('reboot to take effect')
return true
end
2016-12-11 19:24:52 +00:00
end
function systemPage.tabs.aliasTab.grid:draw()
2018-01-24 22:39:38 +00:00
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)
2016-12-11 19:24:52 +00:00
end
function systemPage.tabs.aliasTab:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'delete_alias' then
env.aliases[self.grid:getSelected().alias] = nil
self.grid:setIndex(self.grid:getIndex())
self.grid:draw()
Config.update('shell', env)
systemPage.notification:success('reboot to take effect')
return true
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +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)
Config.update('shell', env)
systemPage.notification:success('reboot to take effect')
return true
end
2016-12-11 19:24:52 +00:00
end
2017-10-11 15:37:52 +00:00
function systemPage.tabs.passwordTab:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'update_password' then
if #self.newPass.value == 0 then
systemPage.notification:error('Invalid password')
elseif Security.getPassword() and not Security.verifyPassword(SHA1.sha1(self.oldPass.value)) then
systemPage.notification:error('Passwords do not match')
else
Security.updatePassword(SHA1.sha1(self.newPass.value))
self.oldPass.inactive = false
systemPage.notification:success('Password updated')
end
2017-10-11 15:37:52 +00:00
2018-01-24 22:39:38 +00:00
return true
end
2017-10-11 15:37:52 +00:00
end
2016-12-11 19:24:52 +00:00
function systemPage.tabs.infoTab:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'update_label' then
os.setComputerLabel(self.label.value)
systemPage.notification:success('Label updated')
return true
end
2016-12-11 19:24:52 +00:00
end
function systemPage:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'quit' then
UI:exitPullEvents()
elseif event.type == 'tab_activate' then
event.activated:focusFirst()
else
return UI.Page.eventHandler(self, event)
end
return true
2016-12-11 19:24:52 +00:00
end
UI:setPage(systemPage)
2017-10-08 21:45:01 +00:00
UI:pullEvents()