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

[windows] Terminal background colour #1487

Closed
gvanem opened this issue Sep 10, 2022 · 9 comments
Closed

[windows] Terminal background colour #1487

gvanem opened this issue Sep 10, 2022 · 9 comments
Assignees

Comments

@gvanem
Copy link

gvanem commented Sep 10, 2022

Hello folks, my first issue.

The function ColorPrintf() ass-u-mes that all users of this library have terminals with a black background colour.
I.e. using it in Microsoft's STL library and the benchmark-std_copy.exe program, the output like like this:

STL-1

Not very nice IMHO.

With my blue terminal background I'd like it to look like:
STL-2

which I fixed with:

--- a/src/colorprint.cc  2022-09-10 14:58:12
+++ b/src/colorprint.cc 2022-09-10 14:54:32
@@ -136,13 +136,16 @@
   CONSOLE_SCREEN_BUFFER_INFO buffer_info;
   GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
   const WORD old_color_attrs = buffer_info.wAttributes;
+  WORD  new_attr;

   // We need to flush the stream buffers into the console before each
   // SetConsoleTextAttribute call lest it affect the text that is already
   // printed but has not yet reached the console.
   fflush(stdout);
+  new_attr = (buffer_info.wAttributes & ~7);
+  new_attr &= ~8;    // Since 'wAttributes' could have been hi-intensity at startup.
   SetConsoleTextAttribute(stdout_handle,
-                          GetPlatformColorCode(color) | FOREGROUND_INTENSITY);
+                          new_attr | GetPlatformColorCode(color) | FOREGROUND_INTENSITY);
   vprintf(fmt, args);

   fflush(stdout);

PS. I use JPsoft's 4NT as the shell.

System

  • Win-10
  • Compiler and version: MSVC ver. 19.34.31721 for x64
@LebedevRI LebedevRI changed the title Terminal background colour [windows] Terminal background colour Sep 10, 2022
@dmah42
Copy link
Member

dmah42 commented Sep 12, 2022

could you create a pull request for this?

@gvanem
Copy link
Author

gvanem commented Sep 12, 2022

I'm not very familiar with git pull request. But my attempt: #1488

@imrichardcole
Copy link
Contributor

Taking a look

@imrichardcole
Copy link
Contributor

Confirmed issue is still present:

image

Will review the original PR and associated comments to work out a solution.

@imrichardcole
Copy link
Contributor

imrichardcole commented Sep 15, 2024

To confirm why this is happening (PR incoming) the issue can be captured like so:

image

In a blue background console, the 5th bit is lost at the point the green message is written.

There are #defines (C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um\consoleapi2.h) for all the colours supported, so can be done without any magic numbers.

@dmah42
Copy link
Member

dmah42 commented Sep 16, 2024

yeah i think we just assume the background colour is black and reset to that instead of reading the current bg colour.

@gvanem
Copy link
Author

gvanem commented Sep 16, 2024

yeah i think we just assume the background colour is black and reset to that instead of reading the current bg colour.

That's just what I wrote 2 years ago. So much for being a Member of Google.

@dmah42
Copy link
Member

dmah42 commented Sep 16, 2024

yeah i think we just assume the background colour is black and reset to that instead of reading the current bg colour.

That's just what I wrote 2 years ago. So much for being a Member of Google.

i'm not sure i understand what point you're trying to make, but it's reflecting more poorly on you than on me. as you are still active on github, did you want to finish the PR you started but never finished two years ago?

imrichardcole added a commit to imrichardcole/benchmark that referenced this issue Oct 23, 2024
imrichardcole added a commit to imrichardcole/benchmark that referenced this issue Oct 23, 2024
imrichardcole added a commit to imrichardcole/benchmark that referenced this issue Oct 23, 2024
imrichardcole added a commit to imrichardcole/benchmark that referenced this issue Oct 23, 2024
imrichardcole added a commit to imrichardcole/benchmark that referenced this issue Oct 24, 2024
dmah42 added a commit that referenced this issue Oct 24, 2024
…the terminal on windows is preserved (#1865)

Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>
@dmah42
Copy link
Member

dmah42 commented Oct 24, 2024

fixed by #1865

@dmah42 dmah42 closed this as completed Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants