The trick is to keep track of how much of the current line you have already used up and whether the next piece of output will exceed the 80 character limit.