mj41374

CenterTestV3

Oct 2nd, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --This is the classroom monitor setup routine
  2. --written by Matt Kelliher
  3. --
  4. -- Useage: <program_name> [-d] [-A|-R|-G|-B|-P] <text>
  5. -- If you would like to see debugging comments on the terminal then pass in the -d
  6. -- option when you run this program before your text
  7. local tArgs = { ... }
  8. assert( #tArgs >= 1, "Useage: <program_name> [-d] <text>" )
  9.  
  10. --Parse the flags:
  11. local param1 = tArgs[1]
  12. local param2 --optional
  13. local param3 --optional
  14. if #tArgs >= 2 then param2 = tArgs[2] end
  15. if #tArgs >= 3 then param3 = tArgs[3] end
  16.  
  17. --Now figure out if the debug flag was set
  18. local debug = false
  19. local GLOBALtext = ""
  20. if param1 == "-d" then
  21.   debug = true
  22.   assert( #tArgs >= 2, "Useage: <program_name> [-d] <text>" )
  23.   GLOBALtext = param2
  24. else
  25.   GLOBALtext = param1
  26. end
  27.  
  28.  
  29. --First: we assume there are 4 equal-sized
  30. --monitors in the room connected to the network
  31. --we'll find them on the network,
  32. --give them names based on color, and
  33. --wrap them with a variable
  34. local Rmon, Gmon, Bmon, Pmon = peripheral.find("monitor", function(name, object) return object.isColor() end)
  35.  
  36. --We'll also get the dimensions of the monitors
  37. --and store them (we'll assume all monitors are
  38. --the same number of columns and rows)
  39. local monRows, monColumns
  40.  
  41. --A useful function for centering text
  42. --on a monitor from the ComputerCraft forums
  43. function centerPrint(text, y, mon)
  44.   local w, h = mon.getSize() or term.getSize()
  45.   if mon then
  46.     mon.setCursorPos( math.ceil( w/2 - #text/2 + 1), y )
  47.     mon.write( text)
  48.   else
  49.     mon.setCursorPos( math.ceil( w/2 - #text/2 ), y )
  50.     term.write( text )
  51.   end
  52. end
  53.  
  54. --A function that wraps text on monitor
  55. --and calls the center function
  56. function printCenterWrap( mon, text, size )
  57.   --size is an optional parameter,
  58.   --we default to 0.5
  59.   if size == nil then size = 0.5 end
  60.  
  61.   if size > 0 then mon.setTextScale(size) end
  62.  
  63.   local MaxCols, MaxRows = mon.getSize()
  64.   local stBuffer = text
  65.   local rowCount = 1
  66.  
  67.   if debug then print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows) end
  68.   while string.len(stBuffer) > 0 and rowCount <= MaxRows do
  69.     if debug then
  70.       print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")")
  71.       t=read() --pause to debug
  72.     end
  73.  
  74.     --find if there's a space character
  75.     local space = string.find(stBuffer, " ")
  76.  
  77.     if space == nil or space >= MaxCols then
  78.       if debug then print("found no space before EOL") end
  79.       --This is the easiest case: we didn't
  80.       --find a space before the End-of-Line MaxCols
  81.       --so just print a substring up to EOL
  82.       --For Left Justify: mon.setCursorPos(1,rowCount)
  83.       if debug then print("SetCursorPos(1,"..rowCount..")") end
  84.       local wl = string.sub(stBuffer,1,MaxCols)
  85.       --mon.write(wl) --center this later
  86.       centerPrint(wl,rowCount,mon)
  87.       if debug then print("Wrote:"..wl) end
  88.       if (MaxCols + 1) > (string.len(stBuffer)) then
  89.         --we're don't processing
  90.         stBuffer = ""
  91.         if debug then print("Set Buffer to EOL") end
  92.       else
  93.         stBuffer = string.sub(stBuffer, MaxCols + 1)
  94.         if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
  95.       end
  96.     else
  97.       if debug then print("found space at: "..space) end
  98.       local sentence = ""
  99.       local nextspace
  100.       --loop to find the next space in the buffer
  101.       local word = string.sub(stBuffer,1,space)
  102.       if debug then print("Word: "..word) end
  103.       repeat        
  104.         sentence = sentence..word
  105.         if debug then
  106.           print("Sentence: "..sentence.." ("..string.len(sentence)..")")
  107.           s=read() --pause to debug
  108.         end
  109.         --test that we don't go past the end
  110.         if (string.len(word)+1) > string.len(stBuffer) then
  111.           stBuffer = ""
  112.         else
  113.           stBuffer = string.sub(stBuffer,string.len(word)+1)
  114.           if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
  115.         end
  116.        
  117.         nextspace = string.find(stBuffer, " ")
  118.         if nextspace == nil then
  119.           if debug then print("nextspace: nil") end
  120.         else
  121.           if debug then print("nextspace: "..nextspace) end
  122.         end
  123.        
  124.         word = string.sub(stBuffer,1,nextspace) --this should be checking for nil!
  125.         if debug then print("nextword: "..word.." ("..string.len(word)..")") end
  126.        
  127.       until nextspace == nil or (string.len(sentence)+nextspace) > MaxCols
  128.      
  129.       --LEFT JUSTIFY: mon.setCursorPos(1, rowCount)
  130.       if debug then print("SetCursorPos(1, "..rowCount..")") end
  131.       if nextspace == nil then
  132.         --mon.write(sentence..word)
  133.         centerPrint(sentence..word,rowCount,mon)
  134.         if debug then print("Wrote sen+word:"..sentence..word.." ("..#sentence+#word..")") end
  135.         --check that we don't go past the end
  136.         if (string.len(word)+1) > string.len(stBuffer) then
  137.             stBuffer = ""
  138.             if debug then print("Cleared the buffer") end
  139.         else
  140.           stBuffer = string.sub(stBuffer,string.len(word)+1)
  141.           if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
  142.         end
  143.       else
  144.         --mon.write(sentence)
  145.         centerPrint(sentence,rowCount,mon)
  146.         if debug then print("Wrote sen:"..sentence.." ("..#sentence..")") end
  147.       end      
  148.      
  149.     end --of finding a space
  150.    
  151.     --increment row counter
  152.     rowCount = rowCount + 1
  153.   end --while for each row
  154.  
  155. end
  156.  
  157.  
  158. --Have a Function to clear monitors where you
  159. --Input/Parameter the monitor object/variable
  160. --and the colors.color you want it to be
  161. function clearMonitor(mon, xcolor)
  162.   mon.clear()
  163.   mon.setTextScale(0.5)
  164.   mon.setCursorPos(1,1)
  165.   mon.setBackgroundColor(xcolor)
  166.   mon.clear()
  167.   mon.setCursorPos(1,1)
  168.   --printCenterWrap(mon, "Welcome to Beyond Minecraft 101 Class!", 3)
  169. end
  170.  
  171. --This function will display text on all monitiors at once
  172. function displayOnAll(text, size)
  173.   if size == nil then size = 0.5 end
  174.   if Rmon then
  175.     clearMonitor(Rmon, colors.red)
  176.     printCenterWrap(Rmon, text, size)
  177.   end
  178.   if Gmon then
  179.     clearMonitor(Gmon, colors.green)
  180.     printCenterWrap(Gmon, text, size)
  181.   end
  182.   if Bmon then
  183.     clearMonitor(Bmon, colors.blue)
  184.     printCenterWrap(Bmon, text, size)
  185.   end
  186.   if Pmon then
  187.     clearMonitor(Pmon, colors.purple)
  188.     printCenterWrap(Pmon, text, size)
  189.   end
  190. end
  191.  
  192. --This function sets up the initial settings
  193. --for the monitors with their colors
  194. --and a blank screen displayed
  195. function initMonitors()
  196.   --Determine which of the 4 monitors we have
  197.   --on our network and set them up accordingly
  198.   if Rmon then
  199.     clearMonitor(Rmon, colors.red)
  200.     print("Connected Red Monitor")
  201.   end
  202.   if Gmon then
  203.     clearMonitor(Gmon, colors.green)
  204.     print("Connected Green Monitor")
  205.   end
  206.   if Bmon then
  207.     clearMonitor(Bmon, colors.blue)
  208.     print("Connected Blue Monitor")
  209.   end
  210.   if Pmon then
  211.     clearMonitor(Pmon, colors.purple)
  212.     print("Connected Purple Monitor")
  213.   end
  214. end
  215.  
  216.  
  217. --This is our main control function
  218. --to run the program
  219. function main()
  220.   initMonitors()
  221.   if GLOBALtext ~= "test" then
  222.     displayOnAll(GLOBALtext, 3)
  223.   else
  224.     displayOnAll("Welcome to the Beyond Minecraft 101 Class!", 3)
  225.   end
  226.  
  227. end
  228.  
  229. --Now kick-off our main program:
  230. main()
Advertisement
Add Comment
Please, Sign In to add comment