ncurses - printw on characters with attributes.

This is a discussion on ncurses - printw on characters with attributes. within the C Programming forums, part of the General Programming Boards category; I'm trying to output a formatted string with one of the characters set to bold. This is my attempt, which ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403

    ncurses - printw on characters with attributes.

    I'm trying to output a formatted string with one of the characters set to bold. This is my attempt, which doesn't output '1' in bold:

    Code:
    printw("%c. Generate packed value\n  ", '1' || A_BOLD);
    The reasoning for this came from the fact, the documentation notes that printw calls have their output done through the addch routine, not printf. I believe the reason why it fails is perhaps because it firsts formats the string and only then outputs.

    Other than dividing the output with one call to addch and another to addstr, is there any way I can do this?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Just noticed a small typo. I'm actually using:

    Code:
    printw("%c. Generate packed value\n  ", '1' | A_BOLD);
    Not the logical or (||).

    I just don't understand why isn't '1' output bold, considering that printw calls addch.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Having not used ncurses in over 10 years, I don't know exactly, but wouldn't '1' | A_BOLD morph the char into something completely different? And, if A_BOLD is 1, 16, or 32 then the value would not change. . .

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Quote Originally Posted by Kennedy
    Having not used ncurses in over 10 years, I don't know exactly, but wouldn't '1' | A_BOLD morph the char into something completely different?
    Yes it would. A chtype (a typedef for unsigned long). The answer is partly there.

    You eventually reminded me to check the official documentation that comes with PDCurses, instead of the website I was looking at. And because of that alone, thanks

    First, it is not true printw will call addch. It will call addstr. And addstr will only handle const char*, which means no embed attributes.

    Second, and this one is my fault for not having realized the obvious, I cannot expect to construct an array of chtypes from a null-terminated string without some kind of help in the form of a function.

    Then it is just a matter of using "strings" (read arrays) of chtypes with the addchstr family of functions, which do accept const chtype*.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Second, and this one is my fault for not having realized the obvious, I cannot expect to construct an array of chtypes from a null-terminated string without some kind of help in the form of a function.
    Why not? printfw has to perform character-at-a-time parsing of the string anyhow. There's no compelling reason for printw to work by calling addstr instead of calling addch. I was surprised to see this is how printw works too.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    First, it wouldn't matter if it called addch. Since printw expects an 8 bit charater string, addch wouldn't still output with embed attributes.

    As a matter of a fact printw does output through addch. But it does it because it calls addstr, which in turn calls addch.

    I think it does that to avoid reinventing the wheel.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Just took a look at ncurses source code. printw sends the whole thing to the vsprintf() standard function for parsing. It doesn't do it itself.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. MURK - a small preview
    By Mario F. in forum Game Programming
    Replies: 27
    Last Post: 12-18-2006, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21