fluffos icon indicating copy to clipboard operation
fluffos copied to clipboard

Sprintf can't identify the ANSI color code length and wrap string wrong length

Open seikichin opened this issue 3 years ago • 1 comments

varargs string wrap(string str, int len, int left) { if(!sizeof(str)) return str;

if (len < 1) len = 75;
if ((left >= len) || (left < 0)) left = 0;
str = sprintf("%*s%-=*s\n",left,"",len-left,str);
if (str[<2..] == "\n\n")
    return str [0..<2];
else
    return str;

} when wrap a string contains ansi code it won't work correctly

seikichin avatar Sep 10 '21 00:09 seikichin

I did a test.

varargs string do_wrap(string str, int len, int left)
{
    if(!sizeof(str))
    return str;

    if (len < 1) len = 75;
    if ((left >= len) || (left < 0)) left = 0;

    str = sprintf("%*s%-=*s\n",left,"",len-left,str);

    if (str[<2..] == "\n\n") return str [0..<2];
    else return str;
}

void wrapitup()
{
    string mess =
    "\e[32mThis is some text. "
    "\e[33mThis is some text. "
    "\e[34mThis is some text. "
    "\e[35mThis is some text. "
    "\e[36mThis is some text. "
    "\e[37mThis is some text. "
    "\e[0m" ;

    printf("\n%s\n", do_wrap(mess, 75, 4)) ;
}

These are the results visually: image

Here are the results from my network log in Beip

CR LF     ESC [32mThis CR LF     is some text. CR LF     ESC [33mThis CR LF     is some text. CR LF     ESC [34mThis CR LF     is some text. CR LF     ESC [35mThis is CR LF     some text. ESC [36mThis is some CR LF     text. ESC [37mThis is some text. ESC [0m CR LF

gesslar avatar Oct 02 '21 04:10 gesslar