Skip to content

Console background color has strange behavior when changed from a program #8144

Closed

Description

Environment

Windows build number: Microsoft Windows [Versión 10.0.18363.1139]
Windows Terminal version (if applicable): 1.3.2651.0

Any other software?

Steps to reproduce

Take a program that changes the console background using Win32 API calls like the following, and execute it several times:

#include "windows.h"

void WriteToConsole( HANDLE handle, const char* text )
{
    WriteConsole( handle, text, (DWORD) strlen(text), NULL, NULL );
}

void main( int argc, const char* argv[] )
{
    HANDLE handle =  GetStdHandle( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
    GetConsoleScreenBufferInfo( handle, &consoleInfo );


    WriteToConsole( handle, "0: Default color\n" );
    SetConsoleTextAttribute( handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN );
    WriteToConsole( handle, "1: Blue over brown\n" );
    WriteToConsole( handle, "2: Blue over brown\n" );
    SetConsoleTextAttribute( handle, FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN );
    WriteToConsole( handle, "3: Red over cyan\n" );
    WriteToConsole( handle, "4: Red over cyan\n" );
    WriteToConsole( handle, "5: Red over cyan\n" );

    SetConsoleTextAttribute( handle, consoleInfo.wAttributes );
}

Expected behavior

When a background color is set and then a text shorter than the console width is written followed by a newline, the remaining background color until the right margin should be transparent (showing the underlying console's background - black by default or whatever background color the user has configured), as it happens in cmd consoles; or even filling with the same color that was just set previously from the app could be acceptable.

image

Actual behavior

The wrong behavior described thereafter does not happen all the time, the first time that a new console is opened or after executing the cls command the behavior is right, but after a few executions (less than 10) the wrong behavior begins.

The wrong behavior consists in that after a background color change the next time that a newline is written , the remaining background color until the right margin is filled with the background color that was set previous to the last color change (n-1), and then after this it behaves normally (like cmd, not filled with a solid color).

When the program execution finishes, the background color of the last empty line is drawn with the last color that was set, but this is inconsistent with the behavior of the previous lines (it should be transparent).

image

Activity

  1. ghost added
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Nov 3, 2020
  2. ghost added
    Needs-Tag-FixDoesn't match tag requirements
    on Nov 3, 2020
  3. zadjii-msft commented on Nov 3, 2020

    @zadjii-msft
    Member

    Hmm, so this isn't #3848, because this doesn't have to do with resizing.

    This does sound awfully a lot like #7360, which was by design, but that was for attributes written by VT sequences. Since you're using the legacy console APIs here, this probably shouldn't have changed...

    Though, I doubt that we'll be able to differentiate between these two cases (writing this test with VT vs Win32 APIs). Even if conhost could differentiate, I'd bet that conpty is gonna blend them into the same VT emitted for the Terminal to use.

  4. added
    Area-OutputRelated to output processing (inserting text into buffer, retrieving buffer text, etc.)
    Issue-BugIt either shouldn't be doing this or needs an investigation.
    on Nov 3, 2020
  5. ghost removed
    Needs-Tag-FixDoesn't match tag requirements
    on Nov 3, 2020
  6. jgonzalezdr commented on Nov 3, 2020

    @jgonzalezdr
    Author

    Just in case it may help to track down the bug, I've discovered that if I resize the window, the console is redrawn with a proper behavior, filling with the active background color until the right margin. This behavior after resizing is not the same than cmd, but it is coherent (I assume that this is the intended behavior).

    Before resizing:

    image

    After resizing:

    image

  7. removed
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Nov 12, 2020
  8. zadjii-msft commented on Aug 23, 2021

    @zadjii-msft
    Member

    Note to self - this still repros in 1.11, even after #5792.

    Though, this does repro at the bottom of conhost's buffer, even without the Terminal. I think this is going to have to be by-design for the Terminal. New lines are going to get initialized with whatever the current attributes are. We could have theoretically scoped a change to "if the console doesn't have VT mode enabled, then don't initialize lines with the current attributes", but even that wouldn't work right here. We enable VT by default for apps running in the Terminal. The app would have to specifically turn VT off. I guess that means this actually was a /dup of #7360

  9. ghost closed this as completedon Aug 23, 2021
  10. ghost added
    Resolution-DuplicateThere's another issue on the tracker that's pretty much the same thing.
    on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-OutputRelated to output processing (inserting text into buffer, retrieving buffer text, etc.)Issue-BugIt either shouldn't be doing this or needs an investigation.Priority-2A description (P2)Product-TerminalThe new Windows Terminal.Resolution-By-DesignIt's supposed to be this way. Sometimes for compatibility reasons.Resolution-DuplicateThere's another issue on the tracker that's pretty much the same thing.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions