Advertisement
cbotx

Monitor Displayer

Sep 17th, 2022 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | Source Code | 0 0
  1. --[[
  2. Instructions:
  3. > label set [Displayer Name]
  4. > pastebin get J6Ss7FZd [Program Name]
  5. > [Program Name]
  6. --]]
  7.  
  8. local display_data = {}
  9.  
  10. function split(inputstr, sep)
  11.   if sep == nil then
  12.     sep = "%s"
  13.   end
  14.   local t={}
  15.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  16.     table.insert(t, str)
  17.   end
  18.   return t
  19. end
  20.  
  21. function updateData(msg)
  22.   local category = msg
  23.   local lines = split(msg, "\n")
  24.   if #lines > 1 then
  25.     display_data[lines[1]] = {unpack(lines, 2, #lines)}
  26.   else
  27.     display_data[lines[1]] = {}
  28.   end
  29. end
  30.  
  31. function getAbsolutePosition(screen_width, screen_height, pane_width, line_number)
  32.   local x = 1
  33.   screen_height = screen_height - 1
  34.   while line_number > screen_height do
  35.     line_number = line_number - screen_height
  36.     x = x + pane_width
  37.   end
  38.   if x + pane_width * 2 > screen_width + 1 then
  39.     return x, line_number + 1, screen_width - x + 1
  40.   else
  41.     return x, line_number + 1, pane_width
  42.   end
  43. end
  44.  
  45. function displayTitle(m, screen_width, str)
  46.   m.setCursorPos(1, 1)
  47.   m.setBackgroundColor(colors.yellow)
  48.   m.setTextColor(colors.black)
  49.   m.write(str..string.rep(" ", screen_width - string.len(str)))
  50. end
  51.  
  52. function displayLabel(m, x, y, length, str)
  53.   length = length - 1
  54.   m.setCursorPos(x, y)
  55.   m.setBackgroundColor(colors.purple)
  56.   m.setTextColor(colors.white)
  57.   m.write(str..string.rep(" ", length - string.len(str)))
  58. end
  59.  
  60. function displayItem(m, x, y, length, leftstr, rightstr, percent)
  61.   leftstr = split(leftstr, ":")[2]
  62.   length = length - 1
  63.   local hlcolor = colors.lime
  64.   if percent < 0.2 then
  65.     hlcolor = colors.red
  66.   elseif percent < 0.5 then
  67.     hlcolor  = colors.orange
  68.   end
  69.   hl_len = math.floor(length * percent)
  70.   remain_len = length - string.len(leftstr) - string.len(rightstr)
  71.   local str
  72.   if remain_len > 0 then
  73.     str = leftstr..string.rep(" ", remain_len)..rightstr
  74.   else
  75.     str = string.sub(leftstr, 1, string.len(leftstr) + remain_len - 3)..".. "..rightstr
  76.   end
  77.   m.setCursorPos(x, y)
  78.   m.setBackgroundColor(hlcolor)
  79.   m.setTextColor(colors.white)
  80.   m.write(string.sub(str, 1, hl_len))
  81.   m.setCursorPos(x + hl_len, y)
  82.   m.setBackgroundColor(colors.gray)
  83.   m.setTextColor(colors.white)
  84.   m.write(string.sub(str, hl_len + 1, string.len(str)))
  85. end
  86.  
  87. function displayAll(m, title)
  88.   m.setTextScale(1)
  89.   m.setBackgroundColor(colors.gray)
  90.   m.clear()
  91.   local screen_width, screen_height = m.getSize()
  92.   local pane_width = screen_width / math.floor(screen_width / 30)
  93.   local item_width = pane_width / 2
  94.   local line_number = 1
  95.   displayTitle(m, screen_width, title)
  96.   for category, items in pairs(display_data) do
  97.     local x, y, width = getAbsolutePosition(screen_width, screen_height, pane_width, line_number)
  98.     displayLabel(m, x, y, width, category)
  99.     for i, item in pairs(items) do
  100.       content = split(item, " ")
  101.       if content[3] ~= 0 then
  102.         if (i % 2 == 1) then
  103.           line_number = line_number + 1
  104.           x, y, width = getAbsolutePosition(screen_width, screen_height, pane_width, line_number)
  105.           item_width = math.floor(width / 2)
  106.           displayItem(m, x, y, item_width, content[1], content[2], content[2] / content[3])
  107.         end
  108.      
  109.         if (i % 2 == 0) then
  110.           item_width = math.floor(width / 2)
  111.           displayItem(m, x + item_width, y, width - item_width, content[1], content[2], content[2] / content[3])
  112.         end
  113.       end
  114.      
  115.     end
  116.     line_number = line_number + 1
  117.   end
  118. end
  119.  
  120. function main()
  121.   local monitor = peripheral.find("monitor")
  122.   monitor.setTextScale(0.5)
  123.   peripheral.find("modem", rednet.open)
  124.   local id, msg
  125.   while true do
  126.     while not rednet.isOpen() do
  127.       os.sleep(10)
  128.       peripheral.find("modem", rednet.open)
  129.     end
  130.     id, msg = rednet.receive("ITEM_UPDATE")
  131.     updateData(msg)
  132.     displayAll(monitor, os.getComputerLabel())
  133.   end
  134. end
  135.  
  136. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement