123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2011 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
- /* copyright --> */
- #include "WinConsoleFile.h"
- #include <cstring>
- #include <cstdio>
- #include <cstdarg>
- #include <vector>
- #include "a2io.h"
- #include "util.h"
- namespace {
- #define FOREGROUND_BLACK 0
- #define FOREGROUND_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
- #define BACKGROUND_BLACK 0
- #define BACKGROUND_WHITE BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
- const WORD kForeground[] = {
- FOREGROUND_BLACK, // black
- FOREGROUND_RED, // red
- FOREGROUND_GREEN, // green
- FOREGROUND_RED | FOREGROUND_GREEN, // yellow
- FOREGROUND_BLUE, // blue
- FOREGROUND_BLUE | FOREGROUND_RED, // magenta
- FOREGROUND_BLUE | FOREGROUND_GREEN, // cyan
- FOREGROUND_WHITE // white
- };
- const WORD kBackground[] = {
- BACKGROUND_BLACK, // black
- BACKGROUND_RED, // red
- BACKGROUND_GREEN, // green
- BACKGROUND_RED | BACKGROUND_GREEN, // yellow
- BACKGROUND_BLUE, // blue
- BACKGROUND_BLUE | BACKGROUND_RED, // magenta
- BACKGROUND_BLUE | BACKGROUND_GREEN, // cyan
- BACKGROUND_WHITE // white
- };
- } // namespace
- namespace aria2 {
- WinConsoleFile::WinConsoleFile(DWORD stdHandle)
- : stdHandle_(stdHandle),
- bold_(false),
- underline_(false),
- reverse_(false),
- fg_(FOREGROUND_WHITE),
- bg_(BACKGROUND_BLACK)
- {
- if (supportsColor()) {
- CONSOLE_SCREEN_BUFFER_INFO info;
- GetConsoleScreenBufferInfo(handle(), &info);
- info.wAttributes &=
- ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE |
- COMMON_LVB_GRID_HORIZONTAL | COMMON_LVB_GRID_LVERTICAL |
- COMMON_LVB_GRID_RVERTICAL | COMMON_LVB_REVERSE_VIDEO |
- COMMON_LVB_UNDERSCORE);
- fg_ = info.wAttributes &
- ~(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED |
- BACKGROUND_INTENSITY);
- bg_ = info.wAttributes &
- ~(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED |
- FOREGROUND_INTENSITY);
- bg_ = (bg_ >> 4) & 0x0F;
- bold_ = info.wAttributes & FOREGROUND_INTENSITY;
- underline_ = info.wAttributes & BACKGROUND_INTENSITY;
- }
- deffg_ = fg_;
- defbg_ = bg_;
- }
- bool WinConsoleFile::supportsColor()
- {
- DWORD mode;
- return GetConsoleMode(handle(), &mode);
- }
- size_t WinConsoleFile::write(const char* str)
- {
- if (!supportsColor()) {
- DWORD written = 0;
- WriteFile(handle(), str, strlen(str), &written, 0);
- return written;
- }
- auto msg = utf8ToWChar(str);
- return writeColorful(msg);
- }
- int WinConsoleFile::vprintf(const char* format, va_list va)
- {
- ssize_t r = vsnprintf(NULL, 0, format, va);
- if (r <= 0) {
- return 0;
- }
- auto buf = make_unique<char[]>(++r);
- r = vsnprintf(buf.get(), r, format, va);
- if (r < 0) {
- return 0;
- }
- return write(buf.get());
- }
- size_t WinConsoleFile::writeColorful(const std::wstring& str)
- {
- size_t written = 0;
- DWORD cw;
- wchar_t suffix;
- int arg = 0;
- std::vector<int> args;
- std::vector<wchar_t> buffer;
- buffer.reserve(str.length());
- enum state_ { ePrefix, ePreFin, eNum0, eNum } state = ePrefix;
- for (const wchar_t ch : str) {
- if (state == ePrefix) {
- if (ch == '\033') {
- state = ePreFin;
- }
- else {
- buffer.push_back(ch);
- continue;
- }
- }
- else if (state == ePreFin) {
- if (ch == '\033')
- ;
- else if (ch == '[') {
- state = eNum0;
- }
- else {
- state = ePrefix;
- }
- }
- else if (state == eNum0 || state == eNum) {
- if (isdigit(ch)) {
- arg = (arg * 10) + (ch - '0');
- state = eNum;
- }
- else if (ch == ';') {
- args.push_back(arg);
- arg = 0;
- state = eNum0;
- }
- else if (ch != '?') {
- if (state == eNum) {
- args.push_back(arg);
- }
- suffix = ch;
- goto out;
- }
- }
- ++written;
- continue;
- out:
- cw = 0;
- if (!buffer.empty()) {
- WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
- }
- written += cw;
- if (suffix == 'm') {
- if (args.empty()) {
- args.push_back(0);
- }
- for (const int a : args) {
- if (a == 0) {
- fg_ = deffg_;
- bg_ = defbg_;
- bold_ = underline_ = reverse_ = false;
- }
- else if (30 <= a && a <= 37) {
- fg_ = a - 30;
- }
- else if (40 <= a && a <= 47) {
- bg_ = a - 40;
- }
- else if (a == 1 || a == 21) {
- bold_ = a == 1;
- }
- else if (a == 4 || a == 24) {
- underline_ = a == 4;
- }
- else if (a == 7 || a == 27) {
- reverse_ = a == 7;
- }
- }
- WORD attribute = 0;
- if (reverse_) {
- attribute = kForeground[bg_] | kBackground[fg_];
- }
- else {
- attribute = kForeground[fg_] | kBackground[bg_];
- }
- if (bold_) {
- attribute |= FOREGROUND_INTENSITY;
- }
- if (underline_) {
- attribute |= BACKGROUND_INTENSITY;
- }
- SetConsoleTextAttribute(handle(), attribute);
- }
- suffix = 0;
- state = ePrefix;
- arg = 0;
- args.clear();
- buffer.clear();
- ++written;
- }
- if (!buffer.empty()) {
- cw = 0;
- WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
- written += cw;
- }
- return written;
- }
- } // namespace aria2
|