Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ocs/apis/sensor")
- fcolors = {
- [0] = colors.white,
- [1] = colors.orange,
- [2] = colors.magenta,
- [3] = colors.lightBlue,
- [4] = colors.yellow,
- [5] = colors.lime,
- [6] = colors.pink,
- [7] = colors.gray,
- [8] = colors.lightGray,
- [9] = colors.cyan,
- [10] = colors.purple,
- [11] = colors.blue,
- [12] = colors.brown,
- [13] = colors.green,
- [14] = colors.red,
- [15] = colors.black,
- ["size"] = 16
- }
- -- Start --
- local ae2Sensor = sensor.wrap("left")
- local monitor = peripheral.wrap("back")
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- function printf(object)
- print(textutils.serialize(object))
- end
- function sort(a,b)
- return a.Size > b.Size
- end
- function spairs(t, order)
- if not t then
- error('spairs: nil passed')
- end
- -- collect the keys
- local keys = {}
- for k in pairs(t) do keys[#keys+1] = k end
- -- if order function given, sort by it by passing the table and keys a, b,
- -- otherwise just sort the keys
- if order then
- table.sort(keys, function(a,b) return order(t[a], t[b]) end)
- else
- table.sort(keys)
- end
- -- return the iterator function
- local i = 0
- return function()
- i = i + 1
- if keys[i] then
- return keys[i], t[keys[i]]
- end
- end
- end
- local ae2
- for k,_ in pairs(ae2Sensor.getTargets()) do
- ae2 = k
- break
- end
- monitor.clear()
- monitor.setCursorPos(1,1)
- local w,h = monitor.getSize()
- monitor.setBackgroundColor(colors.gray)
- for i=1,h+1 do
- if i == 1 or i == h+1 then
- monitor.clearLine()
- else
- monitor.setCursorPos(1,i)
- monitor.write(" ")
- monitor.setCursorPos(w,i)
- monitor.write(" ")
- end
- end
- monitor.setBackgroundColor(colors.black)
- -- actual program --
- local itemsToDisplayCount = h - 4 - 3
- function getItemsSorted(system)
- local storage = system.Items
- local currentItem = 0
- local itemsToDisplay = {}
- for _,v in spairs(storage, sort) do
- itemsToDisplay[currentItem] = v
- if currentItem >= itemsToDisplayCount then
- break
- end
- currentItem = currentItem + 1
- end
- return itemsToDisplay
- end
- -- Displaying --
- function clearLine()
- local w,_ = monitor.getSize()
- local x,y = monitor.getCursorPos()
- local toFill = ""
- for i=2,w-2 do
- toFill = toFill.." "
- end
- monitor.setCursorPos(2,y)
- monitor.write(toFill)
- monitor.setCursorPos(x,y)
- end
- function preparePrintM(line)
- monitor.setCursorPos(3,line)
- clearLine()
- end
- function writeBar(filled, total, colorfill, colorfilltext, colorEmpty, colorEmptyText, text)
- local barwidth = w-4
- local _,y = monitor.getCursorPos()
- local fill = math.floor((filled/total)*barwidth)
- text = text.." "
- local stringlength = #text
- local textstart = barwidth - stringlength
- local emptyTextLength = barwidth - fill
- local filledTextLength = stringlength - emptyTextLength
- local min = fill
- if min > textstart then
- min = textstart
- end
- local toFill = ""
- for i=1,min do
- toFill = toFill.." "
- end
- monitor.setBackgroundColor(colors.black)
- preparePrintM(y)
- monitor.setBackgroundColor(colorfill)
- monitor.setTextColor(colorfilltext)
- monitor.write(toFill)
- if textstart > min then
- local notToFill = ""
- local toEmptyFill = textstart-fill
- for i=1,toEmptyFill do
- notToFill = notToFill.." "
- end
- monitor.setBackgroundColor(colorEmpty)
- monitor.setTextColor(colorEmptyText)
- monitor.write(notToFill)
- end
- local inside
- local outside
- if filledTextLength > 0 then
- inside = string.sub(text,1, filledTextLength)
- outside = string.sub(text,filledTextLength+1,stringlength)
- else
- inside = ""
- outside = text
- end
- monitor.write(inside)
- monitor.setBackgroundColor(colorEmpty)
- monitor.setTextColor(colorEmptyText)
- monitor.write(outside)
- end
- function getLongestWord(table)
- local max = 0
- for _,k in pairs(table) do
- local name = k.Name
- if name == "" then
- name = k.RawName
- end
- if max < #name then
- max = #name
- end
- end
- return max
- end
- function formatNumber(amount)
- local formatted = amount
- local k
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if (k==0) then
- break
- end
- end
- return formatted
- end
- function lengthen(toLengthTo, string)
- local out = ""
- for i=#string,toLengthTo do
- out = out.." "
- end
- return out..string
- end
- local storagedata = {};
- function defaultAction()
- local system = ae2Sensor.getTargetDetails(ae2)
- monitor.setCursorPos(1,3)
- local usedBytes = system.UsedBytes
- local totalBytes = system.FreeBytes + usedBytes
- writeBar(usedBytes,totalBytes,colors.lightBlue, colors.gray, colors.gray, colors.white, usedBytes.."/"..totalBytes.." Bytes used")
- monitor.setCursorPos(1,4)
- local usedSlots = system.UsedTypes
- local totalSlots = usedSlots + system.FreeTypes
- writeBar(usedSlots,totalSlots,colors.lightBlue, colors.gray, colors.gray, colors.white, usedSlots.."/"..totalSlots.." Slots used")
- local items = getItemsSorted(system)
- local longestnum = string.len(""..items[0].Size)
- longestnum = longestnum+(longestnum-1)/3
- local maxLength = getLongestWord(items)
- for line=6,itemsToDisplayCount+5 do
- monitor.setCursorPos(1,line)
- local item = items[line-6]
- local itemCount = item.Size
- local itemName = item.Name
- local externalCount = 0
- local prefix = "";
- if itemName == "" then
- itemName = item.RawName
- end
- if storagedata[item.RawName] ~= nil then
- externalCount = storagedata[item.RawName]
- prefix = formatNumber(externalCount).."x Ext"
- end
- writeBar(externalCount, itemCount, colors.red, colors.gray, colors.gray, colors.white, prefix..lengthen(longestnum+4,formatNumber(itemCount)).."x "..lengthen(maxLength,itemName))
- end
- end
- local modem = peripheral.wrap("right")
- modem.open(5)
- function eventHandler()
- local _, _, _,
- _, message, _ = os.pullEvent("modem_message")
- print("Received a message: "..(message or "nil"))
- storagedata = textutils.unserialize(message)
- print("Fetched storagedata")
- end
- while true do
- parallel.waitForAny(defaultAction,eventHandler)
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement