LayZee

ComputerCraft Info Screen v2

Sep 11th, 2013
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. local headerColor = colors.lightBlue
  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 - 2
  29.  
  30.   monitor.write(" ")
  31.  
  32.   for word in text:gmatch("[a-zA-Z0-9!.,-]+") do
  33.     if #word > remainingSpace then
  34.       newLine()
  35.       monitor.write(" " .. word .. " ")
  36.       remainingSpace = width - #word - 2
  37.     else
  38.       monitor.write(word .. " ")
  39.       remainingSpace = remainingSpace - #word - 2
  40.     end
  41.   end
  42. end
  43.  
  44. function drawBackground (color)
  45.   monitor.setBackgroundColor(color)
  46.   monitor.clear()
  47. end
  48.  
  49. function centerPrint (lineNumber, textColor, text)
  50.   monitor.setCursorPos((width - #text) / 2, lineNumber)
  51.   monitor.setTextColor(textColor)
  52.   monitor.write(text)
  53. end
  54.  
  55. function getData ()
  56.   local info = fs.open("/info.txt", "r")
  57.   local infoData = {}
  58.  
  59.   repeat
  60.     line = info.readLine()
  61.    
  62.     if line then
  63.       table.insert(infoData, line)
  64.     end
  65.   until line == nil
  66.  
  67.   info.close()
  68.  
  69.   return infoData
  70. end
  71.  
  72. --Draw a filled box for e.g. a button or shadow
  73. function drawBox (x, y, width, height, color)
  74.   monitor.setBackgroundColor(color)
  75.   monitor.setTextColor(color)
  76.  
  77.   for i = 0, height - 1 do
  78.     monitor.setCursorPos(x, y + i)
  79.  
  80.     for j = 1, width do
  81.       monitor.write(" ")
  82.     end
  83.   end
  84. end
  85.  
  86. function drawHeader (fillColor, textColor, text)
  87.   --Draw shadow
  88.   drawBox(4, 3, width - 5, 3, colors.lightGray)
  89.  
  90.   --Draw header box
  91.   drawBox(3, 2, width - 5, 3, fillColor)
  92.  
  93.   --Write text
  94.   monitor.setTextColor(textColor)
  95.   centerPrint(3, textColor, text)
  96.   monitor.setCursorPos(1, 6)
  97.   monitor.setBackgroundColor(colors.white)
  98. end
  99.  
  100. function displayInfo ()
  101.   local info = getData()
  102.  
  103.   drawBackground(colors.white)
  104.  
  105.   for i, v in ipairs(info) do
  106.     if i == 1 then
  107.       drawHeader(colors.red, colors.orange, v:upper())
  108.     else
  109.       wordWrap(v)
  110.     end
  111.    
  112.     newLine()
  113.   end
  114. end
  115.  
  116. --Main program
  117. getMonitor()
  118. displayInfo()
Advertisement
Add Comment
Please, Sign In to add comment