Search code examples
ncursesgnu-screenterminfotermcap

Screen truecolor Ubuntu 18.04 broke


One of the few pieces of software that I compile myself is screen. I've found it tends to support some modern features of terminals better than the version in the repositories. I've been doing this for some time now without too much of an issue. However, on upgrade to 18.04, (true)colors seem to have broken.

I run this bit of code to print a rainbow, which still works in a bare konsole:

awk 'BEGIN{                                                                                                                                                                                                                                                                                                     
    s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;                                                                                                                                                                                                                                                                                  
    for (colnum = 0; colnum<77; colnum++) {                                                                                                                                                                                                                                                                                  
        r = 255-(colnum*255/76);                                                                                                                                                                                                                                                                                             
        g = (colnum*510/76);                                                                                                                                                                                                                                                                                                 
        b = (colnum*255/76);
        if (g>255) g = 510-g;
        printf "\033[48;2;%d;%d;%dm", r,g,b;
        printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
        printf "%s\033[0m", substr(s,colnum+1,1);
    }
    printf "\n";
}'

However, when I start screen, the colors are off. It doesn't appear to have fallen back to 256 colors, but rather, the color values are wrong.

I saw this behavior before any recompilation of screen. After seeing the issue, I recompiled and even nuked my git repo and re-cloned and recompiled to try to get things working, to no avail.

Thoughts on where this has gone wrong? I'm not even sure which library might be responsible for this, or how it might work with konsole but not screen.

The good colors look like this: good colors

The bad colors look like this: enter image description here


Solution

  • Someone reminded me about this: it is a bug in screen (see source-repo):

    tputs(tparm("\E[48;2;%d;%d;%dm", _r, _g, _b), 1, DoAddChar);
    

    The problem is that it is using tparm to process 3 parameters, using termcap syntax. But termcap can only represent 2 parameters. To do this in terminfo the developer should have done something like this:

    tputs(tparm("\E[48;2;%p1%d;%p2%d;%p3%dm", _r, _g, _b), 1, DoAddChar);
    

    One of ncurses' bug-fixes during 2017 made that slice of code obsolete:

        + improve _nc_tparm_analyze, using that to extend the checks made by
          tic for reporting inconsistencies between the expected number of
          parameters for a capability and the actual.
    

    GNU screen, by the way, is a termcap application, and mixing in terminfo calls (such as tparm, which is not a termcap feature) makes it less portable than one might wish. For formatting output, termcap provides only tgoto, which uses exactly two parameters.

    Followup: the suggested improvement was applied 2018/11/18, after some discussion in this bug report.