Skip to content

Commit

Permalink
Merge pull request aria2#1058 from kwkam/patch-2
Browse files Browse the repository at this point in the history
WinConsoleFile: fix colour properly
  • Loading branch information
tatsuhiro-t authored Nov 5, 2017
2 parents f198cd4 + a45390c commit 16f458c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/WinConsoleFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,31 @@ WinConsoleFile::WinConsoleFile(DWORD stdHandle)
bg_(0)
{
if (supportsColor()) {
int color;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(handle(), &info);
bold_ = info.wAttributes & FOREGROUND_INTENSITY;
underline_ = info.wAttributes & BACKGROUND_INTENSITY;
int fgcolor = info.wAttributes &
(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
color = info.wAttributes &
(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
for (int fg = 0; fg < kForegroundSize; fg++) {
if (kForeground[fg] == fgcolor) {
if (kForeground[fg] == color) {
fg_ = fg;
break;
}
}
int bgcolor = info.wAttributes &
(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
color = info.wAttributes &
(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
for (int bg = 0; bg < kBackgroundSize; bg++) {
if (kBackground[bg] == bgcolor) {
if (kBackground[bg] == color) {
bg_ = bg;
break;
}
}
}

defbold_ = bold_;
defunderline_ = underline_;
deffg_ = fg_;
defbg_ = bg_;
}
Expand Down Expand Up @@ -216,9 +219,11 @@ size_t WinConsoleFile::writeColorful(const std::wstring& str)
}
for (const int a : args) {
if (a == 0) {
bold_ = defbold_;
underline_ = defunderline_;
reverse_ = false;
fg_ = deffg_;
bg_ = defbg_;
bold_ = underline_ = reverse_ = false;
}
else if (30 <= a && a <= 37) {
fg_ = a - 30;
Expand Down
4 changes: 2 additions & 2 deletions src/WinConsoleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class WinConsoleFile : public OutputFile {

private:
DWORD stdHandle_;
bool bold_;
bool underline_;
bool bold_, defbold_;
bool underline_, defunderline_;
bool reverse_;
WORD fg_, deffg_;
WORD bg_, defbg_;
Expand Down

0 comments on commit 16f458c

Please sign in to comment.