Advertisement
EnterYourName

AE2System

Oct 14th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.21 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.     if a.Name == "Cobblestone" then
  41.         return false
  42.     end
  43.     if b.Name == "Cobblestone" then
  44.         return true
  45.     end
  46.  
  47.     return a.Size > b.Size
  48.  
  49. end
  50.  
  51. function spairs(t, order)
  52.     if not t then
  53.         error('spairs: nil passed')
  54.     end
  55.  
  56.     -- collect the keys
  57.     local keys = {}
  58.     for k in pairs(t) do keys[#keys+1] = k end
  59.  
  60.     -- if order function given, sort by it by passing the table and keys a, b,
  61.     -- otherwise just sort the keys
  62.     if order then
  63.         table.sort(keys, function(a,b) return order(t[a], t[b]) end)
  64.     else
  65.         table.sort(keys)
  66.     end
  67.  
  68.     -- return the iterator function
  69.     local i = 0
  70.     return function()
  71.         i = i + 1
  72.         if keys[i] then
  73.             return keys[i], t[keys[i]]
  74.         end
  75.     end
  76. end
  77.  
  78. local ae2
  79.  
  80. for k,_ in pairs(ae2Sensor.getTargets()) do
  81.  
  82.     ae2 = k
  83.     break
  84.  
  85. end
  86.  
  87. monitor.clear()
  88. monitor.setCursorPos(1,1)
  89.  
  90. local w,h = monitor.getSize()
  91.  
  92. monitor.setBackgroundColor(colors.gray)
  93.  
  94. for i=1,h+1 do
  95.  
  96.     if i == 1 or i == h+1 then
  97.  
  98.         monitor.clearLine()
  99.  
  100.     else
  101.  
  102.         monitor.setCursorPos(1,i)
  103.         monitor.write(" ")
  104.         monitor.setCursorPos(w,i)
  105.         monitor.write(" ")
  106.  
  107.     end
  108.  
  109. end
  110.  
  111. monitor.setBackgroundColor(colors.black)
  112.  
  113. -- actual program --
  114.  
  115. local itemsToDisplayCount = h - 4 - 3
  116. function getItemsSorted(system)
  117.     local storage = system.Items
  118.     local currentItem = 0
  119.     local itemsToDisplay = {}
  120.  
  121.     for _,v in spairs(storage, sort) do
  122.  
  123.         itemsToDisplay[currentItem] = v
  124.         if currentItem >= itemsToDisplayCount then
  125.             break
  126.         end
  127.         currentItem = currentItem + 1
  128.  
  129.     end
  130.  
  131.     return itemsToDisplay
  132. end
  133.  
  134. -- Displaying --
  135.  
  136. function clearLine()
  137.     local w,_ = monitor.getSize()
  138.     local x,y = monitor.getCursorPos()
  139.  
  140.     local toFill = ""
  141.  
  142.     for i=2,w-2 do
  143.  
  144.         toFill = toFill.." "
  145.  
  146.     end
  147.  
  148.     monitor.setCursorPos(2,y)
  149.     monitor.write(toFill)
  150.  
  151.     monitor.setCursorPos(x,y)
  152. end
  153.  
  154. function preparePrintM(line)
  155.     monitor.setCursorPos(3,line)
  156.     clearLine()
  157. end
  158.  
  159. function writeBar(filled, total, colorfill, colorfilltext, text)
  160.  
  161.     local colorsEmpty = colors.gray
  162.     local colorsEmptyText = colors.white
  163.  
  164.     local barwidth = w-4
  165.     local _,y = monitor.getCursorPos()
  166.  
  167.     local fill = math.floor((filled/total)*barwidth)
  168.     text = text.." "
  169.     local stringlength = #text
  170.  
  171.     local textstart = barwidth - stringlength
  172.     local emptyTextLength = barwidth - fill
  173.     local filledTextLength = stringlength - emptyTextLength
  174.  
  175.     local min = fill
  176.     if min > textstart then
  177.         min = textstart
  178.     end
  179.  
  180.     local toFill = ""
  181.  
  182.     for i=1,min do
  183.         toFill = toFill.." "
  184.     end
  185.  
  186.     monitor.setBackgroundColor(colors.black)
  187.  
  188.     preparePrintM(y)
  189.  
  190.     monitor.setBackgroundColor(colorfill)
  191.     monitor.setTextColor(colorfilltext)
  192.  
  193.     monitor.write(toFill)
  194.  
  195.     if textstart > min then
  196.  
  197.         local notToFill = ""
  198.         local toEmptyFill = textstart-fill
  199.         for i=1,toEmptyFill do
  200.             notToFill = notToFill.." "
  201.         end
  202.  
  203.         monitor.setBackgroundColor(colorsEmpty)
  204.         monitor.setTextColor(colorsEmptyText)
  205.  
  206.         monitor.write(notToFill)
  207.  
  208.     end
  209.  
  210.     local inside
  211.     local outside
  212.  
  213.     if filledTextLength > 0 then
  214.         inside = string.sub(text,1, filledTextLength)
  215.         outside = string.sub(text,filledTextLength+1,stringlength)
  216.     else
  217.         inside = ""
  218.         outside = text
  219.     end
  220.  
  221.     monitor.write(inside)
  222.  
  223.     monitor.setBackgroundColor(colors.gray)
  224.     monitor.setTextColor(colors.white)
  225.  
  226.     monitor.write(outside)
  227.  
  228. end
  229.  
  230. function getLongestWord(table)
  231.  
  232.     local max = 0
  233.  
  234.     for _,k in pairs(table) do
  235.  
  236.         local name = k.Name
  237.         if name == "" then
  238.             name = k.RawName
  239.         end
  240.  
  241.         if max < #name then
  242.             max = #name
  243.         end
  244.  
  245.     end
  246.  
  247.     return max
  248.  
  249. end
  250.  
  251. function formatNumber(amount)
  252.     local formatted = amount
  253.     local k
  254.     while true do
  255.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  256.         if (k==0) then
  257.             break
  258.         end
  259.     end
  260.     return formatted
  261. end
  262.  
  263. function lengthen(toLengthTo, number, string)
  264.  
  265.     local out = ""
  266.  
  267.     for i=#string,toLengthTo do
  268.  
  269.         out = out.." "
  270.  
  271.     end
  272.  
  273.     return formatNumber(number).."x "..out..string
  274.  
  275. end
  276.  
  277. while true do
  278.  
  279.     local system = ae2Sensor.getTargetDetails(ae2)
  280.  
  281.     monitor.setCursorPos(1,3)
  282.  
  283.     local usedBytes = system.UsedBytes
  284.     local totalBytes = system.FreeBytes + usedBytes
  285.  
  286.     writeBar(usedBytes,totalBytes,colors.lightBlue, colors.gray,usedBytes.."/"..totalBytes.." Bytes used")
  287.  
  288.     monitor.setCursorPos(1,4)
  289.  
  290.     local usedSlots = system.UsedTypes
  291.     local totalSlots = usedSlots + system.FreeTypes
  292.  
  293.     writeBar(usedSlots,totalSlots,colors.lightBlue, colors.gray,usedSlots.."/"..totalSlots.." Slots used")
  294.  
  295.     local items = getItemsSorted(system)
  296.     local maxcount = items[0].Size
  297.  
  298.     local maxLength = getLongestWord(items)
  299.  
  300.     for line=6,itemsToDisplayCount+5 do
  301.  
  302.         monitor.setCursorPos(1,line)
  303.  
  304.         local item = items[line-6]
  305.         local itemCount = item.Size
  306.         local itemName = item.Name
  307.         if itemName == "" then
  308.             itemName = item.RawName
  309.         end
  310.  
  311.         writeBar(itemCount,maxcount,colors.yellow, colors.gray,lengthen(maxLength,itemCount,itemName))
  312.  
  313.  
  314.     end
  315.  
  316.  
  317.     sleep(0.1)
  318.  
  319. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement