Advertisement
Gronos02

energy.lua

Apr 7th, 2024 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. local storages = {
  2.     peripheral.wrap("modular_accumulator_0"),
  3.     peripheral.wrap("modular_accumulator_1")
  4. }
  5. local screen = peripheral.wrap("monitor_0")
  6.  
  7. term.redirect(screen)
  8. term.clear()
  9.  
  10. function centerText(text)
  11.     local x, y = term.getSize()
  12.     local x2, y2 = term.getCursorPos()
  13.     term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  14.     screen.write(text)
  15. end
  16.  
  17. function createProgressBar(storagePercentFormated)
  18.     local fullBarLength = math.floor(storagePercentFormated / 10)
  19.     local progressBar = ""
  20.     for i = 1, fullBarLength do
  21.         progressBar = progressBar .. "#"
  22.     end
  23.     for i = 1, 10 - fullBarLength do
  24.         progressBar = progressBar .. " "
  25.     end
  26.     progressBar = "[" .. progressBar .. "]"
  27.     return progressBar    
  28. end
  29.  
  30. function updateScreen(storage, index)
  31.     screen.setCursorPos(1, 6 + index * 4)    
  32.     local storagePercent = storage.getPercent()
  33.     local storageCapacity = storage.getCapacity()
  34.     local storagePercentFormated = tonumber(string.format("%.1f", storagePercent))
  35.     centerText("[[ Battery "..index.." ]]")
  36.     screen.setCursorPos(1, 7 + index * 4)
  37.     if (storagePercent >= 75) and (storagePercent <= 100) then
  38.         screen.setTextColor(32)
  39.     elseif (storagePercent >= 50) and (storagePercent < 75) then
  40.         screen.setTextColor(16)
  41.     elseif (storagePercent >= 0) and (storagePercent < 50) then
  42.         screen.setTextColor(16384)
  43.     end  
  44.     centerText(createProgressBar(storagePercentFormated.."  "..storagePercentFormated.."%"))
  45. end
  46.  
  47. while true do
  48.     for index, storage in ipairs(storages) do
  49.         updateScreen(storage, index)
  50.     end
  51.     sleep(0.5)
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement