Lua: Line breaks in strings
<p>I've been working on a formatter that will take a long string and format it into a series of lines broken off at the word within a certain character limit. For instance, <strong>He eats the bread</strong>, broken off at every 8 characters, would return something like:</p>

<pre><code>He eats
the
bread
</code></pre>

<p>This is because "He eats" contains 7 characters, and "the bread" contains 9, so it has to break off at "the" and continue with "bread".</p>

<p>The script itself has been working wonderfully, thanks to the help of other members before. However, now I have a new challenge.</p>

<p>I'm utilizing a free-entry box for entering the series of lines. It's for a program called MUSHclient, if anyone is familiar with it. The <strong>utils.editbox</strong> opens up an editor text box, and allows free form, and returns the result as a <strong>string</strong>. For example:</p>

<pre><code>result = utils.editbox("What are you typing?")
</code></pre>

<p>saves the typed response into <code>result</code> which is a <code>string</code> type. I want to put line breaks in properly, but I can't figure out how to do that. If I <code>print(result)</code>, it returns something like the following:</p>

<pre><code>This is the result of three separate paragraphs.

As you can see, there is a line break between each paragraph.

So the result contains line breaks, or control characters.
</code></pre>

<p>Now, I've managed to extrapolate the individual lines, but not the linebreaks in between, using the following code:</p>

<pre><code>for line in result:gmatch("[^%c]+%c?") do 
  Send(note_wrap(line))
end
</code></pre>

<p>How can I extrapolate for linebreaks as well as textual content?</p>

<p><strong>Edit</strong> For reference, the <code>note_wrap</code> function is as follows:</p>

<pre><code>function note_wrap(str, limit, indent, indent1)
  indent = indent or ""
  indent1 = indent1 or indent
  limit = limit or 79
  local here = 1-#indent1
  local last_color = ''
  return indent1..str:gsub("(%s+)()(%S+)()",
    function(sp, st, word, fi)
      local delta = 0
      local color_before_current_word = last_color
      word:gsub('()@([@%a])', 
        function(pos, c)
          if c == '@' then 
            delta = delta + 1 
          elseif c == 'x' then 
            delta = delta + 5
            last_color = word:sub(pos, pos+4)
          else                 
            delta = delta + 2 
            last_color = word:sub(pos, pos+1)
          end
        end)
      here = here + delta
      if fi-here &gt; limit then
        here = st - #indent + delta
        return "\n"..indent..color_before_current_word..word
      end
    end)
end
</code></pre>

<p>The reason for this code is to be able to take <strong>@rHe eats @x123the bread</strong>, ignore the color codes (indicated by @&lt;letter> or @x&lt;digits>) and return the aforementioned result of:</p>

<pre><code>He eats
the
bread
</code></pre>

<p>but insert the color codes so it then returns:</p>

<pre><code>@rHe eats
@x123the
bread
</code></pre>

<p>If this can be modified to recognize new lines and put an actual new line in, that would be fabulous.</p>

<p><strong>Edit 2</strong> Tried Paul's solution, and it's not doing what I'm needing it to. I may not have been clear enough, so I'll try to clear it up in this edit.</p>

<p>When I type into the free form box, I want the information presented exactly as is, but formatted to break correctly AND maintain entered newlines. For example, if I write this in the free form box:</p>

<pre><code>Mary had a little lamb, little lamb, little lamb
Mary had a little lamb
It's fleece was white as snow
</code></pre>

<p>using Paul's code, and setting string to 79, I get:</p>

<pre><code>Mary had a little lamb, little lamb, little lamb

Mary had a little lamb

It's fleece was white as snow
</code></pre>

<p>And that's not what I want. I'd want it to return it just as it was written, and breaking line as necessary. So if I had a 20 character limit, it'd return:</p>

<pre><code>Mary had a little
lamb, little lamb,
little lamb
Mary had a little
lamb
It's fleece was
white as snow
</code></pre>

<p>If I added manual line breaks, I'd want it to respect those, so if I hit return twice after the first line, it'd have a line break under the first line as well. For example, if I wrote this post in the free form box, I'd want it to respect every new paragraph and every proper line break as well. I hope this clears things up.</p>