LayZee

ComputerCraft Info Screen v3

Oct 7th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. --Resolution 57x24
  2. local params = {...}
  3.  
  4. if #params < 3 then
  5.   error("Use: info monitor_id filename(.txt) filename(.col)")
  6. end
  7.  
  8. local linkColor = colors.red
  9. local monitorId = params[1]
  10. local textFile = params[2] .. ".txt"
  11. local colorFile = params[3] .. ".col"
  12. local monitor
  13. local width, height
  14. local palette = {
  15.   text = nil,
  16.   background = nil,
  17.   header = nil
  18. }
  19.  
  20. local function fileExists(filename)
  21.   local file = fs.open(filename, "r")
  22.  
  23.   if file then
  24.     file.close()
  25.  
  26.     return true
  27.   end
  28.  
  29.   return false
  30. end
  31.  
  32. local function checkFile (filename)
  33.   if not fileExists(filename) then
  34.     error("File '" .. filename .. "' not found")
  35.   end
  36. end
  37.  
  38. local function getMonitor (sideOrId)
  39.   if not peripheral.getType(sideOrId) == "monitor" then
  40.     error("No monitor found")
  41.   end
  42.  
  43.   print("Monitor found: " .. sideOrId)
  44.   monitor = peripheral.wrap(sideOrId)
  45.   monitor.setTextScale(0.5)
  46.   width, height = monitor.getSize()
  47.   monitor.clear()
  48.   monitor.setCursorPos(1, 1)
  49. end
  50.  
  51. local function loadColors ()
  52.   local file = fs.open(colorFile, "r")
  53.  
  54.   for lineNumber = 1, 3 do
  55.     local line = file.readLine()
  56.  
  57.     if not line then
  58.       error(
  59.         colorFile .. ": No line "
  60.           .. lineNumber .. " found"
  61.       )
  62.     end
  63.  
  64.     if lineNumber == 1 then
  65.       palette.text = tonumber(line)
  66.     elseif lineNumber == 2 then
  67.       palette.background = tonumber(line)
  68.     else
  69.       palette.header = tonumber(line)
  70.     end
  71.   end
  72. end
  73.  
  74. local function newLine ()
  75.   local x, y = monitor.getCursorPos()
  76.  
  77.   monitor.setCursorPos(1, y + 1)
  78. end
  79.  
  80. local function wordWrap (text)
  81.   local remainingSpace = width - 2
  82.  
  83.   monitor.write(" ")
  84.  
  85.   for word in text:gmatch("[a-zA-Z0-9()'\"@!:*/.,_-]+") do
  86.     wordIsLink = false
  87.  
  88.     if word:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")
  89.     or word:sub(1, 7) == "http://" then
  90.       wordIsLink = true
  91.       monitor.setTextColor(linkColor)
  92.     end
  93.  
  94.     if #word > remainingSpace then
  95.       newLine()
  96.       monitor.write(" " .. word .. " ")
  97.       remainingSpace = width - #word - 3
  98.     else
  99.       monitor.write(word .. " ")
  100.       remainingSpace = remainingSpace - #word - 1
  101.     end
  102.  
  103.     if wordIsLink then
  104.       monitor.setTextColor(palette.text)
  105.     end
  106.   end
  107. end
  108.  
  109. local function drawBackground (color)
  110.   monitor.setBackgroundColor(color)
  111.   monitor.clear()
  112. end
  113.  
  114. local function centerPrint (lineNumber, textColor, text)
  115.   monitor.setCursorPos((width - #text) / 2, lineNumber)
  116.   monitor.setTextColor(textColor)
  117.   monitor.write(text)
  118. end
  119.  
  120. local function getData ()
  121.   local info = fs.open(textFile, "r")
  122.   local infoData = {}
  123.  
  124.   repeat
  125.     line = info.readLine()
  126.    
  127.     if line then
  128.       table.insert(infoData, line)
  129.     end
  130.   until line == nil
  131.  
  132.   info.close()
  133.  
  134.   return infoData
  135. end
  136.  
  137. --Draw a filled box for e.g. a button or shadow
  138. local function drawBox (x, y, width, height, color)
  139.   monitor.setBackgroundColor(color)
  140.   monitor.setTextColor(color)
  141.  
  142.   for i = 0, height - 1 do
  143.     monitor.setCursorPos(x, y + i)
  144.  
  145.     for j = 1, width do
  146.       monitor.write(" ")
  147.     end
  148.   end
  149. end
  150.  
  151. local function drawHeader (text)
  152.   --Draw shadow
  153.   drawBox(4, 3, width - 5, 3, colors.lightGray)
  154.  
  155.   --Draw header box
  156.   drawBox(3, 2, width - 5, 3, colors.white)
  157.  
  158.   --Write text
  159.   centerPrint(3, palette.header, text)
  160.   monitor.setCursorPos(1, 6)
  161. end
  162.  
  163. local function displayInfo ()
  164.   local text = getData()
  165.  
  166.   drawBackground(palette.background)
  167.  
  168.   for lineNumber, line in ipairs(text) do
  169.     if lineNumber == 1 then
  170.       drawHeader(line:upper())
  171.       monitor.setTextColor(palette.text)
  172.       monitor.setBackgroundColor(palette.background)
  173.     else
  174.       wordWrap(line)
  175.     end
  176.    
  177.     newLine()
  178.   end
  179. end
  180.  
  181. --Main program
  182. checkFile(textFile)
  183. checkFile(colorFile)
  184. getMonitor(monitorId)
  185. loadColors()
  186. displayInfo()
Advertisement
Add Comment
Please, Sign In to add comment