WinConsoleFile.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "WinConsoleFile.h"
  36. #include <cstring>
  37. #include <cstdio>
  38. #include <cstdarg>
  39. #include <vector>
  40. #include "a2io.h"
  41. #include "util.h"
  42. namespace {
  43. #define FOREGROUND_BLACK 0
  44. #define FOREGROUND_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  45. #define BACKGROUND_BLACK 0
  46. #define BACKGROUND_WHITE BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
  47. const WORD kForeground[] = {
  48. FOREGROUND_BLACK, // black
  49. FOREGROUND_RED, // red
  50. FOREGROUND_GREEN, // green
  51. FOREGROUND_RED | FOREGROUND_GREEN, // yellow
  52. FOREGROUND_BLUE, // blue
  53. FOREGROUND_BLUE | FOREGROUND_RED, // magenta
  54. FOREGROUND_BLUE | FOREGROUND_GREEN, // cyan
  55. FOREGROUND_WHITE // white
  56. };
  57. const int kForegroundSize = sizeof(kForeground) / sizeof(kForeground[0]);
  58. const WORD kBackground[] = {
  59. BACKGROUND_BLACK, // black
  60. BACKGROUND_RED, // red
  61. BACKGROUND_GREEN, // green
  62. BACKGROUND_RED | BACKGROUND_GREEN, // yellow
  63. BACKGROUND_BLUE, // blue
  64. BACKGROUND_BLUE | BACKGROUND_RED, // magenta
  65. BACKGROUND_BLUE | BACKGROUND_GREEN, // cyan
  66. BACKGROUND_WHITE // white
  67. };
  68. const int kBackgroundSize = sizeof(kBackground) / sizeof(kBackground[0]);
  69. } // namespace
  70. namespace aria2 {
  71. WinConsoleFile::WinConsoleFile(DWORD stdHandle)
  72. : stdHandle_(stdHandle),
  73. bold_(false),
  74. underline_(false),
  75. reverse_(false),
  76. fg_(7),
  77. bg_(0)
  78. {
  79. if (supportsColor()) {
  80. CONSOLE_SCREEN_BUFFER_INFO info;
  81. GetConsoleScreenBufferInfo(handle(), &info);
  82. bold_ = info.wAttributes & FOREGROUND_INTENSITY;
  83. underline_ = info.wAttributes & BACKGROUND_INTENSITY;
  84. int fgcolor = info.wAttributes &
  85. (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
  86. for (int fg = 0; fg < kForegroundSize; fg++) {
  87. if (kForeground[fg] == fgcolor) {
  88. fg_ = fg;
  89. break;
  90. }
  91. }
  92. int bgcolor = info.wAttributes &
  93. (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
  94. for (int bg = 0; bg < kBackgroundSize; bg++) {
  95. if (kBackground[bg] == bgcolor) {
  96. bg_ = bg;
  97. break;
  98. }
  99. }
  100. }
  101. deffg_ = fg_;
  102. defbg_ = bg_;
  103. }
  104. bool WinConsoleFile::supportsColor()
  105. {
  106. DWORD mode;
  107. return GetConsoleMode(handle(), &mode);
  108. }
  109. size_t WinConsoleFile::write(const char* str)
  110. {
  111. if (!supportsColor()) {
  112. DWORD written = 0;
  113. WriteFile(handle(), str, strlen(str), &written, 0);
  114. return written;
  115. }
  116. auto msg = utf8ToWChar(str);
  117. return writeColorful(msg);
  118. }
  119. int WinConsoleFile::vprintf(const char* format, va_list va)
  120. {
  121. ssize_t r = vsnprintf(NULL, 0, format, va);
  122. if (r <= 0) {
  123. return 0;
  124. }
  125. auto buf = make_unique<char[]>(++r);
  126. r = vsnprintf(buf.get(), r, format, va);
  127. if (r < 0) {
  128. return 0;
  129. }
  130. return write(buf.get());
  131. }
  132. size_t WinConsoleFile::writeColorful(const std::wstring& str)
  133. {
  134. size_t written = 0;
  135. DWORD cw;
  136. wchar_t suffix;
  137. int arg = 0;
  138. std::vector<int> args;
  139. std::vector<wchar_t> buffer;
  140. buffer.reserve(str.length());
  141. enum state_ { ePrefix, ePreFin, eNum0, eNum } state = ePrefix;
  142. for (const wchar_t ch : str) {
  143. if (state == ePrefix) {
  144. if (ch == '\033') {
  145. state = ePreFin;
  146. }
  147. else {
  148. buffer.push_back(ch);
  149. continue;
  150. }
  151. }
  152. else if (state == ePreFin) {
  153. if (ch == '\033')
  154. ;
  155. else if (ch == '[') {
  156. state = eNum0;
  157. }
  158. else {
  159. state = ePrefix;
  160. }
  161. }
  162. else if (state == eNum0 || state == eNum) {
  163. if (isdigit(ch)) {
  164. arg = (arg * 10) + (ch - '0');
  165. state = eNum;
  166. }
  167. else if (ch == ';') {
  168. args.push_back(arg);
  169. arg = 0;
  170. state = eNum0;
  171. }
  172. else if (ch != '?') {
  173. if (state == eNum) {
  174. args.push_back(arg);
  175. }
  176. suffix = ch;
  177. goto out;
  178. }
  179. }
  180. ++written;
  181. continue;
  182. out:
  183. cw = 0;
  184. if (!buffer.empty()) {
  185. WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
  186. }
  187. written += cw;
  188. if (suffix == 'm') {
  189. if (args.empty()) {
  190. args.push_back(0);
  191. }
  192. for (const int a : args) {
  193. if (a == 0) {
  194. fg_ = deffg_;
  195. bg_ = defbg_;
  196. bold_ = underline_ = reverse_ = false;
  197. }
  198. else if (30 <= a && a <= 37) {
  199. fg_ = a - 30;
  200. }
  201. else if (40 <= a && a <= 47) {
  202. bg_ = a - 40;
  203. }
  204. else if (a == 1 || a == 21) {
  205. bold_ = a == 1;
  206. }
  207. else if (a == 4 || a == 24) {
  208. underline_ = a == 4;
  209. }
  210. else if (a == 7 || a == 27) {
  211. reverse_ = a == 7;
  212. }
  213. }
  214. WORD attribute = 0;
  215. if (reverse_) {
  216. attribute = kForeground[bg_] | kBackground[fg_];
  217. }
  218. else {
  219. attribute = kForeground[fg_] | kBackground[bg_];
  220. }
  221. if (bold_) {
  222. attribute |= FOREGROUND_INTENSITY;
  223. }
  224. if (underline_) {
  225. attribute |= BACKGROUND_INTENSITY;
  226. }
  227. SetConsoleTextAttribute(handle(), attribute);
  228. }
  229. suffix = 0;
  230. state = ePrefix;
  231. arg = 0;
  232. args.clear();
  233. buffer.clear();
  234. ++written;
  235. }
  236. if (!buffer.empty()) {
  237. cw = 0;
  238. WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
  239. written += cw;
  240. }
  241. return written;
  242. }
  243. } // namespace aria2