--This is the classroom monitor setup routine --written by Matt Kelliher -- -- Useage: [-d] [-A|-R|-G|-B|-P] -- If you would like to see debugging comments on the terminal then pass in the -d -- option when you run this program before your text local tArgs = { ... } assert( #tArgs >= 1, "Useage: [-d] " ) --Parse the flags: local param1 = tArgs[1] local param2 --optional local param3 --optional if #tArgs >= 2 then param2 = tArgs[2] end if #tArgs >= 3 then param3 = tArgs[3] end --Now figure out if the debug flag was set local debug = false local GLOBALtext = "" if param1 == "-d" then debug = true assert( #tArgs >= 2, "Useage: [-d] " ) GLOBALtext = param2 else GLOBALtext = param1 end --First: we assume there are 4 equal-sized --monitors in the room connected to the network --we'll find them on the network, --give them names based on color, and --wrap them with a variable local Rmon, Gmon, Bmon, Pmon = peripheral.find("monitor", function(name, object) return object.isColor() end) --We'll also get the dimensions of the monitors --and store them (we'll assume all monitors are --the same number of columns and rows) local monRows, monColumns --A useful function for centering text --on a monitor from the ComputerCraft forums function centerPrint(text, y, mon) local w, h = mon.getSize() or term.getSize() if mon then mon.setCursorPos( math.ceil( w/2 - #text/2 + 1), y ) mon.write( text) else mon.setCursorPos( math.ceil( w/2 - #text/2 ), y ) term.write( text ) end end --A function that wraps text on monitor --and calls the center function function printCenterWrap( mon, text, size ) --size is an optional parameter, --we default to 0.5 if size == nil then size = 0.5 end if size > 0 then mon.setTextScale(size) end local MaxCols, MaxRows = mon.getSize() local stBuffer = text local rowCount = 1 if debug then print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows) end while string.len(stBuffer) > 0 and rowCount <= MaxRows do if debug then print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")") t=read() --pause to debug end --find if there's a space character local space = string.find(stBuffer, " ") if space == nil or space >= MaxCols then if debug then print("found no space before EOL") end --This is the easiest case: we didn't --find a space before the End-of-Line MaxCols --so just print a substring up to EOL --For Left Justify: mon.setCursorPos(1,rowCount) if debug then print("SetCursorPos(1,"..rowCount..")") end local wl = string.sub(stBuffer,1,MaxCols) --mon.write(wl) --center this later centerPrint(wl,rowCount,mon) if debug then print("Wrote:"..wl) end if (MaxCols + 1) > (string.len(stBuffer)) then --we're don't processing stBuffer = "" if debug then print("Set Buffer to EOL") end else stBuffer = string.sub(stBuffer, MaxCols + 1) if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end end else if debug then print("found space at: "..space) end local sentence = "" local nextspace --loop to find the next space in the buffer local word = string.sub(stBuffer,1,space) if debug then print("Word: "..word) end repeat sentence = sentence..word if debug then print("Sentence: "..sentence.." ("..string.len(sentence)..")") s=read() --pause to debug end --test that we don't go past the end if (string.len(word)+1) > string.len(stBuffer) then stBuffer = "" else stBuffer = string.sub(stBuffer,string.len(word)+1) if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end end nextspace = string.find(stBuffer, " ") if nextspace == nil then if debug then print("nextspace: nil") end else if debug then print("nextspace: "..nextspace) end end word = string.sub(stBuffer,1,nextspace) --this should be checking for nil! if debug then print("nextword: "..word.." ("..string.len(word)..")") end until nextspace == nil or (string.len(sentence)+nextspace) > MaxCols --LEFT JUSTIFY: mon.setCursorPos(1, rowCount) if debug then print("SetCursorPos(1, "..rowCount..")") end if nextspace == nil then --mon.write(sentence..word) centerPrint(sentence..word,rowCount,mon) if debug then print("Wrote sen+word:"..sentence..word.." ("..#sentence+#word..")") end --check that we don't go past the end if (string.len(word)+1) > string.len(stBuffer) then stBuffer = "" if debug then print("Cleared the buffer") end else stBuffer = string.sub(stBuffer,string.len(word)+1) if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end end else --mon.write(sentence) centerPrint(sentence,rowCount,mon) if debug then print("Wrote sen:"..sentence.." ("..#sentence..")") end end end --of finding a space --increment row counter rowCount = rowCount + 1 end --while for each row end --Have a Function to clear monitors where you --Input/Parameter the monitor object/variable --and the colors.color you want it to be function clearMonitor(mon, xcolor) mon.clear() mon.setTextScale(0.5) mon.setCursorPos(1,1) mon.setBackgroundColor(xcolor) mon.clear() mon.setCursorPos(1,1) --printCenterWrap(mon, "Welcome to Beyond Minecraft 101 Class!", 3) end --This function will display text on all monitiors at once function displayOnAll(text, size) if size == nil then size = 0.5 end if Rmon then clearMonitor(Rmon, colors.red) printCenterWrap(Rmon, text, size) end if Gmon then clearMonitor(Gmon, colors.green) printCenterWrap(Gmon, text, size) end if Bmon then clearMonitor(Bmon, colors.blue) printCenterWrap(Bmon, text, size) end if Pmon then clearMonitor(Pmon, colors.purple) printCenterWrap(Pmon, text, size) end end --This function sets up the initial settings --for the monitors with their colors --and a blank screen displayed function initMonitors() --Determine which of the 4 monitors we have --on our network and set them up accordingly if Rmon then clearMonitor(Rmon, colors.red) print("Connected Red Monitor") end if Gmon then clearMonitor(Gmon, colors.green) print("Connected Green Monitor") end if Bmon then clearMonitor(Bmon, colors.blue) print("Connected Blue Monitor") end if Pmon then clearMonitor(Pmon, colors.purple) print("Connected Purple Monitor") end end --This is our main control function --to run the program function main() initMonitors() if GLOBALtext ~= "test" then displayOnAll(GLOBALtext, 3) else displayOnAll("Welcome to the Beyond Minecraft 101 Class!", 3) end end --Now kick-off our main program: main()