Guest User

Untitled

a guest
Dec 23rd, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. local function padLeft(str, w)
  2.   return string.rep(" ", w - #str) .. str
  3. end
  4.  
  5. local function findPeripheral(_type)
  6.   for _,name in pairs(peripheral.getNames()) do
  7.     if peripheral.getType(name) == _type then
  8.       return peripheral.wrap(name)
  9.     end
  10.   end
  11.  
  12.   error("Cannot find ".._type.." attached to this computer", 0) --# may as well put the error here
  13. end
  14.  
  15. local m = findPeripheral("monitor")
  16. local powerUnit = findPeripheral("mfsu") or findPeripheral("batbox") --# have the larger unit take priority, if an mfsu is found then a search will not be performed for a batbox
  17.  
  18. m.setTextScale(0.5)
  19.  
  20. local w, h = m.getSize()
  21. local total = (powerUnit.getCapacity or powerUnit.getEUCapacity)() --# a compatible call, it will find the function pointer and invoke it, whichever it may be
  22.  
  23. os.startTimer(1)
  24.  
  25. while true do
  26.   local stored = (powerUnit.getStored or powerUnit.getEUStored)() --# a compatible call, it will find the function pointer and invoke it, whichever it may be
  27.  
  28.   m.clear()
  29.   m.setCursorPos(1, 2)
  30.   m.setTextColour(colours.orange)
  31.   m.write("Energy:")
  32.   m.setTextColour(colours.white)
  33.   m.write(padLeft(tostring(stored), w - 7))
  34.  
  35.   m.setCursorPos(1, 4)
  36.   m.setTextColour(colours.orange)
  37.   m.write("Total:")
  38.   m.setTextColour(colours.white)
  39.   m.write(padLeft(tostring(total), w - 6))
  40.  
  41.   local p = (w-2) * stored / total
  42.  
  43.   m.setBackgroundColour(colours.lightGrey)
  44.   m.setCursorPos(2, 8)
  45.   m.write(string.rep(" ", w-2))
  46.   m.setBackgroundColour(colours.grey)
  47.   m.setCursorPos(2, 8)
  48.   m.write(string.rep(" ", p))
  49.   m.setBackgroundColour(colours.black)
  50.  
  51.   os.pullEvent("timer")
  52.   os.startTimer(1)
  53. end
Advertisement
Add Comment
Please, Sign In to add comment