Fixed a bug where a multi-word highlight keyword (such as "oil lamp") crossed a line wrap boundry, and was thus missed. All this would be unnecessary if sprintf() understood pinkfish codes:
varargs string format_desc(string raw, int terminal_width, int leading_indent_width, int inner_indent_width, string *highlight, string highlight_color) {
string result = "";
string *paragraphs = rexplode(raw, "\n");
string leading_indent = "";
string inner_indent = "";
if(undefinedp(terminal_width) || terminal_width < 10) {
object tp = this_player();
terminal_width = 75;
if(!undefinedp(tp)) {
int sw = tp->GetScreen()[0];
if(!undefinedp(sw) && sw > 0) {
terminal_width = sw;
}
}
}
if(!undefinedp(leading_indent_width) && leading_indent_width > 0) {
leading_indent = sprintf("%-*.*s", leading_indent_width, leading_indent_width, " ");
}
if(!undefinedp(inner_indent_width) && inner_indent_width > 0) {
inner_indent = sprintf("%-*.*s", inner_indent_width, inner_indent_width, " ");
}
for(int i = 0; i < sizeof(paragraphs); i++) {
string p = paragraphs[i];
result += (i != 0 ? "" : "");
result += leading_indent + implode(explode(sprintf("%-=*s\n", terminal_width, p), "\n"), "\n" + inner_indent);
result += "\n";
}
if(!undefinedp(highlight)) {
if(undefinedp(highlight_color)) {
highlight_color = "%^BOLD%^%^YELLOW%^";
}
foreach(string h in highlight) {
result = replace_string(result, h, highlight_color + h + "%^RESET%^", 1);
if(strsrch(h, " ") != -1) {
string *words = explode(h, " ");
for(int i = 0; i < sizeof(words); i++) {
string t = replace_string(h, " ", "\n", i+1, i+1);
result = replace_string(result, t, highlight_color + t + "%^RESET%^", 1);
}
}
}
}
return result;
}