ls-files: require worktree when --deleted is given
[git/kirr.git] / compat / winansi.c
blobe2d96dfe6f75213de567174261d9aeba3e663d9d
1 /*
2 * Copyright 2008 Peter Harris <git@peter.is-a-geek.org>
3 */
5 #include <windows.h>
6 #include "../git-compat-util.h"
8 /*
9 Functions to be wrapped:
11 #undef printf
12 #undef fprintf
13 #undef fputs
14 /* TODO: write */
17 ANSI codes used by git: m, K
19 This file is git-specific. Therefore, this file does not attempt
20 to implement any codes that are not used by git.
22 TODO: K
25 static HANDLE console;
26 static WORD plain_attr;
27 static WORD attr;
28 static int negative;
30 static void init(void)
32 CONSOLE_SCREEN_BUFFER_INFO sbi;
34 static int initialized = 0;
35 if (initialized)
36 return;
38 console = GetStdHandle(STD_OUTPUT_HANDLE);
39 if (console == INVALID_HANDLE_VALUE)
40 console = NULL;
42 if (!console)
43 return;
45 GetConsoleScreenBufferInfo(console, &sbi);
46 attr = plain_attr = sbi.wAttributes;
47 negative = 0;
49 initialized = 1;
53 #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
54 #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
56 static void set_console_attr(void)
58 WORD attributes = attr;
59 if (negative) {
60 attributes &= ~FOREGROUND_ALL;
61 attributes &= ~BACKGROUND_ALL;
63 /* This could probably use a bitmask
64 instead of a series of ifs */
65 if (attr & FOREGROUND_RED)
66 attributes |= BACKGROUND_RED;
67 if (attr & FOREGROUND_GREEN)
68 attributes |= BACKGROUND_GREEN;
69 if (attr & FOREGROUND_BLUE)
70 attributes |= BACKGROUND_BLUE;
72 if (attr & BACKGROUND_RED)
73 attributes |= FOREGROUND_RED;
74 if (attr & BACKGROUND_GREEN)
75 attributes |= FOREGROUND_GREEN;
76 if (attr & BACKGROUND_BLUE)
77 attributes |= FOREGROUND_BLUE;
79 SetConsoleTextAttribute(console, attributes);
82 static const char *set_attr(const char *str)
84 const char *func;
85 size_t len = strspn(str, "0123456789;");
86 func = str + len;
88 switch (*func) {
89 case 'm':
90 do {
91 long val = strtol(str, (char **)&str, 10);
92 switch (val) {
93 case 0: /* reset */
94 attr = plain_attr;
95 negative = 0;
96 break;
97 case 1: /* bold */
98 attr |= FOREGROUND_INTENSITY;
99 break;
100 case 2: /* faint */
101 case 22: /* normal */
102 attr &= ~FOREGROUND_INTENSITY;
103 break;
104 case 3: /* italic */
105 /* Unsupported */
106 break;
107 case 4: /* underline */
108 case 21: /* double underline */
109 /* Wikipedia says this flag does nothing */
110 /* Furthermore, mingw doesn't define this flag
111 attr |= COMMON_LVB_UNDERSCORE; */
112 break;
113 case 24: /* no underline */
114 /* attr &= ~COMMON_LVB_UNDERSCORE; */
115 break;
116 case 5: /* slow blink */
117 case 6: /* fast blink */
118 /* We don't have blink, but we do have
119 background intensity */
120 attr |= BACKGROUND_INTENSITY;
121 break;
122 case 25: /* no blink */
123 attr &= ~BACKGROUND_INTENSITY;
124 break;
125 case 7: /* negative */
126 negative = 1;
127 break;
128 case 27: /* positive */
129 negative = 0;
130 break;
131 case 8: /* conceal */
132 case 28: /* reveal */
133 /* Unsupported */
134 break;
135 case 30: /* Black */
136 attr &= ~FOREGROUND_ALL;
137 break;
138 case 31: /* Red */
139 attr &= ~FOREGROUND_ALL;
140 attr |= FOREGROUND_RED;
141 break;
142 case 32: /* Green */
143 attr &= ~FOREGROUND_ALL;
144 attr |= FOREGROUND_GREEN;
145 break;
146 case 33: /* Yellow */
147 attr &= ~FOREGROUND_ALL;
148 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
149 break;
150 case 34: /* Blue */
151 attr &= ~FOREGROUND_ALL;
152 attr |= FOREGROUND_BLUE;
153 break;
154 case 35: /* Magenta */
155 attr &= ~FOREGROUND_ALL;
156 attr |= FOREGROUND_RED | FOREGROUND_BLUE;
157 break;
158 case 36: /* Cyan */
159 attr &= ~FOREGROUND_ALL;
160 attr |= FOREGROUND_GREEN | FOREGROUND_BLUE;
161 break;
162 case 37: /* White */
163 attr |= FOREGROUND_RED |
164 FOREGROUND_GREEN |
165 FOREGROUND_BLUE;
166 break;
167 case 38: /* Unknown */
168 break;
169 case 39: /* reset */
170 attr &= ~FOREGROUND_ALL;
171 attr |= (plain_attr & FOREGROUND_ALL);
172 break;
173 case 40: /* Black */
174 attr &= ~BACKGROUND_ALL;
175 break;
176 case 41: /* Red */
177 attr &= ~BACKGROUND_ALL;
178 attr |= BACKGROUND_RED;
179 break;
180 case 42: /* Green */
181 attr &= ~BACKGROUND_ALL;
182 attr |= BACKGROUND_GREEN;
183 break;
184 case 43: /* Yellow */
185 attr &= ~BACKGROUND_ALL;
186 attr |= BACKGROUND_RED | BACKGROUND_GREEN;
187 break;
188 case 44: /* Blue */
189 attr &= ~BACKGROUND_ALL;
190 attr |= BACKGROUND_BLUE;
191 break;
192 case 45: /* Magenta */
193 attr &= ~BACKGROUND_ALL;
194 attr |= BACKGROUND_RED | BACKGROUND_BLUE;
195 break;
196 case 46: /* Cyan */
197 attr &= ~BACKGROUND_ALL;
198 attr |= BACKGROUND_GREEN | BACKGROUND_BLUE;
199 break;
200 case 47: /* White */
201 attr |= BACKGROUND_RED |
202 BACKGROUND_GREEN |
203 BACKGROUND_BLUE;
204 break;
205 case 48: /* Unknown */
206 break;
207 case 49: /* reset */
208 attr &= ~BACKGROUND_ALL;
209 attr |= (plain_attr & BACKGROUND_ALL);
210 break;
211 default:
212 /* Unsupported code */
213 break;
215 str++;
216 } while (*(str-1) == ';');
218 set_console_attr();
219 break;
220 case 'K':
221 /* TODO */
222 break;
223 default:
224 /* Unsupported code */
225 break;
228 return func + 1;
231 static int ansi_emulate(const char *str, FILE *stream)
233 int rv = 0;
234 const char *pos = str;
236 while (*pos) {
237 pos = strstr(str, "\033[");
238 if (pos) {
239 size_t len = pos - str;
241 if (len) {
242 size_t out_len = fwrite(str, 1, len, stream);
243 rv += out_len;
244 if (out_len < len)
245 return rv;
248 str = pos + 2;
249 rv += 2;
251 fflush(stream);
253 pos = set_attr(str);
254 rv += pos - str;
255 str = pos;
256 } else {
257 rv += strlen(str);
258 fputs(str, stream);
259 return rv;
262 return rv;
265 int winansi_fputs(const char *str, FILE *stream)
267 int rv;
269 if (!isatty(fileno(stream)))
270 return fputs(str, stream);
272 init();
274 if (!console)
275 return fputs(str, stream);
277 rv = ansi_emulate(str, stream);
279 if (rv >= 0)
280 return 0;
281 else
282 return EOF;
285 static int winansi_vfprintf(FILE *stream, const char *format, va_list list)
287 int len, rv;
288 char small_buf[256];
289 char *buf = small_buf;
290 va_list cp;
292 if (!isatty(fileno(stream)))
293 goto abort;
295 init();
297 if (!console)
298 goto abort;
300 va_copy(cp, list);
301 len = vsnprintf(small_buf, sizeof(small_buf), format, cp);
302 va_end(cp);
304 if (len > sizeof(small_buf) - 1) {
305 buf = malloc(len + 1);
306 if (!buf)
307 goto abort;
309 len = vsnprintf(buf, len + 1, format, list);
312 rv = ansi_emulate(buf, stream);
314 if (buf != small_buf)
315 free(buf);
316 return rv;
318 abort:
319 rv = vfprintf(stream, format, list);
320 return rv;
323 int winansi_fprintf(FILE *stream, const char *format, ...)
325 va_list list;
326 int rv;
328 va_start(list, format);
329 rv = winansi_vfprintf(stream, format, list);
330 va_end(list);
332 return rv;
335 int winansi_printf(const char *format, ...)
337 va_list list;
338 int rv;
340 va_start(list, format);
341 rv = winansi_vfprintf(stdout, format, list);
342 va_end(list);
344 return rv;