Hey folks,
I've been digging into a problem with aligning coloured text, and am running short on ideas. I'm working with a modified SMAUG/SWR codebase, but I believe this is primarily related to standard functions.
The problem : using '%-40s' (or any string format/length token) will consider the colour tokens as part of the '40', and the output will consequently be 2 shorter than I want.
The code:
ch_printf( ch, "&zName: &c%-40s &zVnum: &c%d\n\r", (location->name), location->vnum );
ch_printf( ch, "&zName: &c%-40s &zVnum: &c%d\n\r", showclr(location->name), location->vnum );
ch_printf( ch, "&zName: &c%-40s &zVnum: &c%d\n\r", stripclr(location->name), location->vnum );
ch_printf( ch, "&zName: &c%-40s | strlen: %d | strlen_color: %d\n\r", location->name, strlen(location->name), strlen_color(location->name));
The output:
Name: The Kuragame Waterfall Vnum: 4102
Name: &pThe Kuragame Waterfall Vnum: 4102
Name: The Kuragame Waterfall Vnum: 4102
Name: The Kuragame Waterfall | strlen: 24 | strlen_colour: 22
Technically, there are 24 characters in the string provided, so it's not entirely incorrect to be adding only 16 spaces to make it to the 40 requested. However, I would like it to disregard the colour tags when counting characters.
My best guess is that it's because ch_printf() has no idea that I'm going to swap colour tokens for ANSI code, so when it calculates string length, it's doing the best it can without that information.
ch_printf:
void ch_printf( CHAR_DATA *ch, char *fmt, ... )
{
char buf[MAX_STRING_LENGTH*2];
va_list args;
va_start( args, fmt );
vsnprintf( buf, MAX_STRING_LENGTH*2, fmt, args );
va_end( args );
send_to_char_color( buf, ch );
}
I'm at a bit of a loss as to how other MUDs handle this; perhaps I should have opened with the question - do they?
Any advice is appreciated!
-Marowi |