milo: cleanup

This commit is contained in:
kepler155c
2018-11-23 11:39:39 -05:00
parent 7498276573
commit fa43186d52
13 changed files with 326 additions and 347 deletions

View File

@@ -46,6 +46,7 @@ Needs work
\030 \031 \030f\0317\130\030 \031 \030f\0317\130",
run = "Appstore.lua",
},
--[[
[ "131260cbfbb0c821f8eae5e7c3c296c7aa4d50b9" ] = {
title = "Music",
category = "Apps",
@@ -56,7 +57,6 @@ Needs work
run = "usr/apps/Music.lua",
requires = 'turtle',
},
--[[
[ "89307d419a2fe4fbb69af92b3d3af27b6ec14d3e" ] = {
title = "Telnet",
category = "Apps",

View File

@@ -205,7 +205,7 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem)
_G._debug({'eval', recipe.result, count })
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
local maxCount = math.floor((recipe.maxCount or 64) / recipe.count)
for key,icount in pairs(Craft.sumIngredients(recipe)) do
local itemCount = Craft.getItemCount(origItem.ingredients, key)

View File

@@ -1,5 +1,5 @@
local Config = require('config')
local Craft = require('turtle.craft')
local Craft = require('craft2')
local itemDB = require('itemDB')
local Util = require('util')

View File

@@ -287,20 +287,6 @@ function Storage:export(target, slot, count, item)
-- If no slot and full amount is not exported, then no need to check rest of adapters
-- ... so should not reach here
--[[
-- not found - scan all others
for _, adapter in self:onlineAdapters() do
if not adapter.cache or not adapter.cache[key] then
provide(adapter)
if count <= 0 then
_G._debug('STORAGE: FOUND: %s - %d', key, count)
break
end
end
end
--]]
return total
end
@@ -368,7 +354,7 @@ function Storage:import(source, slot, count, item)
if count <= 0 then
return total
end
if adapter.cache and adapter.cache[key] and not node.lock then
if not node.lock and adapter.cache and adapter.cache[key] then
insert(adapter)
end
end

301
milo/core/listing.lua Normal file
View File

@@ -0,0 +1,301 @@
local Craft = require('craft2')
local itemDB = require('itemDB')
local Event = require('event')
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local context = Milo:getContext()
local displayMode = Milo:getState('displayMode') or 0
local string = _G.string
local displayModes = {
[0] = { text = 'A', help = 'Showing all items' },
[1] = { text = 'I', help = 'Showing inventory items' },
[2] = { text = 'C', help = 'Showing craftable items' },
}
local function filterItems(t, filter)
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 or
displayMode == 2 and v.has_recipe then
table.insert(r, v)
end
end
end
return r
end
return t
end
local listingPage = UI.Page {
menuBar = UI.MenuBar {
buttons = {
{ text = 'Learn', event = 'learn' },
--{ text = 'Forget', event = 'forget' },
{ text = 'Craft', event = 'craft' },
{ text = 'Edit', event = 'details' },
{ text = 'Refresh', event = 'refresh', x = -12 },
{
text = '\206',
x = -3,
dropdown = {
{ text = 'Setup', event = 'network' },
UI.MenuBar.spacer,
{
text = 'Rescan storage',
event = 'rescan',
help = 'Rescan all inventories'
},
},
},
},
},
grid = UI.Grid {
y = 2, ey = -2,
columns = {
{ heading = ' Qty', key = 'count' , width = 4, justify = 'right' },
{ heading = 'Name', key = 'displayName' },
{ heading = 'Min', key = 'low' , width = 4 },
{ heading = 'Max', key = 'limit' , width = 4 },
},
sortColumn = 'displayName',
},
statusBar = UI.StatusBar {
filter = UI.TextEntry {
x = 1, ex = -17,
limit = 50,
shadowText = 'filter',
shadowTextColor = colors.gray,
backgroundColor = colors.cyan,
backgroundFocusColor = colors.cyan,
accelerators = {
[ 'enter' ] = 'eject',
},
},
storageStatus = UI.Text {
x = -16, ex = -9,
textColor = colors.lime,
backgroundColor = colors.cyan,
value = '',
},
amount = UI.TextEntry {
x = -8, ex = -4,
limit = 3,
shadowText = '1',
shadowTextColor = colors.gray,
backgroundColor = colors.black,
backgroundFocusColor = colors.black,
accelerators = {
[ 'enter' ] = 'eject_specified',
},
help = 'Specify an amount to send',
},
display = UI.Button {
x = -3,
event = 'toggle_display',
value = 0,
text = displayModes[displayMode].text,
help = displayModes[displayMode].help,
},
},
notification = UI.Notification(),
throttle = UI.Throttle {
textColor = colors.yellow,
borderColor = colors.gray,
},
accelerators = {
r = 'refresh',
[ 'control-r' ] = 'refresh',
[ 'control-e' ] = 'eject',
[ 'control-s' ] = 'eject_stack',
[ 'control-a' ] = 'eject_all',
[ 'control-m' ] = 'network',
q = 'quit',
},
}
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage.grid:getRowTextColor(row, selected)
if row.is_craftable then
return colors.yellow
end
if row.has_recipe then
return colors.cyan
end
return UI.Grid:getRowTextColor(row, selected)
end
function listingPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.count = row.count > 0 and Util.toBytes(row.count)
if row.low then
row.low = Util.toBytes(row.low)
end
if row.limit then
row.limit = Util.toBytes(row.limit)
end
return row
end
function listingPage:eventHandler(event)
if event.type == 'quit' then
UI:exitPullEvents()
elseif event.type == 'eject' or event.type == 'grid_select' then
local item = self.grid:getSelected()
if item then
item.count = Milo:craftAndEject(item, 1)
self.grid:draw()
end
elseif event.type == 'eject_stack' then
local item = self.grid:getSelected()
if item then
item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item))
self.grid:draw()
end
elseif event.type == 'eject_all' then
local item = self.grid:getSelected()
if item then
local updated = Milo:getItem(Milo:listItems(), item)
if updated then
Milo:craftAndEject(item, updated.count)
end
end
elseif event.type == 'eject_specified' then
local item = self.grid:getSelected()
local count = tonumber(self.statusBar.amount.value)
if item and count then
self.statusBar.amount:reset()
self:setFocus(self.statusBar.filter)
Milo:craftAndEject(item, count)
end
elseif event.type == 'network' then
UI:setPage('network')
elseif event.type == 'details' or event.type == 'grid_select_right' then
local item = self.grid:getSelected()
if item then
UI:setPage('item', item)
end
elseif event.type == 'refresh' then
self:refresh()
self.grid:draw()
self:setFocus(self.statusBar.filter)
elseif event.type == 'rescan' then
self:refresh(true)
self.grid:draw()
self:setFocus(self.statusBar.filter)
elseif event.type == 'toggle_display' then
displayMode = (displayMode + 1) % 3
Util.merge(event.button, displayModes[displayMode])
event.button:draw()
self:applyFilter()
self.grid:draw()
Milo:setState('displayMode', displayMode)
elseif event.type == 'learn' then
UI:setPage('learn')
elseif event.type == 'craft' then
local item = self.grid:getSelected()
if item then
if Craft.findRecipe(item) then -- or item.is_craftable then
UI:setPage('craft', self.grid:getSelected())
else
self.notification:error('No recipe defined')
end
end
elseif event.type == 'text_change' and event.element == self.statusBar.filter then
self.filter = event.text
if #self.filter == 0 then
self.filter = nil
end
self:applyFilter()
self.grid:draw()
self.statusBar.filter:focus()
else
UI.Page.eventHandler(self, event)
end
return true
end
function listingPage:enable()
Event.onTimeout(0, function()
self:refresh()
self:draw()
self:sync()
self.timer = Event.onInterval(3, function()
for _,v in pairs(self.allItems) do
local c = context.storage.cache[v.key]
v.count = c and c.count or 0
end
self.grid:draw()
self:sync()
end)
local function updateStatus()
self.statusBar.storageStatus.value =
context.storage:isOnline() and '' or 'offline'
self.statusBar.storageStatus.textColor =
context.storage:isOnline() and colors.lime or colors.red
end
updateStatus()
self.handler = Event.on({ 'storage_offline', 'storage_online' }, function()
updateStatus()
self.statusBar.storageStatus:draw()
self:sync()
end)
end)
self:setFocus(self.statusBar.filter)
UI.Page.enable(self)
end
function listingPage:disable()
Event.off(self.timer)
Event.off(self.handler)
UI.Page.disable(self)
end
function listingPage:refresh(force)
local throttle = function() self.throttle:update() end
self.throttle:enable()
self.allItems = Milo:mergeResources(Milo:listItems(force, throttle))
self:applyFilter()
self.throttle:disable()
end
function listingPage:applyFilter()
local t = filterItems(self.allItems, self.filter)
self.grid:setValues(t)
end
UI:addPage('listing', listingPage)

View File

@@ -442,8 +442,6 @@ function nodeWizard:enable(node)
self.node.adapter = adapter
node.adapter = adapter
_G._p3 = self.node -- TODO: remove - debugging
local choices = {
{ name = 'Ignore', value = 'ignore', '' },
{ name = 'Hidden', value = 'hidden', help = 'Do not show in list' },

View File

@@ -1,4 +1,4 @@
local Craft = require('turtle.craft')
local Craft = require('craft2')
local Milo = require('milo')
local sync = require('sync').sync
local Util = require('util')
@@ -36,6 +36,7 @@ function craftTask:craft(recipe, item)
item.ingredients[recipe.result].total = item.count
item.ingredients[recipe.result].crafted = item.crafted
--[[
_G._p2 = item
if not item.history then
item.history = { }
@@ -46,13 +47,14 @@ for k,v in pairs(item.ingredients) do
t.history.input[k] = Util.shallowCopy(v)
end
table.insert(item.history, t)
]]
Craft.craftRecipe(recipe, item.requested - item.crafted, context.storage, item)
--[[
for k,v in pairs(item.ingredients) do
t.history.output[k] = Util.shallowCopy(v)
end
]]
end
function craftTask:cycle()

View File

@@ -1,4 +1,4 @@
local Craft = require('turtle.craft')
local Craft = require('craft2')
local itemDB = require('itemDB')
local Milo = require('milo')
local UI = require('ui')

View File

@@ -1,16 +1,14 @@
local Ansi = require('ansi')
local Craft = require('turtle.craft')
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local Ansi = require('ansi')
local Craft = require('craft2')
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local device = _G.device
local context = Milo:getContext()
-- TODO: allow change of machine
local itemPage = UI.Page {
titleBar = UI.TitleBar {
title = 'Limit Resource',
@@ -29,18 +27,6 @@ local itemPage = UI.Page {
formLabel = 'Max', formKey = 'limit', help = 'Send to trash if above max',
validate = 'numeric',
},
--[[
[3] = UI.Chooser {
width = 7,
formLabel = 'Autocraft', formKey = 'auto',
nochoice = 'No',
choices = {
{ name = 'Yes', value = true },
{ name = 'No', value = false },
},
help = 'Craft until out of ingredients'
},
]]
[4] = UI.Checkbox {
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
help = 'Ignore damage of item',
@@ -163,7 +149,12 @@ function itemPage:enable(item)
end
function itemPage.machines.grid:isRowValid(_, value)
return value.mtype == 'machine'
local ignores = Util.transpose({ 'storage', 'ignore', 'hidden' })
if not ignores[value.mtype] then
local node = context.storage.nodes[value.name]
_debug(node)
return node and node.adapter and node.adapter.online and node.adapter.pushItems
end
end
function itemPage.machines.grid:getDisplayValues(row)

View File

@@ -1,4 +1,4 @@
local Craft = require('turtle.craft')
local Craft = require('craft2')
local Event = require('event')
local itemDB = require('itemDB')
local Milo = require('milo')

View File

@@ -1,300 +1 @@
local Craft = require('turtle.craft')
local itemDB = require('itemDB')
local Event = require('event')
local Milo = require('milo')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local context = Milo:getContext()
local displayMode = Milo:getState('displayMode') or 0
local displayModes = {
[0] = { text = 'A', help = 'Showing all items' },
[1] = { text = 'I', help = 'Showing inventory items' },
[2] = { text = 'C', help = 'Showing craftable items' },
}
local function filterItems(t, filter)
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 or
displayMode == 2 and v.has_recipe then
table.insert(r, v)
end
end
end
return r
end
return t
end
local listingPage = UI.Page {
menuBar = UI.MenuBar {
buttons = {
{ text = 'Learn', event = 'learn' },
--{ text = 'Forget', event = 'forget' },
{ text = 'Craft', event = 'craft' },
{ text = 'Edit', event = 'details' },
{ text = 'Refresh', event = 'refresh', x = -12 },
{
text = '\206',
x = -3,
dropdown = {
{ text = 'Setup', event = 'network' },
UI.MenuBar.spacer,
{
text = 'Rescan storage',
event = 'rescan',
help = 'Rescan all inventories'
},
},
},
},
},
grid = UI.Grid {
y = 2, ey = -2,
columns = {
{ heading = ' Qty', key = 'count' , width = 4, justify = 'right' },
{ heading = 'Name', key = 'displayName' },
{ heading = 'Min', key = 'low' , width = 4 },
{ heading = 'Max', key = 'limit' , width = 4 },
},
sortColumn = 'displayName',
},
statusBar = UI.StatusBar {
filter = UI.TextEntry {
x = 1, ex = -17,
limit = 50,
shadowText = 'filter',
shadowTextColor = colors.gray,
backgroundColor = colors.cyan,
backgroundFocusColor = colors.cyan,
accelerators = {
[ 'enter' ] = 'eject',
},
},
storageStatus = UI.Text {
x = -16, ex = -9,
textColor = colors.lime,
backgroundColor = colors.cyan,
value = '',
},
amount = UI.TextEntry {
x = -8, ex = -4,
limit = 3,
shadowText = '1',
shadowTextColor = colors.gray,
backgroundColor = colors.black,
backgroundFocusColor = colors.black,
accelerators = {
[ 'enter' ] = 'eject_specified',
},
help = 'Specify an amount to send',
},
display = UI.Button {
x = -3,
event = 'toggle_display',
value = 0,
text = displayModes[displayMode].text,
help = displayModes[displayMode].help,
},
},
notification = UI.Notification(),
throttle = UI.Throttle {
textColor = colors.yellow,
borderColor = colors.gray,
},
accelerators = {
r = 'refresh',
[ 'control-r' ] = 'refresh',
[ 'control-e' ] = 'eject',
[ 'control-s' ] = 'eject_stack',
[ 'control-a' ] = 'eject_all',
[ 'control-m' ] = 'network',
q = 'quit',
},
}
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage.grid:getRowTextColor(row, selected)
if row.is_craftable then
return colors.yellow
end
if row.has_recipe then
return colors.cyan
end
return UI.Grid:getRowTextColor(row, selected)
end
function listingPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.count = row.count > 0 and Util.toBytes(row.count)
if row.low then
row.low = Util.toBytes(row.low)
end
if row.limit then
row.limit = Util.toBytes(row.limit)
end
return row
end
function listingPage:eventHandler(event)
if event.type == 'quit' then
UI:exitPullEvents()
elseif event.type == 'eject' or event.type == 'grid_select' then
local item = self.grid:getSelected()
if item then
item.count = Milo:craftAndEject(item, 1)
self.grid:draw()
end
elseif event.type == 'eject_stack' then
local item = self.grid:getSelected()
if item then
item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item))
self.grid:draw()
end
elseif event.type == 'eject_all' then
local item = self.grid:getSelected()
if item then
local updated = Milo:getItem(Milo:listItems(), item)
if updated then
Milo:craftAndEject(item, updated.count)
end
end
elseif event.type == 'eject_specified' then
local item = self.grid:getSelected()
local count = tonumber(self.statusBar.amount.value)
if item and count then
self.statusBar.amount:reset()
self:setFocus(self.statusBar.filter)
Milo:craftAndEject(item, count)
end
elseif event.type == 'network' then
UI:setPage('network')
elseif event.type == 'details' or event.type == 'grid_select_right' then
local item = self.grid:getSelected()
if item then
UI:setPage('item', item)
end
elseif event.type == 'refresh' then
self:refresh()
self.grid:draw()
self:setFocus(self.statusBar.filter)
elseif event.type == 'rescan' then
self:refresh(true)
self.grid:draw()
self:setFocus(self.statusBar.filter)
elseif event.type == 'toggle_display' then
displayMode = (displayMode + 1) % 3
Util.merge(event.button, displayModes[displayMode])
event.button:draw()
self:applyFilter()
self.grid:draw()
Milo:setState('displayMode', displayMode)
elseif event.type == 'learn' then
UI:setPage('learn')
elseif event.type == 'craft' then
local item = self.grid:getSelected()
if item then
if Craft.findRecipe(item) then -- or item.is_craftable then
UI:setPage('craft', self.grid:getSelected())
else
self.notification:error('No recipe defined')
end
end
elseif event.type == 'text_change' and event.element == self.statusBar.filter then
self.filter = event.text
if #self.filter == 0 then
self.filter = nil
end
self:applyFilter()
self.grid:draw()
self.statusBar.filter:focus()
else
UI.Page.eventHandler(self, event)
end
return true
end
function listingPage:enable()
Event.onTimeout(0, function()
self:refresh()
self:draw()
self:sync()
self.timer = Event.onInterval(3, function()
for _,v in pairs(self.allItems) do
local c = context.storage.cache[v.key]
v.count = c and c.count or 0
end
self.grid:draw()
self:sync()
end)
local function updateStatus()
self.statusBar.storageStatus.value =
context.storage:isOnline() and '' or 'offline'
self.statusBar.storageStatus.textColor =
context.storage:isOnline() and colors.lime or colors.red
end
updateStatus()
self.handler = Event.on({ 'storage_offline', 'storage_online' }, function()
updateStatus()
self.statusBar.storageStatus:draw()
self:sync()
end)
end)
self:setFocus(self.statusBar.filter)
UI.Page.enable(self)
end
function listingPage:disable()
Event.off(self.timer)
Event.off(self.handler)
UI.Page.disable(self)
end
function listingPage:refresh(force)
local throttle = function() self.throttle:update() end
self.throttle:enable()
self.allItems = Milo:mergeResources(Milo:listItems(force, throttle))
self:applyFilter()
self.throttle:disable()
end
function listingPage:applyFilter()
local t = filterItems(self.allItems, self.filter)
self.grid:setValues(t)
end
UI:addPage('listing', listingPage)
--moved file

View File

@@ -1,4 +1,4 @@
local Craft = require('turtle.craft')
local Craft = require('craft2')
local itemDB = require('itemDB')
local Milo = require('milo')

View File

@@ -1,4 +1,4 @@
local Craft = require('turtle.craft')
local Craft = require('craft2')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')