WinConsoleFile.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 WORD kBackground[] = {
  58. BACKGROUND_BLACK, // black
  59. BACKGROUND_RED, // red
  60. BACKGROUND_GREEN, // green
  61. BACKGROUND_RED | BACKGROUND_GREEN, // yellow
  62. BACKGROUND_BLUE, // blue
  63. BACKGROUND_BLUE | BACKGROUND_RED, // magenta
  64. BACKGROUND_BLUE | BACKGROUND_GREEN, // cyan
  65. BACKGROUND_WHITE // white
  66. };
  67. } // namespace
  68. namespace aria2 {
  69. WinConsoleFile::WinConsoleFile(DWORD stdHandle)
  70. : stdHandle_(stdHandle),
  71. bold_(false),
  72. underline_(false),
  73. reverse_(false),
  74. fg_(FOREGROUND_WHITE),
  75. bg_(BACKGROUND_BLACK)
  76. {
  77. if (supportsColor()) {
  78. CONSOLE_SCREEN_BUFFER_INFO info;
  79. GetConsoleScreenBufferInfo(handle(), &info);
  80. info.wAttributes &=
  81. ~(COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE |
  82. COMMON_LVB_GRID_HORIZONTAL | COMMON_LVB_GRID_LVERTICAL |
  83. COMMON_LVB_GRID_RVERTICAL | COMMON_LVB_REVERSE_VIDEO |
  84. COMMON_LVB_UNDERSCORE);
  85. fg_ = info.wAttributes &
  86. ~(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED |
  87. BACKGROUND_INTENSITY);
  88. bg_ = info.wAttributes &
  89. ~(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED |
  90. FOREGROUND_INTENSITY);
  91. bg_ = (bg_ >> 4) & 0x0F;
  92. bold_ = info.wAttributes & FOREGROUND_INTENSITY;
  93. underline_ = info.wAttributes & BACKGROUND_INTENSITY;
  94. }
  95. deffg_ = fg_;
  96. defbg_ = bg_;
  97. }
  98. bool WinConsoleFile::supportsColor()
  99. {
  100. DWORD mode;
  101. return GetConsoleMode(handle(), &mode);
  102. }
  103. size_t WinConsoleFile::write(const char* str)
  104. {
  105. if (!supportsColor()) {
  106. DWORD written = 0;
  107. WriteFile(handle(), str, strlen(str), &written, 0);
  108. return written;
  109. }
  110. auto msg = utf8ToWChar(str);
  111. return writeColorful(msg);
  112. }
  113. int WinConsoleFile::vprintf(const char* format, va_list va)
  114. {
  115. ssize_t r = vsnprintf(NULL, 0, format, va);
  116. if (r <= 0) {
  117. return 0;
  118. }
  119. auto buf = make_unique<char[]>(++r);
  120. r = vsnprintf(buf.get(), r, format, va);
  121. if (r < 0) {
  122. return 0;
  123. }
  124. return write(buf.get());
  125. }
  126. size_t WinConsoleFile::writeColorful(const std::wstring& str)
  127. {
  128. size_t written = 0;
  129. DWORD cw;
  130. wchar_t suffix;
  131. int arg = 0;
  132. std::vector<int> args;
  133. std::vector<wchar_t> buffer;
  134. buffer.reserve(str.length());
  135. enum state_ { ePrefix, ePreFin, eNum0, eNum } state = ePrefix;
  136. for (const wchar_t ch : str) {
  137. if (state == ePrefix) {
  138. if (ch == '\033') {
  139. state = ePreFin;
  140. }
  141. else {
  142. buffer.push_back(ch);
  143. continue;
  144. }
  145. }
  146. else if (state == ePreFin) {
  147. if (ch == '\033')
  148. ;
  149. else if (ch == '[') {
  150. state = eNum0;
  151. }
  152. else {
  153. state = ePrefix;
  154. }
  155. }
  156. else if (state == eNum0 || state == eNum) {
  157. if (isdigit(ch)) {
  158. arg = (arg * 10) + (ch - '0');
  159. state = eNum;
  160. }
  161. else if (ch == ';') {
  162. args.push_back(arg);
  163. arg = 0;
  164. state = eNum0;
  165. }
  166. else if (ch != '?') {
  167. if (state == eNum) {
  168. args.push_back(arg);
  169. }
  170. suffix = ch;
  171. goto out;
  172. }
  173. }
  174. ++written;
  175. continue;
  176. out:
  177. cw = 0;
  178. if (!buffer.empty()) {
  179. WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
  180. }
  181. written += cw;
  182. if (suffix == 'm') {
  183. if (args.empty()) {
  184. args.push_back(0);
  185. }
  186. for (const int a : args) {
  187. if (a == 0) {
  188. fg_ = deffg_;
  189. bg_ = defbg_;
  190. bold_ = underline_ = reverse_ = false;
  191. }
  192. else if (30 <= a && a <= 37) {
  193. fg_ = a - 30;
  194. }
  195. else if (40 <= a && a <= 47) {
  196. bg_ = a - 40;
  197. }
  198. else if (a == 1 || a == 21) {
  199. bold_ = a == 1;
  200. }
  201. else if (a == 4 || a == 24) {
  202. underline_ = a == 4;
  203. }
  204. else if (a == 7 || a == 27) {
  205. reverse_ = a == 7;
  206. }
  207. }
  208. WORD attribute = 0;
  209. if (reverse_) {
  210. attribute = kForeground[bg_] | kBackground[fg_];
  211. }
  212. else {
  213. attribute = kForeground[fg_] | kBackground[bg_];
  214. }
  215. if (bold_) {
  216. attribute |= FOREGROUND_INTENSITY;
  217. }
  218. if (underline_) {
  219. attribute |= BACKGROUND_INTENSITY;
  220. }
  221. SetConsoleTextAttribute(handle(), attribute);
  222. }
  223. suffix = 0;
  224. state = ePrefix;
  225. arg = 0;
  226. args.clear();
  227. buffer.clear();
  228. ++written;
  229. }
  230. if (!buffer.empty()) {
  231. cw = 0;
  232. WriteConsoleW(handle(), buffer.data(), buffer.size(), &cw, nullptr);
  233. written += cw;
  234. }
  235. return written;
  236. }
  237. } // namespace aria2