LayZee

ComputerCraft Monitor Info Screen

Aug 9th, 2013
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. local headerColor = colors.red
  2. local monitor
  3. local width, height
  4.  
  5. function getMonitor ()
  6.   for i, v in ipairs(rs.getSides()) do
  7.     if peripheral.getType(v) == "monitor" then
  8.       print("Monitor found on ", v, " side")
  9.       monitor = peripheral.wrap(v)
  10.       width, height = monitor.getSize()
  11.       monitor.clear()
  12.       monitor.setCursorPos(1, 1)
  13.      
  14.       return
  15.     end
  16.   end
  17.  
  18.   error("No monitor found")
  19. end
  20.  
  21. function newLine ()
  22.   local x, y = monitor.getCursorPos()
  23.  
  24.   monitor.setCursorPos(1, y + 1)
  25. end
  26.  
  27. function wordWrap (text)
  28.   local remainingSpace = width
  29.  
  30.   for word in text:gmatch("%S+") do
  31.     if #word > remainingSpace then
  32.       newLine()
  33.       monitor.write(word .. " ")
  34.       remainingSpace = width - #word - 1
  35.     else
  36.       monitor.write(word .. " ")
  37.       remainingSpace = remainingSpace - #word - 1
  38.     end
  39.   end
  40. end
  41.  
  42. function fillBackground ()
  43.   monitor.setBackgroundColor(colors.blue)
  44.  
  45.   for y = 1, height do
  46.     for x = 1, width do
  47.       monitor.write(" ")
  48.     end
  49.    
  50.     newLine()
  51.   end
  52.  
  53.   monitor.setCursorPos(1, 1)
  54. end
  55.  
  56. function cprint (text)
  57.   for i = 1, width do
  58.     monitor.write(" ")
  59.   end
  60.  
  61.   newLine()
  62.  
  63.   for i = 1, (width - #text) / 2 do
  64.     monitor.write(" ")
  65.   end
  66.  
  67.   monitor.setTextColor(headerColor)
  68.   monitor.write(text)
  69.  
  70.   for i = 1, (width - #text) / 2 + 1 do
  71.     monitor.write(" ")
  72.   end
  73.  
  74.   newLine()
  75.  
  76.   for i = 1, width do
  77.     monitor.write(" ")
  78.   end
  79.  
  80.   newLine()
  81.  
  82.   for i = 1, width do
  83.     monitor.write("-")
  84.   end
  85.  
  86.   monitor.setTextColor(colors.orange)
  87. end
  88.  
  89. function getData ()
  90.   local info = fs.open("/info.txt", "r")
  91.   local infoData = {}
  92.  
  93.   repeat
  94.     line = info.readLine()
  95.    
  96.     if line then
  97.       table.insert(infoData, line)
  98.     end
  99.   until line == nil
  100.  
  101.   info.close()
  102.  
  103.   return infoData
  104. end
  105.  
  106. function displayInfo ()
  107.   local info = getData()
  108.  
  109.   fillBackground()
  110.  
  111.   for i, v in ipairs(info) do
  112.     if i == 1 then
  113.       cprint(v:upper())
  114.     else
  115.       wordWrap(v)
  116.     end
  117.    
  118.     newLine()
  119.   end
  120. end
  121.  
  122. --Main program
  123. getMonitor()
  124. displayInfo()
Advertisement
Add Comment
Please, Sign In to add comment