milo: improved sorting
This commit is contained in:
@@ -67,7 +67,7 @@ local function scan()
|
|||||||
local doDropOff
|
local doDropOff
|
||||||
|
|
||||||
for _,v in pairs(summed) do
|
for _,v in pairs(summed) do
|
||||||
if v.count > 48 then
|
if v.count > 32 then
|
||||||
doDropOff = true
|
doDropOff = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,13 +12,14 @@ local colors = _G.colors
|
|||||||
local device = _G.device
|
local device = _G.device
|
||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
local socket
|
local string = _G.string
|
||||||
|
|
||||||
local SHIELD_SLOT = 2
|
local SHIELD_SLOT = 2
|
||||||
local STARTUP_FILE = 'usr/autorun/miloRemote.lua'
|
local STARTUP_FILE = 'usr/autorun/miloRemote.lua'
|
||||||
|
|
||||||
local config = Config.load('miloRemote', { displayMode = 0 })
|
local config = Config.load('miloRemote', { displayMode = 0 })
|
||||||
|
|
||||||
|
local socket
|
||||||
local depositMode = {
|
local depositMode = {
|
||||||
[ true ] = { text = '\25', textColor = colors.black, help = 'Deposit enabled' },
|
[ true ] = { text = '\25', textColor = colors.black, help = 'Deposit enabled' },
|
||||||
[ false ] = { text = '\215', textColor = colors.red, help = 'Deposit disabled' },
|
[ false ] = { text = '\215', textColor = colors.red, help = 'Deposit disabled' },
|
||||||
@@ -70,7 +71,8 @@ local page = UI.Page {
|
|||||||
{ heading = 'Name', key = 'displayName' },
|
{ heading = 'Name', key = 'displayName' },
|
||||||
},
|
},
|
||||||
values = { },
|
values = { },
|
||||||
sortColumn = 'displayName',
|
sortColumn = config.sortColumn or 'count',
|
||||||
|
inverseSort = config.inverseSort,
|
||||||
help = '^(s)tack, ^(a)ll'
|
help = '^(s)tack, ^(a)ll'
|
||||||
},
|
},
|
||||||
statusBar = UI.Window {
|
statusBar = UI.Window {
|
||||||
@@ -170,26 +172,6 @@ local function getPlayerName()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function filterItems(t, filter, displayMode)
|
|
||||||
if filter or displayMode > 0 then
|
|
||||||
local r = { }
|
|
||||||
if filter then
|
|
||||||
filter = filter:lower()
|
|
||||||
end
|
|
||||||
for _,v in pairs(t) do
|
|
||||||
if not filter or string.find(v.lname, filter, 1, true) then
|
|
||||||
if not displayMode or
|
|
||||||
displayMode == 0 or
|
|
||||||
displayMode == 1 and v.count > 0 then
|
|
||||||
table.insert(r, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
|
|
||||||
function page:setStatus(status)
|
function page:setStatus(status)
|
||||||
self.menuBar.infoBar:setStatus(status)
|
self.menuBar.infoBar:setStatus(status)
|
||||||
self:sync()
|
self:sync()
|
||||||
@@ -271,6 +253,34 @@ function page.grid:getDisplayValues(row)
|
|||||||
return row
|
return row
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function page.grid:sortCompare(a, b)
|
||||||
|
if self.sortColumn ~= 'displayName' then
|
||||||
|
if a[self.sortColumn] == b[self.sortColumn] then
|
||||||
|
if self.inverseSort then
|
||||||
|
return a.displayName > b.displayName
|
||||||
|
end
|
||||||
|
return a.displayName < b.displayName
|
||||||
|
end
|
||||||
|
if a[self.sortColumn] == 0 then
|
||||||
|
return self.inverseSort
|
||||||
|
end
|
||||||
|
if b[self.sortColumn] == 0 then
|
||||||
|
return not self.inverseSort
|
||||||
|
end
|
||||||
|
return a[self.sortColumn] < b[self.sortColumn]
|
||||||
|
end
|
||||||
|
return UI.Grid.sortCompare(self, a, b)
|
||||||
|
end
|
||||||
|
|
||||||
|
function page.grid:eventHandler(event)
|
||||||
|
if event.type == 'grid_sort' then
|
||||||
|
config.sortColumn = event.sortColumn
|
||||||
|
config.inverseSort = event.inverseSort
|
||||||
|
Config.update('miloRemote', config)
|
||||||
|
end
|
||||||
|
return UI.Grid.eventHandler(self, event)
|
||||||
|
end
|
||||||
|
|
||||||
function page:transfer(item, count, msg)
|
function page:transfer(item, count, msg)
|
||||||
Sound.play('ui.button.click', .3)
|
Sound.play('ui.button.click', .3)
|
||||||
local response = self:sendRequest({ request = 'transfer', item = item, count = count }, msg)
|
local response = self:sendRequest({ request = 'transfer', item = item, count = count }, msg)
|
||||||
@@ -442,6 +452,24 @@ function page:refresh(requestType)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function page:applyFilter()
|
function page:applyFilter()
|
||||||
|
local function filterItems(t, filter, displayMode)
|
||||||
|
if filter or displayMode > 0 then
|
||||||
|
local r = { }
|
||||||
|
if filter then
|
||||||
|
filter = filter:lower()
|
||||||
|
end
|
||||||
|
for _,v in pairs(t) do
|
||||||
|
if not filter or string.find(v.lname, filter, 1, true) then
|
||||||
|
if filter or --displayMode == 0 or
|
||||||
|
displayMode == 1 and v.count > 0 then
|
||||||
|
table.insert(r, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return r
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
local t = filterItems(self.items, self.filter, config.displayMode)
|
local t = filterItems(self.items, self.filter, config.displayMode)
|
||||||
self.grid:setValues(t)
|
self.grid:setValues(t)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ local displayModes = {
|
|||||||
[1] = { text = 'I', help = 'Showing inventory items' },
|
[1] = { text = 'I', help = 'Showing inventory items' },
|
||||||
}
|
}
|
||||||
|
|
||||||
local listingPage = UI.Page {
|
local page = UI.Page {
|
||||||
menuBar = UI.MenuBar {
|
menuBar = UI.MenuBar {
|
||||||
buttons = {
|
buttons = {
|
||||||
{ text = 'Learn', event = 'learn' },
|
{ text = 'Learn', event = 'learn' },
|
||||||
@@ -45,7 +45,8 @@ local listingPage = UI.Page {
|
|||||||
{ heading = 'Min', key = 'low' , width = 4 },
|
{ heading = 'Min', key = 'low' , width = 4 },
|
||||||
{ heading = 'Max', key = 'limit' , width = 4 },
|
{ heading = 'Max', key = 'limit' , width = 4 },
|
||||||
},
|
},
|
||||||
sortColumn = 'displayName',
|
sortColumn = Milo:getState('sortColumn') or 'count',
|
||||||
|
inverseSort = Milo:getState('inverseSort'),
|
||||||
},
|
},
|
||||||
statusBar = UI.StatusBar {
|
statusBar = UI.StatusBar {
|
||||||
filter = UI.TextEntry {
|
filter = UI.TextEntry {
|
||||||
@@ -105,11 +106,11 @@ local listingPage = UI.Page {
|
|||||||
allItems = { }
|
allItems = { }
|
||||||
}
|
}
|
||||||
|
|
||||||
function listingPage.statusBar:draw()
|
function page.statusBar:draw()
|
||||||
return UI.Window.draw(self)
|
return UI.Window.draw(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage.grid:getRowTextColor(row, selected)
|
function page.grid:getRowTextColor(row, selected)
|
||||||
if row.is_craftable then
|
if row.is_craftable then
|
||||||
return colors.yellow
|
return colors.yellow
|
||||||
end
|
end
|
||||||
@@ -119,7 +120,7 @@ function listingPage.grid:getRowTextColor(row, selected)
|
|||||||
return UI.Grid:getRowTextColor(row, selected)
|
return UI.Grid:getRowTextColor(row, selected)
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage.grid:getDisplayValues(row)
|
function page.grid:getDisplayValues(row)
|
||||||
row = Util.shallowCopy(row)
|
row = Util.shallowCopy(row)
|
||||||
row.count = row.count > 0 and Util.toBytes(row.count)
|
row.count = row.count > 0 and Util.toBytes(row.count)
|
||||||
if row.low then
|
if row.low then
|
||||||
@@ -131,7 +132,34 @@ function listingPage.grid:getDisplayValues(row)
|
|||||||
return row
|
return row
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:eject(amount)
|
function page.grid:sortCompare(a, b)
|
||||||
|
if self.sortColumn ~= 'displayName' then
|
||||||
|
if a[self.sortColumn] == b[self.sortColumn] then
|
||||||
|
if self.inverseSort then
|
||||||
|
return a.displayName > b.displayName
|
||||||
|
end
|
||||||
|
return a.displayName < b.displayName
|
||||||
|
end
|
||||||
|
if a[self.sortColumn] == 0 then
|
||||||
|
return self.inverseSort
|
||||||
|
end
|
||||||
|
if b[self.sortColumn] == 0 then
|
||||||
|
return not self.inverseSort
|
||||||
|
end
|
||||||
|
return a[self.sortColumn] < b[self.sortColumn]
|
||||||
|
end
|
||||||
|
return UI.Grid.sortCompare(self, a, b)
|
||||||
|
end
|
||||||
|
|
||||||
|
function page.grid:eventHandler(event)
|
||||||
|
if event.type == 'grid_sort' then
|
||||||
|
Milo:setState('sortColumn', event.sortColumn)
|
||||||
|
Milo:setState('inverseSort', event.inverseSort)
|
||||||
|
end
|
||||||
|
return UI.Grid.eventHandler(self, event)
|
||||||
|
end
|
||||||
|
|
||||||
|
function page:eject(amount)
|
||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item and amount then
|
if item and amount then
|
||||||
-- get most up-to-date item
|
-- get most up-to-date item
|
||||||
@@ -159,7 +187,7 @@ function listingPage:eject(amount)
|
|||||||
Sound.play('entity.villager.no')
|
Sound.play('entity.villager.no')
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:eventHandler(event)
|
function page:eventHandler(event)
|
||||||
if event.type == 'quit' then
|
if event.type == 'quit' then
|
||||||
UI:exitPullEvents()
|
UI:exitPullEvents()
|
||||||
|
|
||||||
@@ -234,7 +262,7 @@ function listingPage:eventHandler(event)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:enable()
|
function page:enable()
|
||||||
local function updateStatus()
|
local function updateStatus()
|
||||||
self.statusBar.storageStatus.value =
|
self.statusBar.storageStatus.value =
|
||||||
context.storage:isOnline() and '' or 'offline'
|
context.storage:isOnline() and '' or 'offline'
|
||||||
@@ -268,13 +296,13 @@ function listingPage:enable()
|
|||||||
UI.Page.enable(self)
|
UI.Page.enable(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:disable()
|
function page:disable()
|
||||||
Event.off(self.timer)
|
Event.off(self.timer)
|
||||||
Event.off(self.handler)
|
Event.off(self.handler)
|
||||||
UI.Page.disable(self)
|
UI.Page.disable(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:refresh(force)
|
function page:refresh(force)
|
||||||
local throttle = function() self.throttle:update() end
|
local throttle = function() self.throttle:update() end
|
||||||
|
|
||||||
self.throttle:enable()
|
self.throttle:enable()
|
||||||
@@ -283,7 +311,7 @@ function listingPage:refresh(force)
|
|||||||
self.throttle:disable()
|
self.throttle:disable()
|
||||||
end
|
end
|
||||||
|
|
||||||
function listingPage:applyFilter()
|
function page:applyFilter()
|
||||||
local function filterItems(t, filter)
|
local function filterItems(t, filter)
|
||||||
if filter or displayMode > 0 then
|
if filter or displayMode > 0 then
|
||||||
local r = { }
|
local r = { }
|
||||||
@@ -292,7 +320,7 @@ function listingPage:applyFilter()
|
|||||||
end
|
end
|
||||||
for _,v in pairs(t) do
|
for _,v in pairs(t) do
|
||||||
if not filter or string.find(v.lname, filter, 1, true) then
|
if not filter or string.find(v.lname, filter, 1, true) then
|
||||||
if displayMode == 0 or
|
if filter or --displayMode == 0 or
|
||||||
displayMode == 1 and v.count > 0 then
|
displayMode == 1 and v.count > 0 then
|
||||||
table.insert(r, v)
|
table.insert(r, v)
|
||||||
end
|
end
|
||||||
@@ -307,4 +335,4 @@ function listingPage:applyFilter()
|
|||||||
self.grid:setValues(t)
|
self.grid:setValues(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
UI:addPage('listing', listingPage)
|
UI:addPage('listing', page)
|
||||||
|
|||||||
Reference in New Issue
Block a user