Advertisement
Gokborg

IMonitorAPI

Sep 8th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. imonitor = {}
  2.  
  3. local monitor
  4.  
  5. local function round(num)
  6.     return math.floor(num + 0.5)
  7. end
  8.  
  9. function exact_send(x, y, msg)
  10.     monitor.setCursorPos(x,y)
  11.     monitor.write(msg)
  12. end
  13.  
  14. local function getCX(msg)
  15.     local width, height = monitor.getSize()
  16.     local length = string.len(msg)
  17.     local cx = round(((width+1)-length)/2)
  18.     return cx
  19. end
  20.  
  21. function f_send(pos, y, msg)
  22.     local length = string.len(msg)
  23.     local width, height = monitor.getSize()
  24.     if pos == "RIGHT" then
  25.         exact_send(width-length, y, msg)
  26.     elseif pos == "CENTER" then
  27.         local cx = round(((width+1)-length)/2)
  28.         exact_send(cx, y, msg)
  29.     elseif pos == "LEFT" then
  30.         exact_send(1, y, msg)
  31.     else
  32.         error("Invalid argument: "..pos)
  33.     end
  34. end
  35.  
  36. function c_send(y, msg)
  37.     f_send("CENTER", y, msg)
  38. end
  39.  
  40. function r_send(y, msg)
  41.     f_send("RIGHT", y, msg)
  42. end
  43.  
  44. function l_send(y, msg)
  45.     f_send("LEFT", y, msg)
  46. end
  47.  
  48. --x can be a # or a position ('LEFT', 'RIGHT', 'CENTER')
  49. function send(x, y, msg)
  50.     if type(x) == "string" then
  51.         f_send(x, y, msg)
  52.         return
  53.     end
  54.     exact_send(x, y, msg)
  55. end
  56.  
  57. function draw_color_bar(x, y, color, percent)
  58.     send(x,y,"[")
  59.     local limit = round(percent/10)
  60.     local oX = getCX(limit)
  61.     for i = 0, limit, 1 do
  62.         monitor.setCursorPos(getX(oX+i), y)
  63.         monitor.blit(" ", color, color)
  64.     end
  65.     for i = limit, 9, 1 do
  66.         send(getX(oX+i), y, " ")
  67.     end
  68.     send(getX(oX+9), y, "]")
  69. end
  70.  
  71. function draw_bar(x, y, percent)
  72.     local bar = "["
  73.     local limit = round(percent/10)
  74.     for i=0, limit, 1 do
  75.         bar = bar.."="
  76.     end
  77.     for i=limit, 9, 1 do
  78.         bar = bar.." "
  79.     end
  80.     bar = bar.."]"
  81.    
  82.     send(x, y, bar)
  83. end
  84.  
  85. function setMonitor(some_monitor)
  86.     monitor = some_monitor
  87. end
  88.  
  89. return imonitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement