Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While this doesn't quite meet all your requirements &mdash; for one thing it doesn't handle tabs, I've used the following gem, which adds a <code>wordWrap()</code> method to Javascript <code>String</code>s, on a couple of occasions to do something similar to what you're describing &mdash; so it might be a good starting point to come up with something that also does the additional things you want.</p> <pre><code>//+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/string/wordwrap [rev. #2] // String.wordWrap(maxLength: Integer, // [breakWith: String = "\n"], // [cutType: Integer = 0]): String // // Returns an string with the extra characters/words "broken". // // maxLength maximum amount of characters per line // breakWith string that will be added whenever one is needed to // break the line // cutType 0 = words longer than "maxLength" will not be broken // 1 = words will be broken when needed // 2 = any word that trespasses the limit will be broken String.prototype.wordWrap = function(m, b, c){ var i, j, l, s, r; if(m &lt; 1) return this; for(i = -1, l = (r = this.split("\n")).length; ++i &lt; l; r[i] += s) for(s = r[i], r[i] = ""; s.length &gt; m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")) j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 &amp;&amp; m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length; return r.join("\n"); }; </code></pre> <p>I'd also like to comment that it seems to me as though, in general, you'd want to use a monospaced font if tabs are involved because the width of words would vary with the proportional font used (making the results of using of tab stops very font dependent).</p> <p><strong>Update</strong>: Here's a slightly more readable version courtesy of an online <a href="http://jsbeautifier.org" rel="nofollow">javascript&nbsp;beautifier</a>:</p> <pre><code>String.prototype.wordWrap = function(m, b, c) { var i, j, l, s, r; if (m &lt; 1) return this; for (i = -1, l = (r = this.split("\n")).length; ++i &lt; l; r[i] += s) for (s = r[i], r[i] = ""; s.length &gt; m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")) j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 &amp;&amp; m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length; return r.join("\n"); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload