Skip to content

Instantly share code, notes, and snippets.

@Krutoy242
Last active March 5, 2023 07:25
Show Gist options
  • Save Krutoy242/780b54c0602d633b57c9532668a1150a to your computer and use it in GitHub Desktop.
Save Krutoy242/780b54c0602d633b57c9532668a1150a to your computer and use it in GitHub Desktop.
colossal-chest-info
--[[
Menu drawn with https://kirilllive.github.io/ASCII_Art_Paint/ascii_paint.html
installation:
wget https://gist.githubusercontent.com/Krutoy242/780b54c0602d633b57c9532668a1150a/raw/3b7903d09b572e8c495841c730933705602c1d4e/chest-info.lua -f && chest-info.lua
]]
local component = require'component'
local computer = require'computer'
-- local ic = component.inventory_controller
local unicode = require'unicode'
local gpu = component.gpu
function findChest()
for address, name in component.list() do
if string.find(name, "colossal") then return name end
end
end
local chest = component[findChest()]
-- local w, h = gpu.getResolution()
-- gpu.fill(1, 1, w, h, " ") -- clears the screen
local sleep = function(t)
local u = computer.uptime
local d = u() + (t or 1)
while true do
local signal = computer.pullSignal(d - u())
if signal == 'key_down' then return true end
if u() >= d then return false end
end
end
local side = 0
for i=1, 5 do
local sz = chest.getInventorySize(i)
if sz and sz > 9 then side = i end
end
--------------------------------------------------
-- GUI Panel
--------------------------------------------------
local splashLines = {
'╓───────────── Colossal Chest info ──────────────╖',
'╟████████████▓▓▓▓▓▓▒▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░╢',
'╙used free╜',
' ╔ Please remove from ╗ ',
'▐╚ chest: ╝▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
'▐ ▌ ',
}
for i=1, #splashLines do
if i <= 3 then
gpu.setForeground(0x000000)
gpu.setBackground(0xFFFFFF)
else
gpu.setForeground(0xFFFFFF)
gpu.setBackground(0x000000)
end
gpu.set(1, i, splashLines[i])
end
local function padLeft(text, pad)
return string.rep(' ', pad - string.len(text)) .. text
end
local function updateBar(used, total)
local s_used, s_free = tostring(used), tostring(total - used)
local text = s_used .. padLeft(s_free, 48 - #s_used)
local fract = math.floor((used / total) * 48)
gpu.setForeground(0x000000)
gpu.setBackground(0xFFFFFF)
gpu.set(2, 2, unicode.sub(text, 1, fract))
gpu.setForeground(0xFFFFFF)
gpu.setBackground(0x000000)
gpu.set(2+fract, 2, unicode.sub(text, fract+1))
end
local function createPanel(x, y, w, h)
local lines = 0
return {
print = function(text)
lines = lines + 1
gpu.copy(x, y+1, w, h-1, 0, -1)
gpu.set(x, y-1, unicode.sub(splashLines[y-1], x, x+w)) -- restore header
gpu.fill(x, y-1+h, w, 1, ' ')
gpu.set(x, y-1+h, unicode.sub(text, 1, w))
end,
adjust = function(line, text)
gpu.fill(x, y-1+h - line, w, 1, ' ')
gpu.set(x, y-1+h - line, unicode.sub(text, 1, w))
end,
clear = function()
gpu.fill(x, y, w, h, ' ')
lines = 0
end,
}
end
--------------------------------------------------
local store, occupied, reported, reportedCount
local panel = createPanel(2, 6, 22, 11)
-- local loadSymbols = {'⠟','⠯','⠷','⠾','⠽','⠻'}
-- local loadSymbolsLen = #loadSymbols
local function inspect(it)
if not it or not it.label then return end
occupied = occupied + 1
if store[it.label] and store[it.label] > 0 then
store[it.label] = store[it.label] + it.size
else
store[it.label] = it.size
end
if store[it.label] and store[it.label] >= 64 * 9 then
if not reported[it.label] then
panel.print(store[it.label] .. '+ ' .. it.label)
reported[it.label] = true
reportedCount = reportedCount + 1
else
panel.adjust(reportedCount, store[it.label] .. '+ ' .. it.label)
end
end
end
local function scanChest()
store = {}
occupied = 0
reported = {}
reportedCount = 0
-- Clear chat field
panel.clear()
local allStacks = chest.getAllStacks()
local size = chest.getInventorySize(side)
for i=1, size do
inspect(allStacks[i])
end
-- Show occupied amount
updateBar(occupied, size)
end
while true do
scanChest()
if sleep(5) then return end
end
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment