1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-11 03:09:55 +00:00
opus/sys/apps/system/path.lua

104 lines
2.4 KiB
Lua
Raw Normal View History

local Config = require('opus.config')
local UI = require('opus.ui')
local Util = require('opus.util')
2018-12-23 03:11:16 +00:00
2019-02-07 08:10:05 +00:00
local tab = UI.Tab {
2018-12-23 03:11:16 +00:00
tabTitle = 'Path',
description = 'Set the shell path',
tabClose = true,
entry = UI.TextEntry {
x = 2, y = 2, ex = -2,
limit = 256,
2019-02-07 08:10:05 +00:00
shadowText = 'enter new path',
2018-12-23 03:11:16 +00:00
accelerators = {
enter = 'update_path',
},
2019-06-18 19:19:24 +00:00
help = 'add a new path',
2018-12-23 03:11:16 +00:00
},
grid = UI.Grid {
2019-02-07 08:10:05 +00:00
y = 4, ey = -3,
2018-12-23 03:11:16 +00:00
disableHeader = true,
columns = { { key = 'value' } },
autospace = true,
2019-06-18 19:19:24 +00:00
sortColumn = 'index',
help = 'double-click to remove, shift-arrow to move',
accelerators = {
delete = 'remove',
},
},
statusBar = UI.StatusBar { },
accelerators = {
[ 'shift-up' ] = 'move_up',
[ 'shift-down' ] = 'move_down',
2018-12-23 03:11:16 +00:00
},
}
2019-02-07 08:10:05 +00:00
function tab:updateList(path)
self.grid.values = { }
for k,v in ipairs(Util.split(path, '(.-):')) do
table.insert(self.grid.values, { index = k, value = v })
2019-06-18 19:19:24 +00:00
end
self.grid:update()
2018-12-23 03:11:16 +00:00
end
2019-02-07 08:10:05 +00:00
function tab:enable()
2019-06-18 19:19:24 +00:00
local env = Config.load('shell')
self:updateList(env.path)
2019-02-07 08:10:05 +00:00
UI.Tab.enable(self)
end
function tab:save()
2019-06-18 19:19:24 +00:00
local t = { }
for _, v in ipairs(self.grid.values) do
table.insert(t, v.value)
end
local env = Config.load('shell')
env.path = table.concat(t, ':')
self:updateList(env.path)
Config.update('shell', env)
2019-02-07 08:10:05 +00:00
end
function tab:eventHandler(event)
2019-06-18 19:19:24 +00:00
if event.type == 'update_path' then
table.insert(self.grid.values, {
value = self.entry.value,
})
2019-02-07 08:10:05 +00:00
self:save()
self.entry:reset()
2019-06-18 19:19:24 +00:00
self.entry:draw()
self.grid:draw()
2018-12-23 03:11:16 +00:00
return true
2019-02-07 08:10:05 +00:00
2019-06-18 19:19:24 +00:00
elseif event.type == 'grid_select' or event.type == 'remove' then
local selected = self.grid:getSelected()
if selected then
table.remove(self.grid.values, selected.index)
self:save()
self.grid:draw()
end
2019-02-07 08:10:05 +00:00
2019-06-18 19:19:24 +00:00
elseif event.type == 'focus_change' then
2019-02-07 08:10:05 +00:00
self.statusBar:setStatus(event.focused.help)
2019-06-18 19:19:24 +00:00
elseif event.type == 'move_up' then
local entry = self.grid:getSelected()
if entry.index > 1 then
table.insert(self.grid.values, entry.index - 1, table.remove(self.grid.values, entry.index))
self.grid:setIndex(entry.index - 1)
self:save()
self.grid:draw()
end
2019-02-07 08:10:05 +00:00
2019-06-18 19:19:24 +00:00
elseif event.type == 'move_down' then
local entry = self.grid:getSelected()
if entry.index < #self.grid.values then
table.insert(self.grid.values, entry.index + 1, table.remove(self.grid.values, entry.index))
self.grid:setIndex(entry.index + 1)
self:save()
self.grid:draw()
end
end
2018-12-23 03:11:16 +00:00
end
2019-02-07 08:10:05 +00:00
return tab