milo cleanup

This commit is contained in:
kepler155c
2018-11-11 14:45:39 -05:00
parent a00a66f4e6
commit fe1fc6ea93
13 changed files with 200 additions and 200 deletions

View File

@@ -14,7 +14,7 @@ local context = Milo:getContext()
local function saveConfig()
local t = { }
for k,v in pairs(context.config.remoteDefaults) do
for k,v in pairs(context.config.nodes) do
t[k] = v.adapter
v.adapter = nil
end
@@ -22,19 +22,29 @@ local function saveConfig()
Config.update('milo', context.config)
for k,v in pairs(t) do
context.config.remoteDefaults[k].adapter = v
context.config.nodes[k].adapter = v
end
context.storage:initStorage()
end
local machinesPage = UI.Page {
local networkPage = UI.Page {
titleBar = UI.TitleBar {
previousPage = true,
title = 'Machines',
title = 'Network',
},
filter = UI.TextEntry {
y = -2, x = 1, ex = -9,
limit = 50,
shadowText = 'filter',
backgroundColor = colors.cyan,
backgroundFocusColor = colors.cyan,
accelerators = {
[ 'enter' ] = 'eject',
},
},
grid = UI.ScrollingGrid {
y = 2, ey = -2,
values = context.config.remoteDefaults,
y = 2, ey = -3,
values = context.config.nodes,
columns = {
{ key = 'suffix', width = 4, justify = 'right' },
{ heading = 'Name', key = 'displayName' },
@@ -42,23 +52,25 @@ local machinesPage = UI.Page {
{ heading = 'Pri', key = 'priority', width = 3 },
},
sortColumn = 'displayName',
help = 'Select Machine',
help = 'Select Node',
},
remove = UI.Button {
y = -1, x = -4,
text = '-', event = 'remove_machine',
help = 'Remove Machine',
y = -2, x = -4,
text = '-', event = 'remove_node',
help = 'Remove Node',
},
statusBar = UI.StatusBar {
ex = -7,
backgroundColor = colors.cyan,
backgroundColor = colors.lightGray,
},
notification = UI.Notification { },
}
function machinesPage.grid:getDisplayValues(row)
function networkPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
local t = { row.name:match(':(.+)_(%d+)$') }
if #t ~= 2 then
t = { row.name:match('(.+)_(%d+)$') }
end
if t and #t == 2 then
row.name, row.suffix = table.unpack(t)
row.name = row.name .. '_' .. row.suffix
@@ -67,7 +79,7 @@ function machinesPage.grid:getDisplayValues(row)
return row
end
function machinesPage.grid:getRowTextColor(row, selected)
function networkPage.grid:getRowTextColor(row, selected)
if not device[row.name] then
return colors.red
end
@@ -77,11 +89,11 @@ function machinesPage.grid:getRowTextColor(row, selected)
return UI.Grid:getRowTextColor(row, selected)
end
function machinesPage:getList()
function networkPage:getList()
for _, v in pairs(device) do
if v.pullItems then
if not context.config.remoteDefaults[v.name] then
context.config.remoteDefaults[v.name] = {
if not context.config.nodes[v.name] then
context.config.nodes[v.name] = {
name = v.name,
mtype = 'ignore',
}
@@ -90,9 +102,10 @@ function machinesPage:getList()
end
end
function machinesPage:enable()
function networkPage:enable()
self:getList()
self.grid:update()
self:setFocus(self.filter)
UI.Page.enable(self)
self.handler = Event.on({ 'device_attach', 'device_detach'}, function()
self:getList()
@@ -102,28 +115,50 @@ function machinesPage:enable()
end)
end
function machinesPage:disable()
function networkPage:disable()
UI.Page.disable(self)
Event.off(self.handler)
end
function machinesPage:eventHandler(event)
function networkPage:applyFilter()
local t = context.config.nodes
local filter = self.filter.value
if #filter > 0 then
t = { }
filter = filter:lower()
for _,v in pairs(context.config.nodes) do
if (v.displayName and string.find(string.lower(v.displayName), filter, 1, true)) or
string.find(string.lower(v.name), filter, 1, true) then
table.insert(t, v)
end
end
end
self.grid:setValues(t)
end
function networkPage:eventHandler(event)
if event.type == 'grid_select' then
if not device[event.selected.name] then
self.notification:error('Unable to edit while disconnected')
else
UI:setPage('machineWizard', event.selected)
UI:setPage('nodeWizard', event.selected)
end
elseif event.type == 'remove_machine' then
local machine = self.grid:getSelected()
if machine then
context.config.remoteDefaults[machine.name] = nil
elseif event.type == 'remove_node' then
local node = self.grid:getSelected()
if node then
context.config.nodes[node.name] = nil
saveConfig()
end
self.grid:update()
self.grid:draw()
elseif event.type == 'text_change' then
self:applyFilter()
self.grid:draw()
elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help)
@@ -133,7 +168,7 @@ function machinesPage:eventHandler(event)
return true
end
local machineWizard = UI.Page {
local nodeWizard = UI.Page {
titleBar = UI.TitleBar { title = 'Configure' },
wizard = UI.Wizard {
y = 2, ey = -2,
@@ -260,7 +295,7 @@ The settings will take effect immediately!]],
}
--[[ Filter slide out ]] --
function machineWizard.filter:show(entry, callback, whitelistOnly)
function nodeWizard.filter:show(entry, callback, whitelistOnly)
self.entry = entry
self.callback = callback
@@ -280,13 +315,13 @@ function machineWizard.filter:show(entry, callback, whitelistOnly)
sync.lock(turtle)
end
function machineWizard.filter:hide()
function nodeWizard.filter:hide()
UI.SlideOut.hide(self)
Milo:resumeCrafting()
sync.release(turtle)
end
function machineWizard.filter:resetGrid()
function nodeWizard.filter:resetGrid()
local t = { }
for k in pairs(self.entry.filter) do
table.insert(t, itemDB:splitKey(k))
@@ -294,13 +329,13 @@ function machineWizard.filter:resetGrid()
self.grid:setValues(t)
end
function machineWizard.filter.grid:getDisplayValues(row)
function nodeWizard.filter.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.displayName = itemDB:getName(row)
return row
end
function machineWizard.filter:eventHandler(event)
function nodeWizard.filter:eventHandler(event)
if event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help)
@@ -341,16 +376,16 @@ function machineWizard.filter:eventHandler(event)
end
--[[ General Page ]] --
function machineWizard.wizard.pages.general:enable()
function nodeWizard.wizard.pages.general:enable()
UI.Window.enable(self)
self:focusFirst()
end
function machineWizard.wizard.pages.general:setMachine(machine)
function nodeWizard.wizard.pages.general:setNode(node)
local inventory
if device[machine.name] and device[machine.name].list then
inventory = device[machine.name].list()
if device[node.name] and device[node.name].list then
inventory = device[node.name].list()
for k,v in pairs(inventory) do
v.slot = k
end
@@ -359,18 +394,18 @@ function machineWizard.wizard.pages.general:setMachine(machine)
self.grid:setValues(inventory or { })
end
function machineWizard.wizard.pages.general.grid:getDisplayValues(row)
function nodeWizard.wizard.pages.general.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.displayName = itemDB:getName(row)
return row
end
function machineWizard.wizard.pages.general:validate()
function nodeWizard.wizard.pages.general:validate()
return self.form:save()
end
--[[ Wizard ]] --
function machineWizard.wizard:eventHandler(event)
function nodeWizard.wizard:eventHandler(event)
if event.type == 'nextView' and
Util.find(self.pages, 'enabled', true) == self.pages.general then
@@ -383,7 +418,7 @@ function machineWizard.wizard:eventHandler(event)
self.pages.confirmation.index = 2
for _, page in pairs(self.pages) do
if not page.index and page:isValidFor(self.parent.machine) then
if not page.index and page:isValidFor(self.parent.node) then
page.index = index
index = index + 1
end
@@ -396,16 +431,16 @@ function machineWizard.wizard:eventHandler(event)
end
end
function machineWizard:enable(machine)
local adapter = machine.adapter
machine.adapter = nil -- don't deep copy the adapter
self.machine = Util.deepCopy(machine)
self.machine.adapter = adapter
machine.adapter = adapter
function nodeWizard:enable(node)
local adapter = node.adapter
node.adapter = nil -- don't deep copy the adapter
self.node = Util.deepCopy(node)
self.node.adapter = adapter
node.adapter = adapter
_G._p2 = self.machine
self.wizard.pages.general.form:setValues(self.machine)
self.wizard.pages.general.form[1].shadowText = self.machine.name
_G._p2 = self.node
self.wizard.pages.general.form:setValues(self.node)
self.wizard.pages.general.form[1].shadowText = self.node.name
-- restore indices
for _, page in pairs(self.wizard.pages) do
@@ -418,18 +453,18 @@ _G._p2 = self.machine
UI.Page.enable(self)
for _, v in pairs(self.wizard.pages) do
if v.setMachine then
v:setMachine(self.machine)
if v.setNode then
v:setNode(self.node)
end
end
end
function machineWizard:eventHandler(event)
function nodeWizard:eventHandler(event)
if event.type == 'cancel' then
UI:setPreviousPage()
elseif event.type == 'accept' then
Util.prune(self.machine, function(v)
Util.prune(self.node, function(v)
if type(v) == 'boolean' then
return v
elseif type(v) == 'string' then
@@ -440,8 +475,8 @@ function machineWizard:eventHandler(event)
return true
end)
Util.clear(context.config.remoteDefaults[self.machine.name])
Util.merge(context.config.remoteDefaults[self.machine.name], self.machine)
Util.clear(context.config.nodes[self.node.name])
Util.merge(context.config.nodes[self.node.name], self.node)
saveConfig()
UI:setPreviousPage()
@@ -451,7 +486,7 @@ function machineWizard:eventHandler(event)
elseif event.type == 'enable_view' then
local current = event.next or event.prev
self.titleBar.title = current.title or 'Machine'
self.titleBar.title = current.title or 'Node'
self.titleBar:draw()
elseif event.type == 'focus_change' then
@@ -467,5 +502,5 @@ function machineWizard:eventHandler(event)
return true
end
UI:addPage('machines', machinesPage)
UI:addPage('machineWizard', machineWizard)
UI:addPage('network', networkPage)
UI:addPage('nodeWizard', nodeWizard)