Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background colour sometimes ignored in Windows Terminal #8823

Closed
clinton-r opened this issue Jan 19, 2021 · 5 comments
Closed

Background colour sometimes ignored in Windows Terminal #8823

clinton-r opened this issue Jan 19, 2021 · 5 comments
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Area-VT Virtual Terminal sequence support Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Priority-3 A description (P3) Product-Terminal The new Windows Terminal. Resolution-Fix-Available It's available in an Insiders build or a release

Comments

@clinton-r
Copy link

Environment

Microsoft Windows [Version 10.0.19041.746]
Windows Terminal Version: 1.4.3243.0
Microsoft Visual Studio Community 2019 Version 16.8.4

Steps to reproduce

Compile the test program below.
Open Windows Terminal cmd or Powershell tab. Run the program. (Might have to run it several times to see the problem.)

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

static HANDLE hStdin = NULL;
static HANDLE hStdout = NULL;

typedef struct
{
    int fgCode;
    int bgCode;
    char* colourName;

} VT_COLOUR_INFO;

void writeString(char* str)
{
#if 1
    WriteConsoleA(hStdout, str, strlen(str), NULL, NULL);
#else
    printf(str);
#endif
}

int main(void)
{
    char buf[1000];
    DWORD mode;

    // Get handles for stdin and stdout
    hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == INVALID_HANDLE_VALUE || hStdin == NULL)
        return 1;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdout == INVALID_HANDLE_VALUE || hStdout == NULL)
        return 1;

    // Enable escape sequences
    if (!GetConsoleMode(hStdout, &mode))
        return 1;
    if (!SetConsoleMode(hStdout, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING))
        return 1;

    if (!GetConsoleMode(hStdin, &mode))
        return 1;
    if (!SetConsoleMode(hStdin, mode | ENABLE_VIRTUAL_TERMINAL_INPUT))
        return 1;

    // TEST OUTPUT

    // SGR colour codes
    VT_COLOUR_INFO colourLookup[] =
    {
        { 30, 40, "black" },
        { 31, 41, "red" },
        { 32, 42, "green" },
        { 33, 43, "yellow" },
        { 34, 44, "blue" },
        { 35, 45, "magenta" },
        { 36, 46, "cyan" },
        { 37, 47, "white" },

        { 90, 100, "bright black" },
        { 91, 101, "bright red" },
        { 92, 102, "bright green" },
        { 93, 103, "bright yellow" },
        { 94, 104, "bright blue" },
        { 95, 105, "bright magenta" },
        { 96, 106, "bright cyan" },
        { 97, 107, "bright white" },
    };

    writeString("normal text\r\n");

    for (int bg = 0; bg < (sizeof(colourLookup) / sizeof(colourLookup[0])); bg += 1)
    {
        sprintf_s(buf, sizeof(buf), "\x1b[%dm", colourLookup[bg].bgCode); // set background colour
        writeString(buf);
        for (int fg = 0; fg < (sizeof(colourLookup) / sizeof(colourLookup[0])); fg += 1)
        {
            writeString("\x1b[2K"); // erase line (set line to background colour)
            sprintf_s(buf, sizeof(buf), "\x1b[%dm", colourLookup[fg].fgCode); // set foreground colour
            writeString(buf);
            sprintf_s(buf, sizeof(buf), "%16s on %16s ",
                colourLookup[fg].colourName, colourLookup[bg].colourName);
            writeString(buf);
            writeString("\x1b[7mnegative ");    // SGR negative on
            writeString("\x1b[27m");            // SGR negative off
            writeString("\x1b[4munderline ");   // SGR underline on
            writeString("\x1b[24m\r\n");        // SGR underline off
        }
    }

    writeString("\x1b[0m"); // SGR defaults
    writeString("\x1b[2K"); // erase line
    writeString("\r\nnormal text\r\n");

    if (!SetConsoleMode(hStdin, mode))
        return 1;

    return 0;
}

Expected behavior

The test program uses SGR VT sequences to show all the different foreground and background colours, as well as negative and underlined text.

Actual behavior

For each line, the program first erases the line before printing text on it, to set the entire line to the background colour. It appears that sometimes black is used instead of the background colour. Also, spaces are sometimes printed as black instead of the background colour as well. Here is an example with both line and a space:
bgcolour

Notes

Doesn't happen in conhost.

Something similar happens when the terminal scrolls down - usually the new blank line gets the background colour as it should, but occasionally it gets black instead. (Try commenting out the first line in the for loop.)

Thanks

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jan 19, 2021
@zadjii-msft
Copy link
Member

Weird. I don't think this is #3848, though it does seem similar.

@zadjii-msft zadjii-msft added Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Area-VT Virtual Terminal sequence support Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-3 A description (P3) Product-Terminal The new Windows Terminal. labels Jan 19, 2021
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Jan 19, 2021
@zadjii-msft zadjii-msft added this to the Terminal v2.0 milestone Jan 19, 2021
@j4james
Copy link
Collaborator

j4james commented Jan 19, 2021

This looks to me like a problem in the conpty space optimization - on some lines it's just not sending through the ESC [ K to clear the line. It's sensitive to timing, so by inserting sleeps in different parts of the test code, you can affect how often the problem shows up (or even prevent it showing up).

Issue #5430 might be related.

@DHowett DHowett removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Jan 26, 2021
@zadjii-msft zadjii-msft modified the milestones: Terminal v2.0, 22H2 Jan 4, 2022
@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 1, 2022
@zadjii-msft
Copy link
Member

We believe this was fixed in Terminal v1.13, in #11982. @clinton-r would you mind confirming?

@clinton-r
Copy link
Author

We believe this was fixed in Terminal v1.13, in #11982. @clinton-r would you mind confirming?

Hi @zadjii-msft , I just tried it again with version 1.13.10395.0 and the problem is gone. Thanks!

I had also mentioned this in the Notes section:

Something similar happens when the terminal scrolls down - usually the new blank line gets the background colour as it should, but occasionally it gets black instead. (Try commenting out the first line in the for loop.)

(Note I meant the first line of the inner for loop.)

That is still the case, but I think it makes sense for it to be behaving like this - I assume it colours the full line when scrolling down, before I set the new colours and write text. Here's what it looks like:

scroll_bg_colour

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Tag-Fix Doesn't match tag requirements and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Mar 2, 2022
@zadjii-msft zadjii-msft modified the milestones: 22H2, Terminal v1.13 Mar 2, 2022
@zadjii-msft zadjii-msft added the Resolution-Fix-Available It's available in an Insiders build or a release label Mar 2, 2022
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Mar 2, 2022
@zadjii-msft
Copy link
Member

Yep, that's correct. That's more in line with what other Terminal emulators do. Thanks for confirming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Area-VT Virtual Terminal sequence support Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Priority-3 A description (P3) Product-Terminal The new Windows Terminal. Resolution-Fix-Available It's available in an Insiders build or a release
Projects
None yet
Development

No branches or pull requests

4 participants