0
votes

I have a terminal program using ncurses that periodically changes the foreground and background colors. Unfortunately, the cursor color won't change until the terminal scrolls. What is ncurses doing during a scroll that causes xterm and other ANSI-compliant terminal emulators to change the cursor color?

I have this in my .Xresources:

xterm*cursorColor:      *XtDefaultForeground

I found this: How do I change the cursor color in ncurses forms? showing me the escape sequence to change the cursor color, but it doesn't take into account the fact that the cursor is actually brighter because it's in bold. I can multiply the values I get from color_content() with 1.48, but this strikes me as inviting trouble later on.
For some reason I can't figure out, just dividing the bold color by the non-bold color (values obtained with a color picker) didn't work. The resulting color was brighter, but didn't match the target value, so I found 1.48 by trial and error.

If I'm stuck with using escape sequences, that's okay, but I'd like to know of a cleaner way to properly bold a color.

1

1 Answers

0
votes

ncurses doesn't know anything about cursor-color: there's no commonality across terminals to assume that one can set (or control) the cursor color.

The xterm sequence indicated may/may not be supported by other terminals. It happens to modify the value which would be initialized by the cursorColor resource:

Specifies the color to use for the text cursor. The default is "XtDefaultForeground". By default, xterm attempts to keep this color from being the same as the background color, since it draws the cursor by filling the background of a text cell. The same restriction applies to control sequences which may change this color.

Setting this resource overrides most of xterm's adjustments to cursor color. It will still use reverse-video to disallow some cases, such as a black cursor on a black background.

That is, by default, xterm attempts to keep the cursor visible by adjusting its color based on the cell where the cursor currently is. As you move the cursor around in a text field, those colors may be different, causing xterm to change the cursor color.

Now... there are some special cases (see ShowCursor and HideCursor in the xterm source), but basically it tries to use the current cell's foreground/background colors (reversed). In ncurses, you can get the cell's current colors by using one of the inch or in_wch variations. The returned data includes a color pair, and you can extract the color values for foreground/background from that using pair_content. Reversing those to make a new color pair would be close (but you're already aware that bold can interact with the rendered color).