]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/colours.h
Move STATS handler back to cmd_stats so it's hotpatchable again
[user/henk/code/inspircd.git] / win / colours.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #ifndef COLORS_H
22 #define COLORS_H
23
24 #define TRED FOREGROUND_RED | FOREGROUND_INTENSITY
25 #define TGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
26 #define TYELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
27 #define TNORMAL FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE
28 #define TWHITE TNORMAL | FOREGROUND_INTENSITY
29 #define TBLUE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
30
31 inline void sc(WORD color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); }
32
33 /* Handles colors in printf */
34 int printf_c(const char * format, ...)
35 {
36         // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
37         static char message[500];
38         static char temp[500];
39         int color1, color2;
40
41         /* parse arguments */
42         va_list ap;
43         va_start(ap, format);
44         vsnprintf(message, 500, format, ap);
45         va_end(ap);
46
47         /* search for unix-style escape sequences */
48         int t;
49         int c = 0;
50         const char * p = message;
51         while (*p != 0)
52         {
53                 if (*p == '\033')
54                 {
55                         // Escape sequence -> copy into the temp buffer, and parse the color.
56                         p++;
57                         t = 0;
58                         while ((*p) && (*p != 'm'))
59                         {
60                                 temp[t++] = *p;
61                                 ++p;
62                         }
63
64                         temp[t] = 0;
65                         p++;
66
67                         if (*temp == '[')
68                         {
69                                 if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
70                                 {
71                                         switch(color2)
72                                         {
73                                         case 32:                // Green
74                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);              // Yellow
75                                                 break;
76
77                                         default:                // Unknown
78                                                 // White
79                                                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
80                                                 break;
81                                         }
82                                 }
83                                 else
84                                 {
85                                         switch (*(temp+1))
86                                         {
87                                                 case '0':
88                                                         // Returning to normal colour.
89                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
90                                                         break;
91
92                                                 case '1':
93                                                         // White
94                                                         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), TWHITE);
95                                                         break;
96
97                                                 default:
98                                                         char message[50];
99                                                         sprintf(message, "Unknown color code: %s", temp);
100                                                         MessageBox(0, message, message, MB_OK);
101                                                         break;
102                                         }
103                                 }
104                         }
105                 }
106
107                 putchar(*p);
108                 ++c;
109                 ++p;
110         }
111
112         return c;
113 }
114
115 #endif
116