• printf and color codes

    From Nightfox@VERT/DIGDIST to Digital Man on Sun Jan 20 18:14:56 2013
    Hi DM -

    I'm not sure if this is a bug or is intentional, but the JavaScript printf function doesn't seem to account for color codes (or perhaps non-printable characters in general) when printing strings with a specific field length. For instance:

    var name = "Bob Smith";
    printf("%-25s", name);

    That prints "Bob Smith" in a field with 25 characters as you'd expect, but if the string has any non-printable characters in it, each non-printable character subtracts 1 from the field length when the string is printed out. CTRL-A color codes have this effect when used with printf.

    Nightfox


    ---
    þ Synchronet þ Digital Distortion BBS - digitaldistortionbbs.com
  • From Mro@VERT/BBSESINF to Nightfox on Sun Jan 20 21:43:04 2013
    Re: printf and color codes
    By: Nightfox to Digital Man on Sun Jan 20 2013 06:14 pm

    That prints "Bob Smith" in a field with 25 characters as you'd expect, but if the string has any non-printable characters in it, each non-printable character subtracts 1 from the field length when the string is printed out. CTRL-A color codes have this effect when used with printf.


    i'm pretty sure that's intentional and certainly seems like it should be.
    who's name or other variable would have color codes in it, though?
    ---
    þ Synchronet þ ::: BBSES.info - free BBS services :::
  • From Nightfox@VERT/DIGDIST to Mro on Sun Jan 20 21:50:02 2013
    Re: printf and color codes
    By: Mro to Nightfox on Sun Jan 20 2013 21:43:04

    non-printable character subtracts 1 from the field length when the
    string is printed out. CTRL-A color codes have this effect when used
    with printf.

    i'm pretty sure that's intentional and certainly seems like it should be.

    It's not what I would expect. If you specify a field length for printf(), it should fill that entire field up to that length. Otherwise, the results can be unexpected and ugly. The main uses for specifying a field length are to align text and filling a space when overwriting existing text on the screen. I would expect printf() to only count visible/printable characters in the length of a string you give it.

    who's name or other variable would have color codes in it, though?

    Anything where color is desired.. :P

    Nightfox


    ---
    þ Synchronet þ Digital Distortion BBS - digitaldistortionbbs.com
  • From MCMLXXIX@VERT/MDJ to Nightfox on Mon Jan 21 11:29:24 2013
    Re: printf and color codes
    By: Nightfox to Mro on Sun Jan 20 2013 21:50:02

    Re: printf and color codes
    By: Mro to Nightfox on Sun Jan 20 2013 21:43:04

    non-printable character subtracts 1 from the field length when the
    string is printed out. CTRL-A color codes have this effect when used
    with printf.

    i'm pretty sure that's intentional and certainly seems like it should b

    It's not what I would expect. If you specify a field length for printf(), i should fill that entire field up to that length. Otherwise, the results can unexpected and ugly. The main uses for specifying a field length are to ali text and filling a space when overwriting existing text on the screen. I wo expect printf() to only count visible/printable characters in the length of string you give it.

    who's name or other variable would have color codes in it, though?

    Anything where color is desired.. :P


    I think you're right about this.. if printf is able to handle ctrl-A codes it likely should be able to format them correctly.. you can work around it though.

    var diff = str.length - console.strlen(str);
    printf("%-*s",25+diff,str);



    ---
    þ Synchronet þ The BRoKEN BuBBLE (MDJ.ATH.CX)
  • From Nightfox@VERT/DIGDIST to MCMLXXIX on Mon Jan 21 12:36:54 2013
    I think you're right about this.. if printf is able to handle ctrl-A codes it likely should be able to format them correctly.. you can work around it though.

    var diff = str.length - console.strlen(str);
    printf("%-*s",25+diff,str);

    That's true. Workarounds are always possible, and I've found other
    workarounds for my needs as well. Just thought I'd point it out though
    because it seemed odd.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion BBS - digitaldistortionbbs.com
  • From Digital Man@VERT to Nightfox on Mon Jan 21 23:48:01 2013
    Re: printf and color codes
    By: Nightfox to Digital Man on Sun Jan 20 2013 06:14 pm

    Hi DM -

    I'm not sure if this is a bug or is intentional, but the JavaScript printf function doesn't seem to account for color codes (or perhaps non-printable characters in general) when printing strings with a specific field length. For instance:

    var name = "Bob Smith";
    printf("%-25s", name);

    That prints "Bob Smith" in a field with 25 characters as you'd expect, but if the string has any non-printable characters in it, each non-printable character subtracts 1 from the field length when the string is printed out. CTRL-A color codes have this effect when used with printf.

    Being a C programmer, that's the behavior I would expect. There's no special Ctrl-A code handling in the format/printf functions. You can use console.strlen() to calculate the number of *printable* characters in a string (excluding Ctrl-A codes) and use that to pad your console output with spaces if
    you wish.

    digital man

    Synchronet "Real Fact" #48:
    Synchronet program was named 'sbbs' instead of 'sync' to avoid conflict w/Unix. Norco, CA WX: 65.6øF, 8.0% humidity, 11 mph E wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Digital Man@VERT to Nightfox on Mon Jan 21 23:56:55 2013
    Re: printf and color codes
    By: Nightfox to Mro on Sun Jan 20 2013 09:50 pm

    Re: printf and color codes
    By: Mro to Nightfox on Sun Jan 20 2013 21:43:04

    non-printable character subtracts 1 from the field length when the
    string is printed out. CTRL-A color codes have this effect when used
    with printf.

    i'm pretty sure that's intentional and certainly seems like it should be.

    It's not what I would expect. If you specify a field length for printf(), it should fill that entire field up to that length. Otherwise, the results can be unexpected and ugly. The main uses for specifying a field length
    are to align text and filling a space when overwriting existing text on the screen. I would expect printf() to only count visible/printable characters in the length of a string you give it.

    That's not how printf (and the related formatting functions) in C work however.
    The width modifier to the %s specifier does not specify the number of "printable" characters, just the number of characters, period (which includes non-printable characters, like \a, \b, and so on).

    digital man

    Synchronet "Real Fact" #76:
    Rob Swindell still has dozens of BBS-related magazines in his possession. Norco, CA WX: 65.4øF, 8.0% humidity, 7 mph E wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Nightfox@VERT/DIGDIST to Digital Man on Tue Jan 22 07:46:06 2013
    Re: printf and color codes
    By: Digital Man to Nightfox on Mon Jan 21 2013 23:56:55

    That's not how printf (and the related formatting functions) in C work however. The width modifier to the %s specifier does not specify the number of "printable" characters, just the number of characters, period (which includes non-printable characters, like \a, \b, and so on).

    Coming from a C/C++ background, I suspected that was so. Even then, it seems somewhat odd.. But oh well, we deal with it. :)

    Nightfox


    ---
    þ Synchronet þ Digital Distortion BBS - digitaldistortionbbs.com
  • From MCMLXXIX@VERT/MDJ to Nightfox on Tue Jan 22 12:03:16 2013
    Re: printf and color codes
    By: Nightfox to Digital Man on Tue Jan 22 2013 07:46:06

    Re: printf and color codes
    By: Digital Man to Nightfox on Mon Jan 21 2013 23:56:55

    That's not how printf (and the related formatting functions) in C work however. The width modifier to the %s specifier does not specify the number of "printable" characters, just the number of characters, period (which includes non-printable characters, like \a, \b, and so on).

    Coming from a C/C++ background, I suspected that was so. Even then, it seem somewhat odd.. But oh well, we deal with it. :)


    I still think that if the function is going to handle the non-printable characters, it should account for them. If it's not going to account for them, printf() shouldn't be able to handle them (like console.write)



    ---
    þ Synchronet þ The BRoKEN BuBBLE (MDJ.ATH.CX)