Advertisement
EnterYourName

AE2Advanced

Nov 4th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1.  
  2. os.loadAPI("ocs/apis/sensor")
  3.  
  4. fcolors = {
  5.     [0] = colors.white,
  6.     [1] = colors.orange,
  7.     [2] = colors.magenta,
  8.     [3] = colors.lightBlue,
  9.     [4] = colors.yellow,
  10.     [5] = colors.lime,
  11.     [6] = colors.pink,
  12.     [7] = colors.gray,
  13.     [8] = colors.lightGray,
  14.     [9] = colors.cyan,
  15.     [10] = colors.purple,
  16.     [11] = colors.blue,
  17.     [12] = colors.brown,
  18.     [13] = colors.green,
  19.     [14] = colors.red,
  20.     [15] = colors.black,
  21.     ["size"] = 16
  22. }
  23.  
  24. -- Start --
  25.  
  26. local ae2Sensor = sensor.wrap("left")
  27. local monitor = peripheral.wrap("back")
  28.  
  29. monitor.setBackgroundColor(colors.black)
  30. monitor.setTextColor(colors.white)
  31.  
  32. function printf(object)
  33.  
  34.     print(textutils.serialize(object))
  35.  
  36. end
  37.  
  38. function sort(a,b)
  39.  
  40.     return a.Size > b.Size
  41.  
  42. end
  43.  
  44. function spairs(t, order)
  45.     if not t then
  46.         error('spairs: nil passed')
  47.     end
  48.  
  49.     -- collect the keys
  50.     local keys = {}
  51.     for k in pairs(t) do keys[#keys+1] = k end
  52.  
  53.     -- if order function given, sort by it by passing the table and keys a, b,
  54.     -- otherwise just sort the keys
  55.     if order then
  56.         table.sort(keys, function(a,b) return order(t[a], t[b]) end)
  57.     else
  58.         table.sort(keys)
  59.     end
  60.  
  61.     -- return the iterator function
  62.     local i = 0
  63.     return function()
  64.         i = i + 1
  65.         if keys[i] then
  66.             return keys[i], t[keys[i]]
  67.         end
  68.     end
  69. end
  70.  
  71. local ae2
  72.  
  73. for k,_ in pairs(ae2Sensor.getTargets()) do
  74.  
  75.     ae2 = k
  76.     break
  77.  
  78. end
  79.  
  80. monitor.clear()
  81. monitor.setCursorPos(1,1)
  82.  
  83. local w,h = monitor.getSize()
  84.  
  85. monitor.setBackgroundColor(colors.gray)
  86.  
  87. for i=1,h+1 do
  88.  
  89.     if i == 1 or i == h+1 then
  90.  
  91.         monitor.clearLine()
  92.  
  93.     else
  94.  
  95.         monitor.setCursorPos(1,i)
  96.         monitor.write(" ")
  97.         monitor.setCursorPos(w,i)
  98.         monitor.write(" ")
  99.  
  100.     end
  101.  
  102. end
  103.  
  104. monitor.setBackgroundColor(colors.black)
  105.  
  106. -- actual program --
  107.  
  108. local itemsToDisplayCount = h - 4 - 3
  109. function getItemsSorted(system)
  110.     local storage = system.Items
  111.     local currentItem = 0
  112.     local itemsToDisplay = {}
  113.  
  114.     for _,v in spairs(storage, sort) do
  115.  
  116.         itemsToDisplay[currentItem] = v
  117.         if currentItem >= itemsToDisplayCount then
  118.             break
  119.         end
  120.         currentItem = currentItem + 1
  121.  
  122.     end
  123.  
  124.     return itemsToDisplay
  125. end
  126.  
  127. -- Displaying --
  128.  
  129. function clearLine()
  130.     local w,_ = monitor.getSize()
  131.     local x,y = monitor.getCursorPos()
  132.  
  133.     local toFill = ""
  134.  
  135.     for i=2,w-2 do
  136.  
  137.         toFill = toFill.." "
  138.  
  139.     end
  140.  
  141.     monitor.setCursorPos(2,y)
  142.     monitor.write(toFill)
  143.  
  144.     monitor.setCursorPos(x,y)
  145. end
  146.  
  147. function preparePrintM(line)
  148.     monitor.setCursorPos(3,line)
  149.     clearLine()
  150. end
  151.  
  152. function writeBar(filled, total, colorfill, colorfilltext, colorEmpty, colorEmptyText, text)
  153.  
  154.     local barwidth = w-4
  155.     local _,y = monitor.getCursorPos()
  156.  
  157.     local fill = math.floor((filled/total)*barwidth)
  158.     text = text.." "
  159.     local stringlength = #text
  160.  
  161.     local textstart = barwidth - stringlength
  162.     local emptyTextLength = barwidth - fill
  163.     local filledTextLength = stringlength - emptyTextLength
  164.  
  165.     local min = fill
  166.     if min > textstart then
  167.         min = textstart
  168.     end
  169.  
  170.     local toFill = ""
  171.  
  172.     for i=1,min do
  173.         toFill = toFill.." "
  174.     end
  175.  
  176.     monitor.setBackgroundColor(colors.black)
  177.  
  178.     preparePrintM(y)
  179.  
  180.     monitor.setBackgroundColor(colorfill)
  181.     monitor.setTextColor(colorfilltext)
  182.  
  183.     monitor.write(toFill)
  184.  
  185.     if textstart > min then
  186.  
  187.         local notToFill = ""
  188.         local toEmptyFill = textstart-fill
  189.         for i=1,toEmptyFill do
  190.             notToFill = notToFill.." "
  191.         end
  192.  
  193.         monitor.setBackgroundColor(colorEmpty)
  194.         monitor.setTextColor(colorEmptyText)
  195.  
  196.         monitor.write(notToFill)
  197.  
  198.     end
  199.  
  200.     local inside
  201.     local outside
  202.  
  203.     if filledTextLength > 0 then
  204.         inside = string.sub(text,1, filledTextLength)
  205.         outside = string.sub(text,filledTextLength+1,stringlength)
  206.     else
  207.         inside = ""
  208.         outside = text
  209.     end
  210.  
  211.     monitor.write(inside)
  212.  
  213.     monitor.setBackgroundColor(colorEmpty)
  214.     monitor.setTextColor(colorEmptyText)
  215.  
  216.     monitor.write(outside)
  217.  
  218. end
  219.  
  220. function getLongestWord(table)
  221.  
  222.     local max = 0
  223.  
  224.     for _,k in pairs(table) do
  225.  
  226.         local name = k.Name
  227.         if name == "" then
  228.             name = k.RawName
  229.         end
  230.  
  231.         if max < #name then
  232.             max = #name
  233.         end
  234.  
  235.     end
  236.  
  237.     return max
  238.  
  239. end
  240.  
  241. function formatNumber(amount)
  242.     local formatted = amount
  243.     local k
  244.     while true do
  245.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  246.         if (k==0) then
  247.             break
  248.         end
  249.     end
  250.     return formatted
  251. end
  252.  
  253. function lengthen(toLengthTo, string)
  254.  
  255.     local out = ""
  256.  
  257.     for i=#string,toLengthTo do
  258.  
  259.         out = out.." "
  260.  
  261.     end
  262.  
  263.     return out..string
  264.  
  265. end
  266.  
  267. local storagedata = {};
  268.  
  269. function defaultAction()
  270.  
  271.     local system = ae2Sensor.getTargetDetails(ae2)
  272.  
  273.     monitor.setCursorPos(1,3)
  274.  
  275.     local usedBytes = system.UsedBytes
  276.     local totalBytes = system.FreeBytes + usedBytes
  277.  
  278.     writeBar(usedBytes,totalBytes,colors.lightBlue, colors.gray, colors.gray, colors.white, usedBytes.."/"..totalBytes.." Bytes used")
  279.  
  280.     monitor.setCursorPos(1,4)
  281.  
  282.     local usedSlots = system.UsedTypes
  283.     local totalSlots = usedSlots + system.FreeTypes
  284.  
  285.     writeBar(usedSlots,totalSlots,colors.lightBlue, colors.gray, colors.gray, colors.white, usedSlots.."/"..totalSlots.." Slots used")
  286.  
  287.     local items = getItemsSorted(system)
  288.     local longestnum = string.len(""..items[0].Size)
  289.     longestnum = longestnum+(longestnum-1)/3
  290.  
  291.     local maxLength = getLongestWord(items)
  292.  
  293.     for line=6,itemsToDisplayCount+5 do
  294.  
  295.         monitor.setCursorPos(1,line)
  296.  
  297.         local item = items[line-6]
  298.         local itemCount = item.Size
  299.         local itemName = item.Name
  300.         local externalCount = 0
  301.         local prefix = "";
  302.  
  303.  
  304.         if itemName == "" then
  305.             itemName = item.RawName
  306.         end
  307.  
  308.         if storagedata[item.RawName] ~= nil then
  309.  
  310.             externalCount = storagedata[item.RawName]
  311.             prefix = formatNumber(externalCount).."x Ext"
  312.  
  313.         end
  314.  
  315.         writeBar(externalCount, itemCount, colors.red, colors.gray, colors.gray, colors.white, prefix..lengthen(longestnum+4,formatNumber(itemCount)).."x "..lengthen(maxLength,itemName))
  316.  
  317.     end
  318.  
  319.  
  320.  
  321. end
  322.  
  323. local modem = peripheral.wrap("right")
  324. modem.open(5)
  325.  
  326. function eventHandler()
  327.  
  328.     local _, _, _,
  329.     _, message, _ = os.pullEvent("modem_message")
  330.  
  331.     print("Received a message: "..(message or "nil"))
  332.  
  333.     storagedata = textutils.unserialize(message)
  334.     print("Fetched storagedata")
  335.  
  336. end
  337.  
  338. while true do
  339.     parallel.waitForAny(defaultAction,eventHandler)
  340.     sleep(0.1)
  341. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement