Ben S
Ben S

Reputation: 1555

Is it possible to change the foreground and background colour at the same time with terminfo?

I'm writing a console program that needs to output styled text. Right now I'm using fixed ANSI escape codes for foreground and background colours, bold, and underline. For example, I can write \033[35m to change the foreground colour to purple.

The benefit of writing the codes myself is that I can chain them. I can change the background colour and the foreground colour with one command, which saves precious bytes:

\033[48;5;200;38;5;100m

I'd like to switch to using the terminfo library to make my program work on other types of terminals. However, I can't find a way to update multiple colours at once - I want to set bold and underline and the colours with only one \033 and only one m.

There are the following commands listed in the standard, but not only do none of these seem to do what I want, I can't actually find what the first and second sets of video attributes actually are:

Is there a way to get what I want using only terminfo commands? Thanks in advance.

Upvotes: 1

Views: 1742

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54475

No - terminfo does not do this:

  • It lists capabilities which are likely to be provided on many different terminals
  • The standard capabilities were defined ... a while back. Same for termcap, of course.
  • Color and other attributes are (with the most common terminals now) something that you might assume could be combined arbitrarily, but that is fairly recent — nothing guaranteed
  • So the capabilities are in small pieces (update the foreground color, update the background color).
  • The one exception is sgr, which can set up to 9 video attributes at a time. None of those are color.

If you happen to be using ncurses, you could define your own capabilities, using tic -x, and use your own interpretation for those (see for example ncurses user-definable capabilities in the terminal database). As long as their syntax matches the other terminfo strings, you could then use tparm to fill in the numbers (a little better than printf).

Upvotes: 1

Related Questions