forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.cpp
737 lines (627 loc) · 15 KB
/
output.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/** \file output.c
Generic output functions
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <sys/time.h>
#include <unistd.h>
#include <wctype.h>
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <wchar.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "expand.h"
#include "common.h"
#include "output.h"
#include "highlight.h"
#include "env.h"
/**
Number of color names in the col array
*/
#define FISH_COLORS (sizeof(col)/sizeof(wchar_t *))
static int writeb_internal(char c);
/**
Names of different colors.
*/
static const wchar_t *col[]=
{
L"black",
L"red",
L"green",
L"brown",
L"yellow",
L"blue",
L"magenta",
L"purple",
L"cyan",
L"white",
L"normal"
}
;
/**
Mapping from color name (the 'col' array) to color index as used in
ANSI color terminals, and also the fish_color_* constants defined
in highlight.h. Non-ANSI terminals will display the wrong colors,
since they use a different mapping.
*/
static const int col_idx[]=
{
0,
1,
2,
3,
3,
4,
5,
5,
6,
7,
FISH_COLOR_NORMAL,
};
/**
The function used for output
*/
static int (*out)(char c) = &writeb_internal;
/**
Name of terminal
*/
static wcstring current_term;
/* Whether term256 is supported */
static bool support_term256 = false;
void output_set_writer(int (*writer)(char))
{
CHECK(writer,);
out = writer;
}
int (*output_get_writer())(char)
{
return out;
}
static bool term256_support_is_native(void)
{
/* Return YES if we think the term256 support is "native" as opposed to forced. */
return max_colors == 256;
}
bool output_get_supports_term256(void)
{
return support_term256;
}
void output_set_supports_term256(bool val)
{
support_term256 = val;
}
unsigned char index_for_color(rgb_color_t c)
{
if (c.is_named() || ! output_get_supports_term256())
{
return c.to_name_index();
}
else
{
return c.to_term256_index();
}
}
static bool write_color(char *todo, unsigned char idx, bool is_fg)
{
bool result = false;
if (idx < 16 || term256_support_is_native())
{
/* Use tparm */
writembs(tparm(todo, idx));
result = true;
}
else
{
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
char stridx[128];
format_long_safe(stridx, idx);
char buff[128] = "\x1b[";
strcat(buff, is_fg ? "38;5;" : "48;5;");
strcat(buff, stridx);
strcat(buff, "m");
int (*writer)(char) = output_get_writer();
if (writer)
{
for (size_t i=0; buff[i]; i++)
{
writer(buff[i]);
}
}
result = true;
}
return result;
}
bool write_foreground_color(unsigned char idx)
{
if (set_a_foreground && set_a_foreground[0])
{
return write_color(set_a_foreground, idx, true);
}
else if (set_foreground && set_foreground[0])
{
return write_color(set_foreground, idx, true);
}
else
{
return false;
}
}
bool write_background_color(unsigned char idx)
{
if (set_a_background && set_a_background[0])
{
return write_color(set_a_background, idx, false);
}
else if (set_background && set_background[0])
{
return write_color(set_background, idx, false);
}
else
{
return false;
}
}
void set_color(rgb_color_t c, rgb_color_t c2)
{
#if 0
wcstring tmp = c.description();
wcstring tmp2 = c2.description();
printf("set_color %ls : %ls\n", tmp.c_str(), tmp2.c_str());
#endif
ASSERT_IS_MAIN_THREAD();
const rgb_color_t normal = rgb_color_t::normal();
static rgb_color_t last_color = rgb_color_t::normal();
static rgb_color_t last_color2 = rgb_color_t::normal();
static int was_bold=0;
static int was_underline=0;
int bg_set=0, last_bg_set=0;
int is_bold = 0;
int is_underline = 0;
/*
Test if we have at least basic support for setting fonts, colors
and related bits - otherwise just give up...
*/
if (!exit_attribute_mode)
{
return;
}
is_bold |= c.is_bold();
is_bold |= c2.is_bold();
is_underline |= c.is_underline();
is_underline |= c2.is_underline();
if (c.is_reset() || c2.is_reset())
{
c = c2 = normal;
was_bold=0;
was_underline=0;
/*
If we exit attibute mode, we must first set a color, or
previously coloured text might lose it's
color. Terminals are weird...
*/
write_foreground_color(0);
writembs(exit_attribute_mode);
return;
}
if (was_bold && !is_bold)
{
/*
Only way to exit bold mode is a reset of all attributes.
*/
writembs(exit_attribute_mode);
last_color = normal;
last_color2 = normal;
was_bold=0;
was_underline=0;
}
if (! last_color2.is_normal() &&
! last_color2.is_reset() &&
! last_color2.is_ignore())
{
/*
Background was set
*/
last_bg_set=1;
}
if (! c2.is_normal() &&
! c2.is_ignore())
{
/*
Background is set
*/
bg_set=1;
if (c==c2)
c = (c2==rgb_color_t::white())?rgb_color_t::black():rgb_color_t::white();
}
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
{
if (bg_set && !last_bg_set)
{
/*
Background color changed and is set, so we enter bold
mode to make reading easier. This means bold mode is
_always_ on when the background color is set.
*/
writembs(enter_bold_mode);
}
if (!bg_set && last_bg_set)
{
/*
Background color changed and is no longer set, so we
exit bold mode
*/
writembs(exit_attribute_mode);
was_bold=0;
was_underline=0;
/*
We don't know if exit_attribute_mode resets colors, so
we set it to something known.
*/
if (write_foreground_color(0))
{
last_color=rgb_color_t::black();
}
}
}
if (last_color != c)
{
if (c.is_normal())
{
write_foreground_color(0);
writembs(exit_attribute_mode);
last_color2 = rgb_color_t::normal();
was_bold=0;
was_underline=0;
}
else if (! c.is_special())
{
write_foreground_color(index_for_color(c));
}
}
last_color = c;
if (last_color2 != c2)
{
if (c2.is_normal())
{
write_background_color(0);
writembs(exit_attribute_mode);
if (! last_color.is_normal())
{
write_foreground_color(index_for_color(last_color));
}
was_bold=0;
was_underline=0;
last_color2 = c2;
}
else if (! c2.is_special())
{
write_background_color(index_for_color(c2));
last_color2 = c2;
}
}
/*
Lastly, we set bold mode and underline mode correctly
*/
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set)
{
if (is_bold && !was_bold)
{
if (enter_bold_mode)
{
writembs(tparm(enter_bold_mode));
}
}
was_bold = is_bold;
}
if (was_underline && !is_underline)
{
writembs(exit_underline_mode);
}
if (!was_underline && is_underline)
{
writembs(enter_underline_mode);
}
was_underline = is_underline;
}
/**
Default output method, simply calls write() on stdout
*/
static int writeb_internal(char c)
{
write_loop(1, &c, 1);
return 0;
}
int writeb(tputs_arg_t b)
{
out(b);
return 0;
}
int writech(wint_t ch)
{
mbstate_t state;
size_t i;
char buff[MB_LEN_MAX+1];
size_t bytes;
if ((ch >= ENCODE_DIRECT_BASE) &&
(ch < ENCODE_DIRECT_BASE+256))
{
buff[0] = ch - ENCODE_DIRECT_BASE;
bytes=1;
}
else
{
memset(&state, 0, sizeof(state));
bytes= wcrtomb(buff, ch, &state);
switch (bytes)
{
case (size_t)(-1):
{
return 1;
}
}
}
for (i=0; i<bytes; i++)
{
out(buff[i]);
}
return 0;
}
void writestr(const wchar_t *str)
{
char *pos;
CHECK(str,);
// while( *str )
// writech( *str++ );
/*
Check amount of needed space
*/
size_t len = wcstombs(0, str, 0);
if (len == (size_t)-1)
{
debug(1, L"Tried to print invalid wide character string");
return;
}
len++;
/*
Convert
*/
char *buffer, static_buffer[256];
if (len <= sizeof static_buffer)
buffer = static_buffer;
else
buffer = new char[len];
wcstombs(buffer,
str,
len);
/*
Write
*/
for (pos = buffer; *pos; pos++)
{
out(*pos);
}
if (buffer != static_buffer)
delete[] buffer;
}
void writestr_ellipsis(const wchar_t *str, int max_width)
{
int written=0;
int tot;
CHECK(str,);
tot = my_wcswidth(str);
if (tot <= max_width)
{
writestr(str);
return;
}
while (*str != 0)
{
int w = fish_wcwidth(*str);
if (written+w+fish_wcwidth(ellipsis_char)>max_width)
{
break;
}
written+=w;
writech(*(str++));
}
written += fish_wcwidth(ellipsis_char);
writech(ellipsis_char);
while (written < max_width)
{
written++;
writestr(L" ");
}
}
int write_escaped_str(const wchar_t *str, int max_len)
{
wchar_t *out;
int i;
int len;
int written=0;
CHECK(str, 0);
out = escape(str, 1);
len = my_wcswidth(out);
if (max_len && (max_len < len))
{
for (i=0; (written+fish_wcwidth(out[i]))<=(max_len-1); i++)
{
writech(out[i]);
written += fish_wcwidth(out[i]);
}
writech(ellipsis_char);
written += fish_wcwidth(ellipsis_char);
for (i=written; i<max_len; i++)
{
writech(L' ');
written++;
}
}
else
{
written = len;
writestr(out);
}
free(out);
return written;
}
int output_color_code(const wcstring &val, bool is_background)
{
size_t i;
int color=FISH_COLOR_NORMAL;
int is_bold=0;
int is_underline=0;
if (val.empty())
return FISH_COLOR_NORMAL;
wcstring_list_t el;
tokenize_variable_array(val, el);
for (size_t j=0; j < el.size(); j++)
{
const wcstring &next = el.at(j);
wcstring color_name;
if (is_background)
{
// look for something like "--background=red"
const wcstring prefix = L"--background=";
if (string_prefixes_string(prefix, next))
{
color_name = wcstring(next, prefix.size());
}
}
else
{
if (next == L"--bold" || next == L"-o")
is_bold = true;
else if (next == L"--underline" || next == L"-u")
is_underline = true;
else
color_name = next;
}
if (! color_name.empty())
{
for (i=0; i<FISH_COLORS; i++)
{
if (wcscasecmp(col[i], color_name.c_str()) == 0)
{
color = col_idx[i];
break;
}
}
}
}
return color | (is_bold?FISH_COLOR_BOLD:0) | (is_underline?FISH_COLOR_UNDERLINE:0);
}
rgb_color_t parse_color(const wcstring &val, bool is_background)
{
int is_bold=0;
int is_underline=0;
std::vector<rgb_color_t> candidates;
wcstring_list_t el;
tokenize_variable_array(val, el);
for (size_t j=0; j < el.size(); j++)
{
const wcstring &next = el.at(j);
wcstring color_name;
if (is_background)
{
// look for something like "--background=red"
const wcstring prefix = L"--background=";
if (string_prefixes_string(prefix, next))
{
color_name = wcstring(next, prefix.size());
}
}
else
{
if (next == L"--bold" || next == L"-o")
is_bold = true;
else if (next == L"--underline" || next == L"-u")
is_underline = true;
else
color_name = next;
}
if (! color_name.empty())
{
rgb_color_t color = rgb_color_t(color_name);
if (! color.is_none())
{
candidates.push_back(color);
}
}
}
// Pick the best candidate
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
for (size_t i=0; i < candidates.size(); i++)
{
const rgb_color_t &color = candidates.at(i);
if (color.is_rgb() && first_rgb.is_none())
first_rgb = color;
if (color.is_named() && first_named.is_none())
first_named = color;
}
// If we have both RGB and named colors, then prefer rgb if term256 is supported
rgb_color_t result;
if ((!first_rgb.is_none() && output_get_supports_term256()) || first_named.is_none())
{
result = first_rgb;
}
else
{
result = first_named;
}
if (result.is_none())
result = rgb_color_t::normal();
result.set_bold(is_bold);
result.set_underline(is_underline);
#if 0
wcstring desc = result.description();
printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground");
#endif
return result;
}
void output_set_term(const wcstring &term)
{
current_term.assign(term);
}
const wchar_t *output_get_term()
{
return current_term.empty() ? L"<unknown>" : current_term.c_str();
}
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line)
{
if (mbs != NULL)
{
tputs(mbs, 1, &writeb);
}
else
{
debug( 0, _(L"Tried to use terminfo string %s on line %ld of %s, which is undefined in terminal of type \"%ls\". Please report this error to %s"),
mbs_name,
line,
file,
output_get_term(),
PACKAGE_BUGREPORT);
}
}