Posted 28 September 2014 - 08:57 PM
                I'm making a program that keeps a list of tasks and displays them on an attached monitor to be cycled through one at a time. I'm trying to write a function that will display a single string on four lines with word wrap – that is, with lines broken up without breaking up words. First of all, I'm only learning programming with ComputerCraft, so I'm sure there are way easier ways to do this, but can anyone see what's wrong with my function? Specifically, it says that something is expecting a string and only getting a nil value.
                
            
-- Write the current task in the center of the monitor
function DrawTask()
  local firstLine, secondLine, thirdLine, fourthLine
  local endOfFirstLine, endOfSecondLine, endOfThirdLine
  local restOfTask
  monitor.setCursorPos(2, 3)
  if taskList[currentTask] ~= nil then
	monitor.write(tostring(currentTask))
	monitor.write(". ")
	if string.len(taskList[currentTask]) < 15 then
	  monitor.write(taskList[currentTask])
	else
	  firstLine = string.sub(taskList[CurrentTask], 1, 14)
	  endOfFirstLine = string.find(firstLine, " ", -1)
	  firstLine = string.sub(firstLine, 1, endOfFirstLine - 1)
	  restOfTask = string.sub(taskList[CurrentTask], endOfFirstLine + 1)
	  if string.len(restOfTask) < 18 then
		secondLine = restOfTask
		thirdLine = " "
		fourthLine = " "
	  else
		secondLine = string.sub(restOfTask, 1, 17)
		endOfSecondLine = string.find(secondLine, " ", -1)
		secondLine = string.sub(secondLine, 1, endOfSecondLine - 1)
		restOfTask = string.sub(restOfTask, endOfSecondLine + 1)  
		if string.len(restOfTask) < 18 then
		  thirdLine = restOfTask
		  fourthLine = " "
		else
		  thirdLine = string.sub(restOfTask, 1, 17)
		  endOfThirdLine = string.find(thirdLine, " ", -1)
		  thirdLine = string.sub(thirdLine, 1, endOfThirdLine - 1)
		  fourthLine = string.sub(restOfTask, endOfThirdLine + 1)  
		end
	  end
	  monitor.write(firstLine)
	  monitor.setCursorPos(2, 4)
	  monitor.write(secondLine)
	  monitor.setCursorPos(2, 5)
	  monitor.write(thirdLine)
	  monitor.setCursorPos(2, 6)
	  monitor.write(fourthLine)
	end
  else
	monitor.write("NO TASKS!")
  end
end
 
        