Method code for $string.wrap_lines()

[Turn on line numbering]
arg str, len, @stuff;
var output, cutoff, firstline, prefix, plen;

// takes string and wraps it by words, compared to length, returns a list. 
[(prefix ?= ""), (firstline ?= 0)] = stuff;
output = [];
if (firstline)
    str = prefix + str;
plen = strlen(prefix);
while (strlen(str) > len) {
    cutoff = stridx(substr(str, 1, len), " ", -1);
    if (cutoff <= plen) {
        output += [substr(str, 1, len)];
        str = prefix + substr(str, len + 1);
    } else {
        output += [substr(str, 1, cutoff - 1)];
        str = prefix + substr(str, cutoff + 1);
    }
}
return output + [str];

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

Tlon