Method code for $string.wrap_line()

[Turn off line numbering]
  1: arg str, len, @stuff;
  2: var output, cutoff, firstline, prefix, plen;
  3: 
  4: // takes string and wraps it by words, compared to length, breaks with 

  5: [(prefix ?= ""), (firstline ?= 0)] = stuff;
  6: output = "";
  7: if (firstline)
  8:     str = prefix + str;
  9: plen = strlen(prefix);
 10: while (strlen(str) > len) {
 11:     cutoff = stridx(substr(str, 1, len), " ", -1);
 12:     if (cutoff <= plen) {
 13:         output += "
" + substr(str, 1, len);
 14:         str = prefix + substr(str, len + 1);
 15:     } else {
 16:         output += "
" + substr(str, 1, cutoff - 1);
 17:         str = prefix + substr(str, cutoff + 1);
 18:     }
 19: }
 20: return (output ? output.subrange(3) + "
" : "") + str;

// Created 27-Mar-1995 as a part of ColdCore, see: @help Credit

Tlon