1
0
mirror of https://github.com/kepler155c/opus synced 2024-09-20 19:29:38 +00:00
opus/sys/apps/storageActivity.lua

175 lines
3.7 KiB
Lua
Raw Normal View History

requireInjector(getfenv(1))
local ChestProvider = require('chestProvider18')
local Event = require('event')
local MEProvider = require('meProvider')
2017-04-15 15:38:08 +00:00
local RefinedProvider = require('refinedProvider')
local UI = require('ui')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2017-04-15 15:38:08 +00:00
local storage = RefinedProvider()
if not storage:isValid() then
storage = MEProvider()
2017-06-23 06:04:56 +00:00
if not storage:isValid() then
storage = ChestProvider()
end
2017-04-15 15:38:08 +00:00
end
if not storage:isValid() then
error('Not connected to a storage device')
2016-12-11 19:24:52 +00:00
end
multishell.setTitle(multishell.getCurrent(), 'Storage Activity')
UI:configure('StorageActivity', ...)
local changedPage = UI.Page({
grid = UI.Grid({
columns = {
2017-04-16 03:39:15 +00:00
{ heading = 'Qty', key = 'qty', width = 5 },
{ heading = 'Change', key = 'change', width = 6 },
{ heading = 'Name', key = 'display_name', width = UI.term.width - 15 },
2016-12-11 19:24:52 +00:00
},
2017-04-16 03:39:15 +00:00
sortColumn = 'display_name',
rey = -6,
2016-12-11 19:24:52 +00:00
}),
buttons = UI.Window({
2017-04-16 03:39:15 +00:00
ry = -4,
2016-12-11 19:24:52 +00:00
height = 5,
backgroundColor = colors.gray,
prevButton = UI.Button({
event = 'previous',
backgroundColor = colors.lightGray,
x = 2,
y = 2,
height = 3,
width = 5,
text = ' < '
}),
resetButton = UI.Button({
event = 'reset',
backgroundColor = colors.lightGray,
x = 8,
y = 2,
height = 3,
2017-04-16 03:39:15 +00:00
rex = -8,
2016-12-11 19:24:52 +00:00
text = 'Reset'
}),
nextButton = UI.Button({
event = 'next',
backgroundColor = colors.lightGray,
2017-04-16 03:39:15 +00:00
rx = -5,
2016-12-11 19:24:52 +00:00
y = 2,
height = 3,
width = 5,
text = ' > '
})
}),
accelerators = {
q = 'quit',
}
})
2017-04-16 03:39:15 +00:00
function changedPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
local ind = '+'
if row.change < 0 then
ind = ''
end
row.change = ind .. Util.toBytes(row.change)
row.qty = Util.toBytes(row.qty)
return row
end
2016-12-11 19:24:52 +00:00
function changedPage:eventHandler(event)
if event.type == 'reset' then
self.lastItems = nil
self.grid:setValues({ })
self.grid:clear()
self.grid:draw()
elseif event.type == 'next' then
self.grid:nextPage()
elseif event.type == 'previous' then
self.grid:previousPage()
elseif event.type == 'quit' then
Event.exitPullEvents()
else
return UI.Page.eventHandler(self, event)
end
return true
end
2017-05-14 03:45:59 +00:00
local function uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end
2016-12-11 19:24:52 +00:00
function changedPage:refresh()
2017-06-23 06:04:56 +00:00
local t = storage:listItems()
2016-12-11 19:24:52 +00:00
if not t or Util.empty(t) then
self:clear()
self:centeredWrite(math.ceil(self.height/2), 'Communication failure')
return
end
for k,v in pairs(t) do
2017-04-15 15:38:08 +00:00
t[k] = Util.shallowCopy(v)
end
2016-12-11 19:24:52 +00:00
if not self.lastItems then
self.lastItems = t
self.grid:setValues({ })
else
local changedItems = {}
for _,v in pairs(self.lastItems) do
found = false
for k2,v2 in pairs(t) do
2017-05-14 03:45:59 +00:00
if uniqueKey(v) == uniqueKey(v2) then
2016-12-11 19:24:52 +00:00
if v.qty ~= v2.qty then
local c = Util.shallowCopy(v2)
c.lastQty = v.qty
table.insert(changedItems, c)
end
table.remove(t, k2)
found = true
break
end
end
-- New item
if not found then
local c = Util.shallowCopy(v)
c.lastQty = v.qty
c.qty = 0
table.insert(changedItems, c)
end
end
-- No items left
for k,v in pairs(t) do
v.lastQty = 0
table.insert(changedItems, v)
end
2017-04-16 03:39:15 +00:00
2016-12-11 19:24:52 +00:00
for k,v in pairs(changedItems) do
2017-04-16 03:39:15 +00:00
v.change = v.qty - v.lastQty
2016-12-11 19:24:52 +00:00
end
2017-04-16 03:39:15 +00:00
2016-12-11 19:24:52 +00:00
self.grid:setValues(changedItems)
end
2017-04-16 03:39:15 +00:00
self.grid:draw()
2016-12-11 19:24:52 +00:00
end
2017-07-28 23:01:59 +00:00
Event.onInterval(5, function()
2016-12-11 19:24:52 +00:00
changedPage:refresh()
changedPage:sync()
end)
UI:setPage(changedPage)
2017-04-16 03:39:15 +00:00
UI:pullEvents()