-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole.cpp
More file actions
931 lines (822 loc) · 32.2 KB
/
Copy pathconsole.cpp
File metadata and controls
931 lines (822 loc) · 32.2 KB
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// SPDX-License-Identifier: MIT
#include "console.h"
#ifdef _WIN32
# include "stdc_windows.h"
# include <io.h>
#else
# include <sys/ioctl.h>
# include <unistd.h>
#endif
#include <atomic>
#include <mutex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "utf.h"
#include "console_p.h"
#include "str_p.h"
namespace stdc {
// Everything below asks about the *target* being written to, never about the process's own
// stdout, so redirected output is not styled just because stdout happens to be a terminal.
// What the target turned out to be. Independent of the configured color mode, so it can be
// remembered across writes.
enum target_kind {
target_not_a_terminal,
target_vt,
target_legacy, // a Windows console that would not take virtual terminal processing
};
#ifdef _WIN32
using target_id = HANDLE;
static const target_id invalid_target_id = INVALID_HANDLE_VALUE;
#else
using target_id = int;
static const target_id invalid_target_id = -1;
#endif
/// Returns the descriptor behind a file without asking the OS about it. Costs no system call,
/// so a remembered answer can be matched against the file it was probed through on every
/// write.
static target_id target_id_of(FILE *file) {
if (!file) {
return invalid_target_id;
}
#ifdef _WIN32
int fd = ::_fileno(file);
if (fd < 0) {
return invalid_target_id;
}
return reinterpret_cast<HANDLE>(::_get_osfhandle(fd));
#else
int fd = ::fileno(file);
return fd < 0 ? invalid_target_id : fd;
#endif
}
/// Asks the OS what the target is, and on Windows turns on virtual terminal processing while
/// there. This costs system calls and changes console state, so it runs once per target
/// rather than once per write.
static target_kind detect_target(FILE *file) {
target_id id = target_id_of(file);
if (id == invalid_target_id) {
return target_not_a_terminal;
}
#ifdef _WIN32
DWORD mode = 0;
if (!::GetConsoleMode(id, &mode)) {
return target_not_a_terminal;
}
if (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) {
return target_vt;
}
return ::SetConsoleMode(id, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) ? target_vt
: target_legacy;
#else
return ::isatty(id) ? target_vt : target_not_a_terminal;
#endif
}
// A handful of slots is plenty: in practice this only ever sees stdout and stderr. A target
// that does not fit is simply probed every time, which is correct, just not free.
//
// An entry is trusted only while the file still resolves to the descriptor it was probed
// through. That check costs nothing and catches a FILE being closed and its address reused.
// It does not catch freopen() swapping the target underneath the same descriptor, which is
// what set_color_mode() clearing the table is for.
struct target_cache_entry {
FILE *file;
target_id id;
target_kind kind;
};
static constexpr int target_cache_slots = 4;
static target_cache_entry g_target_cache[target_cache_slots]{};
static int g_target_cache_size = 0;
static std::mutex &target_cache_mtx() {
static std::mutex instance;
return instance;
}
static void clear_target_cache() {
std::lock_guard<std::mutex> lock(target_cache_mtx());
g_target_cache_size = 0;
}
static target_kind target_kind_of(FILE *file) {
target_id id = target_id_of(file);
if (id == invalid_target_id) {
return target_not_a_terminal;
}
std::lock_guard<std::mutex> lock(target_cache_mtx());
int slot = -1;
for (int i = 0; i < g_target_cache_size; ++i) {
if (g_target_cache[i].file == file) {
slot = i;
break;
}
}
if (slot >= 0 && g_target_cache[slot].id == id) {
return g_target_cache[slot].kind;
}
target_kind kind = detect_target(file);
if (slot < 0 && g_target_cache_size < target_cache_slots) {
slot = g_target_cache_size++;
}
if (slot >= 0) {
g_target_cache[slot] = {file, id, kind};
}
return kind;
}
class ConsoleOutputGuard {
public:
class BaseOutput {
public:
BaseOutput() = default;
virtual ~BaseOutput() = default;
void enter(FILE *file) {
global_mtx().lock();
_file = file;
#ifdef _WIN32
// Switching the code page is a process wide side effect, so only pay it when the
// target really is a console. For a file or a pipe it would change nothing there
// and disturb whatever else writes to the console. Both calls below come from
// the cache, so this costs no system call of its own.
_console = target_kind_of(file) != target_not_a_terminal ? target_id_of(file)
: INVALID_HANDLE_VALUE;
if (_console != INVALID_HANDLE_VALUE) {
_codepage = ::GetConsoleOutputCP();
::SetConsoleOutputCP(CP_UTF8);
}
#endif
attach();
}
void leave() {
reset();
#ifdef _WIN32
if (_console != INVALID_HANDLE_VALUE) {
::SetConsoleOutputCP(_codepage);
_console = INVALID_HANDLE_VALUE;
}
#endif
_file = nullptr;
global_mtx().unlock();
}
virtual void change(int style, int fg, int bg) = 0;
virtual void reset() = 0;
protected:
// Runs once the target is in place, for whatever a backend has to read off it.
virtual void attach() {
}
FILE *_file = nullptr;
static const int _style_init = console::nostyle;
static const int _fg_init = console::nocolor;
static const int _bg_init = console::nocolor;
int _style = _style_init;
int _fg = _fg_init;
int _bg = _bg_init;
console::detail::attributes current() const {
return {_style, _fg, _bg};
}
void set_current(const console::detail::attributes &attrs) {
_style = attrs.style;
_fg = attrs.fg;
_bg = attrs.bg;
}
static std::mutex &global_mtx() {
static std::mutex _instance;
return _instance;
}
#ifdef _WIN32
HANDLE _console = INVALID_HANDLE_VALUE;
private:
UINT _codepage{};
#endif
};
// Writes no attributes at all. Used whenever the target cannot show them, which covers
// every file and pipe, and is what makes the text path deterministic enough to test.
class PlainOutput : public BaseOutput {
public:
void change(int style, int fg, int bg) override {
(void) style;
(void) fg;
(void) bg;
}
void reset() override {
}
};
class VTOutput : public BaseOutput {
public:
VTOutput() = default;
void change(int style, int fg, int bg) override {
console::detail::attributes next{style, fg, bg};
auto seq = console::detail::sgr_sequence(current(), next);
if (!seq.empty()) {
std::ignore = std::fputs(seq.c_str(), _file);
}
// Assigned even when nothing was emitted, so an attribute that has no escape code
// of its own still counts as the state we are now in.
set_current(next);
}
void reset() override {
auto seq = console::detail::sgr_reset_sequence(current());
if (!seq.empty()) {
std::ignore = std::fputs(seq.c_str(), _file);
set_current({});
}
}
};
#ifdef _WIN32
class WindowsLegacyOutput : public BaseOutput {
public:
WindowsLegacyOutput() = default;
protected:
// The attributes to restore are read off the target that was just attached, not off
// the process's stdout.
void attach() override {
if (_console != INVALID_HANDLE_VALUE) {
::GetConsoleScreenBufferInfo(_console, &_csbi);
}
}
public:
void change(int style, int fg, int bg) override {
console::detail::attributes next{style, fg, bg};
if (next != current()) {
set_current(next);
if (_console != INVALID_HANDLE_VALUE) {
::SetConsoleTextAttribute(
_console, console::detail::legacy_attributes(next, _csbi.wAttributes));
}
}
}
void reset() override {
if (current() != console::detail::attributes{}) {
if (_console != INVALID_HANDLE_VALUE) {
::SetConsoleTextAttribute(_console, _csbi.wAttributes);
}
set_current({});
}
}
private:
CONSOLE_SCREEN_BUFFER_INFO _csbi{};
};
#endif
static BaseOutput &output_for(FILE *file) {
switch (console::resolve_color_mode(file)) {
case console::color_mode::vt: {
static VTOutput instance;
return instance;
}
#ifdef _WIN32
case console::color_mode::windows_legacy: {
static WindowsLegacyOutput instance;
return instance;
}
#endif
default:
break;
}
static PlainOutput instance;
return instance;
}
ConsoleOutputGuard(FILE *file) : _output(&output_for(file)) {
_output->enter(file);
}
~ConsoleOutputGuard() {
_output->leave();
}
void change(int style, int fg, int bg) {
_output->change(style, fg, bg);
}
void reset() {
_output->reset();
}
private:
BaseOutput *_output;
};
namespace console {
namespace detail {
// The SGR code for a foreground color, or nullptr when there is none. Both `nocolor`
// and `black` land in the default branch: neither has ever had a code here, and
// giving them one now would change how existing markup renders.
static const char *fg_code(int fg) {
const bool light = fg & intensified;
switch (fg & 0xF) {
case red:
return light ? "91" : "31";
case green:
return light ? "92" : "32";
case blue:
return light ? "94" : "34";
case yellow:
return light ? "93" : "33";
case purple:
return light ? "95" : "35";
case cyan:
return light ? "96" : "36";
case white:
return light ? "97" : "37";
default:
return nullptr;
}
}
static const char *bg_code(int bg) {
const bool light = bg & intensified;
switch (bg & 0xF) {
case red:
return light ? "101" : "41";
case green:
return light ? "102" : "42";
case blue:
return light ? "104" : "44";
case yellow:
return light ? "103" : "43";
case purple:
return light ? "105" : "45";
case cyan:
return light ? "106" : "46";
case white:
return light ? "107" : "47";
default:
return nullptr;
}
}
std::string sgr_sequence(const attributes &from, const attributes &to) {
std::string codes;
const auto &push = [&codes](const char *code) {
if (!codes.empty()) {
codes += ';';
}
codes += code;
};
if (to.fg != from.fg) {
if (const char *code = fg_code(to.fg)) {
push(code);
}
}
if (to.bg != from.bg) {
if (const char *code = bg_code(to.bg)) {
push(code);
}
}
if (to.style != from.style) {
if (to.style & bold) {
push("1");
}
if (to.style & italic) {
push("3");
}
if (to.style & underline) {
push("4");
}
if (to.style & strikethrough) {
push("9");
}
}
if (codes.empty()) {
return {};
}
return "\033[" + codes + "m";
}
std::string sgr_reset_sequence(const attributes &from) {
if (from == attributes{}) {
return {};
}
return "\033[0m";
}
}
namespace detail {
#ifdef _WIN32
WORD legacy_attributes(const attributes &attrs, WORD initial) {
const auto &color_bits = [](int color, WORD redBit, WORD greenBit, WORD blueBit) {
switch (color & 0xF) {
case red:
return redBit;
case green:
return greenBit;
case blue:
return blueBit;
case yellow:
return WORD(redBit | greenBit);
case purple:
return WORD(redBit | blueBit);
case cyan:
return WORD(greenBit | blueBit);
case white:
return WORD(redBit | greenBit | blueBit);
default:
return WORD(0);
}
};
WORD result = initial;
if (attrs.fg != nocolor) {
constexpr WORD mask = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
FOREGROUND_INTENSITY;
result &= ~mask;
result |= color_bits(attrs.fg, FOREGROUND_RED, FOREGROUND_GREEN,
FOREGROUND_BLUE);
if ((attrs.fg & intensified) || (attrs.style & bold)) {
result |= FOREGROUND_INTENSITY;
}
} else if (attrs.style & bold) {
result |= FOREGROUND_INTENSITY;
}
if (attrs.bg != nocolor) {
constexpr WORD mask = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
BACKGROUND_INTENSITY;
result &= ~mask;
result |= color_bits(attrs.bg, BACKGROUND_RED, BACKGROUND_GREEN,
BACKGROUND_BLUE);
if (attrs.bg & intensified) {
result |= BACKGROUND_INTENSITY;
}
}
return result;
}
int columns_of(const CONSOLE_SCREEN_BUFFER_INFO &info) {
return int(info.srWindow.Right) - int(info.srWindow.Left) + 1;
}
#endif
}
int width(FILE *file, int fallback) {
// COLUMNS first, which is the convention every terminal-aware program follows, and
// is also the only say anyone has when the output is not a terminal to begin with.
if (const char *columns = std::getenv("COLUMNS")) {
int value = std::atoi(columns);
if (value > 0) {
return value;
}
}
target_id id = target_id_of(file);
if (id == invalid_target_id) {
return fallback;
}
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO info{};
if (!::GetConsoleScreenBufferInfo(id, &info)) {
return fallback;
}
int value = detail::columns_of(info);
#else
struct winsize ws {};
if (::ioctl(id, TIOCGWINSZ, &ws) != 0) {
return fallback;
}
int value = int(ws.ws_col);
#endif
// A terminal that answers zero has told us nothing, which happens under some
// multiplexers and in a few CI runners.
return value > 0 ? value : fallback;
}
namespace {
/// The code point ranges that take two columns, from Unicode's East Asian Width
/// property. The Wide and Fullwidth blocks a program prints in practice, rather than
/// the whole table, which would be several hundred ranges and a generator to keep it.
bool is_wide(char32_t c) {
return (c >= 0x1100 && c <= 0x115F) || // Hangul Jamo, initial consonants
(c >= 0x2E80 && c <= 0x303E) || // CJK radicals, Kangxi, CJK symbols
(c >= 0x3041 && c <= 0x33FF) || // kana through CJK compatibility
(c >= 0x3400 && c <= 0x4DBF) || // CJK extension A
(c >= 0x4E00 && c <= 0x9FFF) || // CJK unified ideographs
(c >= 0xA000 && c <= 0xA4CF) || // Yi
(c >= 0xAC00 && c <= 0xD7A3) || // Hangul syllables
(c >= 0xF900 && c <= 0xFAFF) || // CJK compatibility ideographs
(c >= 0xFE30 && c <= 0xFE6F) || // CJK compatibility forms
(c >= 0xFF00 && c <= 0xFF60) || // fullwidth forms
(c >= 0xFFE0 && c <= 0xFFE6) || // fullwidth signs
(c >= 0x1F300 && c <= 0x1F64F) || // pictographs and emoticons
(c >= 0x1F900 && c <= 0x1F9FF) || // supplemental symbols
(c >= 0x20000 && c <= 0x3FFFD); // CJK extension B and later
}
/// Marks that attach to the character before them, taking no column of their own.
bool is_combining(char32_t c) {
return (c >= 0x0300 && c <= 0x036F) || (c >= 0x1AB0 && c <= 0x1AFF) ||
(c >= 0x20D0 && c <= 0x20FF) || (c >= 0xFE20 && c <= 0xFE2F);
}
}
int display_width(char32_t c) {
if (is_combining(c)) {
return 0;
}
return is_wide(c) ? 2 : 1;
}
int display_width(const std::string_view &utf8) {
int columns = 0;
for (char32_t c : utf::utf8_to_utf32(utf8)) {
columns += display_width(c);
}
return columns;
}
static std::atomic<color_mode> g_color_mode{color_mode::automatic};
color_mode get_color_mode() {
return g_color_mode.load(std::memory_order_relaxed);
}
void set_color_mode(color_mode mode) {
g_color_mode.store(mode, std::memory_order_relaxed);
// Also the way to make a target be probed again, which a program that has swapped one
// out with freopen() needs.
clear_target_cache();
}
color_mode resolve_color_mode(FILE *file) {
auto mode = g_color_mode.load(std::memory_order_relaxed);
if (mode != color_mode::automatic) {
#ifndef _WIN32
// No console API to drive here. Writing nothing beats writing escape sequences
// the caller did not ask for.
if (mode == color_mode::windows_legacy) {
return color_mode::never;
}
#endif
return mode;
}
// Probing the target is the expensive half, and on a console it also turns virtual
// terminal processing on, so it happens once per target and is remembered.
switch (target_kind_of(file)) {
case target_vt:
return color_mode::vt;
#ifdef _WIN32
case target_legacy:
return color_mode::windows_legacy;
#endif
default:
break;
}
return color_mode::never;
}
int fputs(int style, int fg, int bg, const char *buf, FILE *file) {
ConsoleOutputGuard cog(file);
cog.change(style, fg, bg);
return std::fputs(buf, file);
}
int fputs(int style, int fg, int bg, const std::string_view &buf, FILE *file) {
ConsoleOutputGuard cog(file);
cog.change(style, fg, bg);
return std::fwrite(buf.data(), sizeof(char), buf.size(), file);
}
int puts(int style, int fg, int bg, const char *buf) {
int ret = console::fputs(style, fg, bg, buf, stdout);
// flush the internal buffer after resetting the console attributes
if (std::putchar('\n') != EOF) {
ret += 1;
}
return ret;
}
int puts(int style, int fg, int bg, const std::string_view &buf) {
int ret = console::fputs(style, fg, bg, buf, stdout);
// flush the internal buffer after resetting the console attributes
if (std::putchar('\n') != EOF) {
ret += 1;
}
return ret;
}
int fprintf(int style, int fg, int bg, FILE *file, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = console::vfprintf(style, fg, bg, file, fmt, args);
va_end(args);
return ret;
}
int vfprintf(int style, int fg, int bg, FILE *file, const char *fmt, va_list args) {
ConsoleOutputGuard cog(file);
cog.change(style, fg, bg);
return std::vfprintf(file, fmt, args);
}
int printf(int style, int fg, int bg, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = console::vprintf(style, fg, bg, fmt, args);
va_end(args);
return ret;
}
int vprintf(int style, int fg, int bg, const char *fmt, va_list args) {
return console::vfprintf(style, fg, bg, stdout, fmt, args);
}
int u8fprintf(FILE *file, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = console::u8vfprintf(file, fmt, args);
va_end(args);
return ret;
}
int u8vfprintf(FILE *file, const char *fmt, va_list args) {
return console::vfprintf(nostyle, nocolor, nocolor, file, fmt, args);
}
int u8printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = console::u8vprintf(fmt, args);
va_end(args);
return ret;
}
int u8vprintf(const char *fmt, va_list args) {
return console::vfprintf(nostyle, nocolor, nocolor, stdout, fmt, args);
}
// The ${...} markup the cfputs family reads is after xmake's, which spells a foreground
// ${red}, a background ${onred}, and the way back ${clear}. Ours writes a background
// ${@red} and only ${reset}, and takes the 8 colors and the attribute words rather than
// the 256 and true color forms.
//
// https://github.com/xmake-io/xmake/blob/v2.9.9/xmake/core/base/colors.lua
static int cfputs_get_color_name(const std::string_view &var) {
if (var == "red") {
return red;
}
if (var == "green") {
return green;
}
if (var == "blue") {
return blue;
}
if (var == "yellow") {
return yellow;
}
if (var == "purple") {
return purple;
}
if (var == "cyan") {
return cyan;
}
if (var == "white") {
return white;
}
if (var == "black") {
return black;
}
if (var == "nocolor") {
return nocolor;
}
return -1;
}
static inline int cfputs_get_color(const std::string_view &var) {
if (starts_with(var, "light")) {
int color = cfputs_get_color_name(var.substr(5));
if (color == -1 || color == nocolor) {
return -1;
}
return color | intensified;
}
return cfputs_get_color_name(var);
}
// @return:
// reset: false
// change: true
static bool cfputs_update_attrs(const std::string_view var, int &style, int &fg, int &bg) {
// reset?
if (var == "reset" || var == "clear") {
style = nostyle;
fg = nocolor;
bg = nocolor;
return false;
}
// bg?
if (STDC_UNLIKELY(var.front() == '@')) {
auto var1 = var.substr(1);
if (STDC_UNLIKELY(var1 == "intensified")) {
if (bg != nocolor)
bg |= intensified;
return true;
}
int color = cfputs_get_color(var1);
if (color != -1) {
bg = color;
}
return true;
}
// fg?
{
if (STDC_UNLIKELY(var == "intensified")) {
if (fg != nocolor)
fg |= intensified;
return true;
}
int color = cfputs_get_color(var);
if (color != -1) {
fg = color;
return true;
}
// fallthrough
}
// style?
if (var == "bold") {
style |= bold;
return true;
}
if (var == "italic") {
style |= italic;
return true;
}
if (var == "nostyle") {
style = nostyle;
return true;
}
if (var == "underline") {
style |= underline;
return true;
}
if (var == "strikethrough") {
style |= strikethrough;
return true;
}
return true;
}
int cfputs(const char *buf, FILE *file) {
return cfputs(std::string_view(buf), file);
}
int cfputs(const std::string_view &buf, FILE *file) {
using namespace str;
vlarray<varexp_part, 10> parts;
if (!varexp_split(buf, parts)) {
return std::fwrite(buf.data(), sizeof(char), buf.size(), file);
}
int style = nostyle;
int fg = nocolor;
int bg = nocolor;
ConsoleOutputGuard cog(file);
int ret = 0;
for (const auto &part : parts) {
switch (part.type) {
case varexp_part_type::literal: {
ret += std::fwrite(part.data, sizeof(char), part.size, file);
break;
}
case varexp_part_type::literal_with_dollar: {
const char *p = part.data;
const char *q = p + part.size;
const char *start = p;
while (p < q) {
if (*p == '$' && p + 1 < q && *(p + 1) == '$') {
ret += std::fwrite(start, sizeof(char), p - start + 1, file);
p += 2;
start = p;
continue;
}
p++;
}
if (start < p) {
ret += std::fwrite(start, sizeof(char), p - start, file);
}
break;
}
case varexp_part_type::variable: {
std::string_view part_view(part.data, part.size);
constexpr std::string_view whitespace = " \t";
size_t start = 0;
while (start < part_view.size()) {
// skip head whitespace
size_t word_start = part_view.find_first_not_of(whitespace, start);
if (word_start == std::string_view::npos)
break;
size_t word_end = part_view.find_first_of(whitespace, word_start);
// extract word
if (word_end == std::string_view::npos) {
std::string_view var = part_view.substr(word_start);
bool change = cfputs_update_attrs(var, style, fg, bg);
cog.reset();
if (change)
cog.change(style, fg, bg);
break;
} else {
std::string_view var =
part_view.substr(word_start, word_end - word_start);
bool change = cfputs_update_attrs(var, style, fg, bg);
cog.reset();
if (change)
cog.change(style, fg, bg);
}
// continue scanning from the end of the current word
start = word_end;
}
break;
}
case varexp_part_type::nested_variable:
// Nested ${} is not supported in color markup, unlike varexp().
break;
}
}
return ret;
}
int cputs(const char *buf) {
return cputs(std::string_view(buf));
}
int cputs(const std::string_view &buf) {
int ret = cfputs(buf, stdout);
if (std::putchar('\n') != EOF) {
ret += 1;
}
return ret;
}
int cfprintf(FILE *file, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = cvfprintf(file, fmt, args);
va_end(args);
return ret;
}
int cvfprintf(FILE *file, const char *fmt, va_list args) {
return cfputs(vasprintf(fmt, args), file);
}
int cprintf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = cvprintf(fmt, args);
va_end(args);
return ret;
}
STDC_EXPORT int cvprintf(const char *fmt, va_list args) {
return cvfprintf(stdout, fmt, args);
}
}
}