1 /* Implementation of text_art::styled_string.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_MEMORY
23 #define INCLUDE_VECTOR
25 #include "coretypes.h"
26 #include "make-unique.h"
27 #include "pretty-print.h"
29 #include "diagnostic.h"
31 #include "text-art/selftests.h"
32 #include "text-art/types.h"
33 #include "color-macros.h"
35 using namespace text_art
;
39 /* Support class for parsing text containing escape codes.
40 See e.g. https://en.wikipedia.org/wiki/ANSI_escape_code
41 We only support the codes that pretty-print.cc can generate. */
43 class escape_code_parser
46 escape_code_parser (style_manager
&sm
,
47 std::vector
<styled_unichar
> &out
)
51 m_cur_style_id (style::id_plain
),
52 m_state (state::START
)
56 void on_char (cppchar_t ch
)
65 /* The start of an escape sequence. */
66 m_state
= state::AFTER_ESC
;
70 case state::AFTER_ESC
:
73 /* ESC [ is a Control Sequence Introducer. */
74 m_state
= state::CS_PARAMETER_BYTES
;
79 /* ESC ] is an Operating System Command. */
80 m_state
= state::WITHIN_OSC
;
84 case state::CS_PARAMETER_BYTES
:
85 if (parameter_byte_p (ch
))
87 m_parameter_bytes
.push_back ((char)ch
);
90 else if (intermediate_byte_p (ch
))
92 m_intermediate_bytes
.push_back ((char)ch
);
93 m_state
= state::CS_INTERMEDIATE_BYTES
;
96 else if (final_byte_p (ch
))
98 on_final_csi_char (ch
);
102 case state::CS_INTERMEDIATE_BYTES
:
103 /* Expect zero or more intermediate bytes. */
104 if (intermediate_byte_p (ch
))
106 m_intermediate_bytes
.push_back ((char)ch
);
109 else if (final_byte_p (ch
))
111 on_final_csi_char (ch
);
115 case state::WITHIN_OSC
:
116 /* Accumulate chars into m_osc_string, until we see an ST or a BEL. */
118 /* Check for ESC \, the String Terminator (aka "ST"). */
120 && m_osc_string
.size () > 0
121 && m_osc_string
.back () == '\033')
123 m_osc_string
.pop_back ();
124 on_final_osc_char ();
130 on_final_osc_char ();
133 m_osc_string
.push_back (ch
);
139 /* Test of handling U+FE0F VARIATION SELECTOR-16 to select the emoji
140 variation for the previous character. */
143 if (m_out
.size () > 0)
144 m_out
.back ().set_emoji_variant ();
148 if (cpp_is_combining_char (ch
))
150 if (m_out
.size () > 0)
152 m_out
.back ().add_combining_char (ch
);
156 /* By default, add the char. */
157 m_out
.push_back (styled_unichar (ch
, false, m_cur_style_id
));
161 void on_final_csi_char (cppchar_t ch
)
170 /* SGR control sequence. */
171 if (m_parameter_bytes
.empty ())
173 std::vector
<int> params (params_from_decimal ());
174 for (auto iter
= params
.begin (); iter
!= params
.end (); )
176 const int param
= *iter
;
180 /* Unrecognized SGR parameter. */
189 set_style_underscore ();
195 /* Named foreground colors. */
197 set_style_fg_color (style::named_color::BLACK
);
200 set_style_fg_color (style::named_color::RED
);
203 set_style_fg_color (style::named_color::GREEN
);
206 set_style_fg_color (style::named_color::YELLOW
);
209 set_style_fg_color (style::named_color::BLUE
);
212 set_style_fg_color (style::named_color::MAGENTA
);
215 set_style_fg_color (style::named_color::CYAN
);
218 set_style_fg_color (style::named_color::WHITE
);
221 /* 8-bit and 24-bit color */
225 const bool fg
= (param
== 38);
227 if (iter
!= params
.end ())
234 if (iter
!= params
.end ())
236 const uint8_t col
= *(iter
++);
238 set_style_fg_color (style::color (col
));
240 set_style_bg_color (style::color (col
));
245 if (iter
!= params
.end ())
247 const uint8_t r
= *(iter
++);
248 if (iter
!= params
.end ())
250 const uint8_t g
= *(iter
++);
251 if (iter
!= params
.end ())
253 const uint8_t b
= *(iter
++);
255 set_style_fg_color (style::color (r
,
259 set_style_bg_color (style::color (r
,
271 /* Named background colors. */
273 set_style_bg_color (style::named_color::BLACK
);
276 set_style_bg_color (style::named_color::RED
);
279 set_style_bg_color (style::named_color::GREEN
);
282 set_style_bg_color (style::named_color::YELLOW
);
285 set_style_bg_color (style::named_color::BLUE
);
288 set_style_bg_color (style::named_color::MAGENTA
);
291 set_style_bg_color (style::named_color::CYAN
);
294 set_style_bg_color (style::named_color::WHITE
);
297 /* Named foreground colors, bright. */
299 set_style_fg_color (style::color (style::named_color::BLACK
,
303 set_style_fg_color (style::color (style::named_color::RED
,
307 set_style_fg_color (style::color (style::named_color::GREEN
,
311 set_style_fg_color (style::color (style::named_color::YELLOW
,
315 set_style_fg_color (style::color (style::named_color::BLUE
,
319 set_style_fg_color (style::color (style::named_color::MAGENTA
,
323 set_style_fg_color (style::color (style::named_color::CYAN
,
327 set_style_fg_color (style::color (style::named_color::WHITE
,
331 /* Named foreground colors, bright. */
333 set_style_bg_color (style::color (style::named_color::BLACK
,
337 set_style_bg_color (style::color (style::named_color::RED
,
341 set_style_bg_color (style::color (style::named_color::GREEN
,
345 set_style_bg_color (style::color (style::named_color::YELLOW
,
349 set_style_bg_color (style::color (style::named_color::BLUE
,
353 set_style_bg_color (style::color (style::named_color::MAGENTA
,
357 set_style_bg_color (style::color (style::named_color::CYAN
,
361 set_style_bg_color (style::color (style::named_color::WHITE
,
370 m_parameter_bytes
.clear ();
371 m_intermediate_bytes
.clear ();
372 m_state
= state::START
;
375 void on_final_osc_char ()
377 if (!m_osc_string
.empty ())
379 switch (m_osc_string
[0])
384 /* Hyperlink support; see:
385 https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
386 We don't support params, so we expect either:
387 (a) "8;;URL" to begin a url (see pp_begin_url), or
388 (b) "8;;" to end a URL (see pp_end_url). */
389 if (m_osc_string
.size () >= 3
390 && m_osc_string
[1] == ';'
391 && m_osc_string
[2] == ';')
393 set_style_url (m_osc_string
.begin () + 3,
394 m_osc_string
.end ());
399 m_osc_string
.clear ();
400 m_state
= state::START
;
403 std::vector
<int> params_from_decimal () const
405 std::vector
<int> result
;
408 for (auto param_ch
: m_parameter_bytes
)
410 if (param_ch
>= '0' && param_ch
<= '9')
416 curr_int
+= param_ch
- '0';
422 result
.push_back (curr_int
);
428 result
.push_back (curr_int
);
432 void refresh_style_id ()
434 m_cur_style_id
= m_sm
.get_or_create_id (m_cur_style_obj
);
438 m_cur_style_obj
= style ();
441 void set_style_bold ()
443 m_cur_style_obj
.m_bold
= true;
446 void set_style_underscore ()
448 m_cur_style_obj
.m_underscore
= true;
451 void set_style_blink ()
453 m_cur_style_obj
.m_blink
= true;
456 void set_style_fg_color (style::color color
)
458 m_cur_style_obj
.m_fg_color
= color
;
461 void set_style_bg_color (style::color color
)
463 m_cur_style_obj
.m_bg_color
= color
;
466 void set_style_url (std::vector
<cppchar_t
>::iterator begin
,
467 std::vector
<cppchar_t
>::iterator end
)
469 // The empty string means "no URL"
470 m_cur_style_obj
.m_url
= std::vector
<cppchar_t
> (begin
, end
);
474 static bool parameter_byte_p (cppchar_t ch
)
476 return ch
>= 0x30 && ch
<= 0x3F;
479 static bool intermediate_byte_p (cppchar_t ch
)
481 return ch
>= 0x20 && ch
<= 0x2F;
484 static bool final_byte_p (cppchar_t ch
)
486 return ch
>= 0x40 && ch
<= 0x7E;
490 std::vector
<styled_unichar
> &m_out
;
492 style m_cur_style_obj
;
493 style::id_t m_cur_style_id
;
495 /* Handling of control sequences. */
500 /* After ESC, expecting '['. */
503 /* Expecting zero or more parameter bytes, an
504 intermediate byte, or a final byte. */
507 /* Expecting zero or more intermediate bytes, or a final byte. */
508 CS_INTERMEDIATE_BYTES
,
514 std::vector
<char> m_parameter_bytes
;
515 std::vector
<char> m_intermediate_bytes
;
516 std::vector
<cppchar_t
> m_osc_string
;
521 /* class text_art::styled_string. */
523 /* Construct a styled_string from STR.
524 STR is assumed to be UTF-8 encoded and 0-terminated.
526 Parse SGR formatting chars from being in-band (within in the sequence
527 of chars) to being out-of-band, as style elements.
528 We only support parsing the subset of SGR chars that can be emitted
529 by pretty-print.cc */
531 styled_string::styled_string (style_manager
&sm
, const char *str
)
534 escape_code_parser
parser (sm
, m_chars
);
536 /* We don't actually want the display widths here, but
537 it's an easy way to decode UTF-8. */
538 cpp_char_column_policy
policy (8, cpp_wcwidth
);
539 cpp_display_width_computation
dw (str
, strlen (str
), policy
);
542 cpp_decoded_char decoded_char
;
543 dw
.process_next_codepoint (&decoded_char
);
545 if (!decoded_char
.m_valid_ch
)
546 /* Skip bytes that aren't valid UTF-8. */
549 /* Decode SGR formatting. */
550 cppchar_t ch
= decoded_char
.m_ch
;
555 styled_string::styled_string (cppchar_t cppchar
, bool emoji
)
557 m_chars
.push_back (styled_unichar (cppchar
, emoji
, style::id_plain
));
561 styled_string::from_fmt_va (style_manager
&sm
,
562 printer_fn format_decoder
,
566 text_info
text (fmt
, args
, errno
);
568 pp_show_color (&pp
) = true;
569 pp
.url_format
= URL_FORMAT_DEFAULT
;
570 pp_format_decoder (&pp
) = format_decoder
;
571 pp_format (&pp
, &text
);
572 pp_output_formatted_text (&pp
);
573 styled_string
result (sm
, pp_formatted_text (&pp
));
578 styled_string::from_fmt (style_manager
&sm
,
579 printer_fn format_decoder
,
580 const char *fmt
, ...)
584 styled_string result
= from_fmt_va (sm
, format_decoder
, fmt
, &ap
);
590 styled_string::calc_canvas_width () const
593 for (auto ch
: m_chars
)
594 result
+= ch
.get_canvas_width ();
599 styled_string::append (const styled_string
&suffix
)
601 m_chars
.insert
<std::vector
<styled_unichar
>::const_iterator
> (m_chars
.end (),
607 styled_string::set_url (style_manager
&sm
, const char *url
)
609 for (auto& ch
: m_chars
)
611 const style
&existing_style
= sm
.get_style (ch
.get_style_id ());
612 style
with_url (existing_style
);
613 with_url
.set_style_url (url
);
614 ch
.m_style_id
= sm
.get_or_create_id (with_url
);
623 test_combining_chars ()
625 /* This really ought to be in libcpp, but we don't have
627 ASSERT_FALSE (cpp_is_combining_char (0));
628 ASSERT_FALSE (cpp_is_combining_char ('a'));
630 /* COMBINING BREVE (U+0306). */
631 ASSERT_TRUE (cpp_is_combining_char (0x0306));
633 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57. */
634 ASSERT_FALSE (cpp_is_combining_char (0x5B57));
636 /* U+FE0F VARIATION SELECTOR-16. */
637 ASSERT_FALSE (cpp_is_combining_char (0xFE0F));
644 styled_string
s (sm
, "");
645 ASSERT_EQ (s
.size (), 0);
646 ASSERT_EQ (s
.calc_canvas_width (), 0);
649 /* Test of a pure ASCII string with no escape codes. */
654 const char *c_str
= "hello world!";
656 styled_string
s (sm
, c_str
);
657 ASSERT_EQ (s
.size (), strlen (c_str
));
658 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (c_str
));
659 for (size_t i
= 0; i
< strlen (c_str
); i
++)
661 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)c_str
[i
]);
662 ASSERT_EQ (s
[i
].get_style_id (), 0);
666 /* Test of decoding UTF-8. */
671 /* U+03C0 "GREEK SMALL LETTER PI". */
672 const char * const pi_utf8
= "\xCF\x80";
675 styled_string
s (sm
, pi_utf8
);
676 ASSERT_EQ (s
.size (), 1);
677 ASSERT_EQ (s
.calc_canvas_width (), 1);
678 ASSERT_EQ (s
[0].get_code (), 0x03c0);
679 ASSERT_EQ (s
[0].emoji_variant_p (), false);
680 ASSERT_EQ (s
[0].double_width_p (), false);
681 ASSERT_EQ (s
[0].get_style_id (), 0);
684 /* Test of double-width character. */
687 test_emoji_from_utf8 ()
689 /* U+1F642 "SLIGHTLY SMILING FACE". */
690 const char * const emoji_utf8
= "\xF0\x9F\x99\x82";
693 styled_string
s (sm
, emoji_utf8
);
694 ASSERT_EQ (s
.size (), 1);
695 ASSERT_EQ (s
.calc_canvas_width (), 2);
696 ASSERT_EQ (s
[0].get_code (), 0x1f642);
697 ASSERT_EQ (s
[0].double_width_p (), true);
698 ASSERT_EQ (s
[0].get_style_id (), 0);
701 /* Test of handling U+FE0F VARIATION SELECTOR-16 to select the emoji
702 variation for the previous character. */
705 test_emoji_variant_from_utf8 ()
707 const char * const emoji_utf8
708 = (/* U+26A0 WARNING SIGN. */
710 /* U+FE0F VARIATION SELECTOR-16 (emoji variation selector). */
714 styled_string
s (sm
, emoji_utf8
);
715 ASSERT_EQ (s
.size (), 1);
716 ASSERT_EQ (s
.calc_canvas_width (), 1);
717 ASSERT_EQ (s
[0].get_code (), 0x26a0);
718 ASSERT_EQ (s
[0].emoji_variant_p (), true);
719 ASSERT_EQ (s
[0].double_width_p (), false);
720 ASSERT_EQ (s
[0].get_style_id (), 0);
724 test_emoji_from_codepoint ()
726 styled_string
s ((cppchar_t
)0x1f642);
727 ASSERT_EQ (s
.size (), 1);
728 ASSERT_EQ (s
.calc_canvas_width (), 2);
729 ASSERT_EQ (s
[0].get_code (), 0x1f642);
730 ASSERT_EQ (s
[0].double_width_p (), true);
731 ASSERT_EQ (s
[0].get_style_id (), 0);
735 test_from_mixed_width_utf8 ()
737 /* This UTF-8 string literal is of the form
738 before mojibake after
739 where the Japanese word "mojibake" is written as the following
740 four unicode code points:
741 U+6587 CJK UNIFIED IDEOGRAPH-6587
742 U+5B57 CJK UNIFIED IDEOGRAPH-5B57
743 U+5316 CJK UNIFIED IDEOGRAPH-5316
744 U+3051 HIRAGANA LETTER KE.
745 Each of these is 3 bytes wide when encoded in UTF-8, whereas the
746 "before" and "after" are 1 byte per unicode character. */
747 const char * const mixed_width_utf8
750 /* U+6587 CJK UNIFIED IDEOGRAPH-6587
751 UTF-8: 0xE6 0x96 0x87
752 C octal escaped UTF-8: \346\226\207. */
755 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57
756 UTF-8: 0xE5 0xAD 0x97
757 C octal escaped UTF-8: \345\255\227. */
760 /* U+5316 CJK UNIFIED IDEOGRAPH-5316
761 UTF-8: 0xE5 0x8C 0x96
762 C octal escaped UTF-8: \345\214\226. */
765 /* U+3051 HIRAGANA LETTER KE
766 UTF-8: 0xE3 0x81 0x91
767 C octal escaped UTF-8: \343\201\221. */
773 styled_string
s (sm
, mixed_width_utf8
);
774 ASSERT_EQ (s
.size (), 6 + 1 + 4 + 1 + 5);
775 ASSERT_EQ (sm
.get_num_styles (), 1);
777 // We expect the Japanese characters to be double width.
778 ASSERT_EQ (s
.calc_canvas_width (), 6 + 1 + (2 * 4) + 1 + 5);
780 ASSERT_EQ (s
[0].get_code (), 'b');
781 ASSERT_EQ (s
[0].double_width_p (), false);
782 ASSERT_EQ (s
[1].get_code (), 'e');
783 ASSERT_EQ (s
[2].get_code (), 'f');
784 ASSERT_EQ (s
[3].get_code (), 'o');
785 ASSERT_EQ (s
[4].get_code (), 'r');
786 ASSERT_EQ (s
[5].get_code (), 'e');
787 ASSERT_EQ (s
[6].get_code (), ' ');
788 ASSERT_EQ (s
[7].get_code (), 0x6587);
789 ASSERT_EQ (s
[7].double_width_p (), true);
790 ASSERT_EQ (s
[8].get_code (), 0x5B57);
791 ASSERT_EQ (s
[9].get_code (), 0x5316);
792 ASSERT_EQ (s
[10].get_code (), 0x3051);
793 ASSERT_EQ (s
[11].get_code (), ' ');
794 ASSERT_EQ (s
[12].get_code (), 'a');
795 ASSERT_EQ (s
[13].get_code (), 'f');
796 ASSERT_EQ (s
[14].get_code (), 't');
797 ASSERT_EQ (s
[15].get_code (), 'e');
798 ASSERT_EQ (s
[16].get_code (), 'r');
800 ASSERT_EQ (s
[0].get_style_id (), 0);
804 assert_style_urleq (const location
&loc
,
806 const char *expected_str
)
808 ASSERT_EQ_AT (loc
, s
.m_url
.size (), strlen (expected_str
));
809 for (size_t i
= 0; i
< s
.m_url
.size (); i
++)
810 ASSERT_EQ_AT (loc
, s
.m_url
[i
], (cppchar_t
)expected_str
[i
]);
813 #define ASSERT_STYLE_URLEQ(STYLE, EXPECTED_STR) \
814 assert_style_urleq ((SELFTEST_LOCATION), (STYLE), (EXPECTED_STR))
823 (sm
, "\33]8;;http://example.com\33\\This is a link\33]8;;\33\\");
824 const char *expected
= "This is a link";
825 ASSERT_EQ (s
.size (), strlen (expected
));
826 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (expected
));
827 ASSERT_EQ (sm
.get_num_styles (), 2);
828 for (size_t i
= 0; i
< strlen (expected
); i
++)
830 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)expected
[i
]);
831 ASSERT_EQ (s
[i
].get_style_id (), 1);
833 ASSERT_STYLE_URLEQ (sm
.get_style (1), "http://example.com");
840 (sm
, "\33]8;;http://example.com\aThis is a link\33]8;;\a");
841 const char *expected
= "This is a link";
842 ASSERT_EQ (s
.size (), strlen (expected
));
843 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (expected
));
844 ASSERT_EQ (sm
.get_num_styles (), 2);
845 for (size_t i
= 0; i
< strlen (expected
); i
++)
847 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)expected
[i
]);
848 ASSERT_EQ (s
[i
].get_style_id (), 1);
850 ASSERT_STYLE_URLEQ (sm
.get_style (1), "http://example.com");
858 styled_string
s (styled_string::from_fmt (sm
, NULL
, "%%i: %i", 42));
859 ASSERT_EQ (s
[0].get_code (), '%');
860 ASSERT_EQ (s
[1].get_code (), 'i');
861 ASSERT_EQ (s
[2].get_code (), ':');
862 ASSERT_EQ (s
[3].get_code (), ' ');
863 ASSERT_EQ (s
[4].get_code (), '4');
864 ASSERT_EQ (s
[5].get_code (), '2');
865 ASSERT_EQ (s
.size (), 6);
866 ASSERT_EQ (s
.calc_canvas_width (), 6);
872 auto_fix_quotes fix_quotes
;
873 open_quote
= "\xe2\x80\x98";
874 close_quote
= "\xe2\x80\x99";
877 styled_string
s (styled_string::from_fmt (sm
, NULL
, "%qs", "msg"));
878 ASSERT_EQ (sm
.get_num_styles (), 2);
879 ASSERT_EQ (s
[0].get_code (), 0x2018);
880 ASSERT_EQ (s
[0].get_style_id (), 0);
881 ASSERT_EQ (s
[1].get_code (), 'm');
882 ASSERT_EQ (s
[1].get_style_id (), 1);
883 ASSERT_EQ (s
[2].get_code (), 's');
884 ASSERT_EQ (s
[2].get_style_id (), 1);
885 ASSERT_EQ (s
[3].get_code (), 'g');
886 ASSERT_EQ (s
[3].get_style_id (), 1);
887 ASSERT_EQ (s
[4].get_code (), 0x2019);
888 ASSERT_EQ (s
[4].get_style_id (), 0);
889 ASSERT_EQ (s
.size (), 5);
892 // Test of parsing SGR codes.
895 test_from_str_with_bold ()
898 /* This is the result of pp_printf (pp, "%qs", "foo")
899 with auto_fix_quotes. */
900 styled_string
s (sm
, "`\33[01m\33[Kfoo\33[m\33[K'");
901 ASSERT_EQ (s
[0].get_code (), '`');
902 ASSERT_EQ (s
[0].get_style_id (), 0);
903 ASSERT_EQ (s
[1].get_code (), 'f');
904 ASSERT_EQ (s
[1].get_style_id (), 1);
905 ASSERT_EQ (s
[2].get_code (), 'o');
906 ASSERT_EQ (s
[2].get_style_id (), 1);
907 ASSERT_EQ (s
[3].get_code (), 'o');
908 ASSERT_EQ (s
[3].get_style_id (), 1);
909 ASSERT_EQ (s
[4].get_code (), '\'');
910 ASSERT_EQ (s
[4].get_style_id (), 0);
911 ASSERT_EQ (s
.size (), 5);
912 ASSERT_TRUE (sm
.get_style (1).m_bold
);
916 test_from_str_with_underscore ()
919 styled_string
s (sm
, "\33[04m\33[KA");
920 ASSERT_EQ (s
[0].get_code (), 'A');
921 ASSERT_EQ (s
[0].get_style_id (), 1);
922 ASSERT_TRUE (sm
.get_style (1).m_underscore
);
926 test_from_str_with_blink ()
929 styled_string
s (sm
, "\33[05m\33[KA");
930 ASSERT_EQ (s
[0].get_code (), 'A');
931 ASSERT_EQ (s
[0].get_style_id (), 1);
932 ASSERT_TRUE (sm
.get_style (1).m_blink
);
935 // Test of parsing SGR codes.
938 test_from_str_with_color ()
944 SGR_SEQ (COLOR_FG_RED
)
948 SGR_SEQ (COLOR_FG_GREEN
)
952 ASSERT_EQ (s
.size (), 5);
953 ASSERT_EQ (sm
.get_num_styles (), 3);
954 ASSERT_EQ (s
[0].get_code (), '0');
955 ASSERT_EQ (s
[0].get_style_id (), 0);
956 ASSERT_EQ (s
[1].get_code (), 'R');
957 ASSERT_EQ (s
[1].get_style_id (), 1);
958 ASSERT_EQ (s
[2].get_code (), '2');
959 ASSERT_EQ (s
[2].get_style_id (), 0);
960 ASSERT_EQ (s
[3].get_code (), 'G');
961 ASSERT_EQ (s
[3].get_style_id (), 2);
962 ASSERT_EQ (s
[4].get_code (), '4');
963 ASSERT_EQ (s
[4].get_style_id (), 0);
964 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::named_color::RED
);
965 ASSERT_EQ (sm
.get_style (2).m_fg_color
, style::named_color::GREEN
);
969 test_from_str_with_named_color ()
974 SGR_SEQ (COLOR_FG_BLACK
) "F"
975 SGR_SEQ (COLOR_FG_RED
) "F"
976 SGR_SEQ (COLOR_FG_GREEN
) "F"
977 SGR_SEQ (COLOR_FG_YELLOW
) "F"
978 SGR_SEQ (COLOR_FG_BLUE
) "F"
979 SGR_SEQ (COLOR_FG_MAGENTA
) "F"
980 SGR_SEQ (COLOR_FG_CYAN
) "F"
981 SGR_SEQ (COLOR_FG_WHITE
) "F"
982 SGR_SEQ (COLOR_FG_BRIGHT_BLACK
) "F"
983 SGR_SEQ (COLOR_FG_BRIGHT_RED
) "F"
984 SGR_SEQ (COLOR_FG_BRIGHT_GREEN
) "F"
985 SGR_SEQ (COLOR_FG_BRIGHT_YELLOW
) "F"
986 SGR_SEQ (COLOR_FG_BRIGHT_BLUE
) "F"
987 SGR_SEQ (COLOR_FG_BRIGHT_MAGENTA
) "F"
988 SGR_SEQ (COLOR_FG_BRIGHT_CYAN
) "F"
989 SGR_SEQ (COLOR_FG_BRIGHT_WHITE
) "F"
990 SGR_SEQ (COLOR_BG_BLACK
) "B"
991 SGR_SEQ (COLOR_BG_RED
) "B"
992 SGR_SEQ (COLOR_BG_GREEN
) "B"
993 SGR_SEQ (COLOR_BG_YELLOW
) "B"
994 SGR_SEQ (COLOR_BG_BLUE
) "B"
995 SGR_SEQ (COLOR_BG_MAGENTA
) "B"
996 SGR_SEQ (COLOR_BG_CYAN
) "B"
997 SGR_SEQ (COLOR_BG_WHITE
) "B"
998 SGR_SEQ (COLOR_BG_BRIGHT_BLACK
) "B"
999 SGR_SEQ (COLOR_BG_BRIGHT_RED
) "B"
1000 SGR_SEQ (COLOR_BG_BRIGHT_GREEN
) "B"
1001 SGR_SEQ (COLOR_BG_BRIGHT_YELLOW
) "B"
1002 SGR_SEQ (COLOR_BG_BRIGHT_BLUE
) "B"
1003 SGR_SEQ (COLOR_BG_BRIGHT_MAGENTA
) "B"
1004 SGR_SEQ (COLOR_BG_BRIGHT_CYAN
) "B"
1005 SGR_SEQ (COLOR_BG_BRIGHT_WHITE
) "B"));
1006 ASSERT_EQ (s
.size (), 33);
1007 for (size_t i
= 0; i
< s
.size (); i
++)
1008 ASSERT_EQ (s
[i
].get_style_id (), i
);
1009 for (size_t i
= 0; i
< 17; i
++)
1010 ASSERT_EQ (s
[i
].get_code (), 'F');
1011 for (size_t i
= 17; i
< 33; i
++)
1012 ASSERT_EQ (s
[i
].get_code (), 'B');
1016 test_from_str_with_8_bit_color ()
1020 styled_string
s (sm
,
1021 ("\e[38;5;232m\e[KF"));
1022 ASSERT_EQ (s
.size (), 1);
1023 ASSERT_EQ (s
[0].get_code (), 'F');
1024 ASSERT_EQ (s
[0].get_style_id (), 1);
1025 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::color (232));
1029 styled_string
s (sm
,
1030 ("\e[48;5;231m\e[KB"));
1031 ASSERT_EQ (s
.size (), 1);
1032 ASSERT_EQ (s
[0].get_code (), 'B');
1033 ASSERT_EQ (s
[0].get_style_id (), 1);
1034 ASSERT_EQ (sm
.get_style (1).m_bg_color
, style::color (231));
1039 test_from_str_with_24_bit_color ()
1043 styled_string
s (sm
,
1044 ("\e[38;2;243;250;242m\e[KF"));
1045 ASSERT_EQ (s
.size (), 1);
1046 ASSERT_EQ (s
[0].get_code (), 'F');
1047 ASSERT_EQ (s
[0].get_style_id (), 1);
1048 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::color (243, 250, 242));
1052 styled_string
s (sm
,
1053 ("\e[48;2;253;247;231m\e[KB"));
1054 ASSERT_EQ (s
.size (), 1);
1055 ASSERT_EQ (s
[0].get_code (), 'B');
1056 ASSERT_EQ (s
[0].get_style_id (), 1);
1057 ASSERT_EQ (sm
.get_style (1).m_bg_color
, style::color (253, 247, 231));
1062 test_from_str_combining_characters ()
1065 styled_string
s (sm
,
1066 /* CYRILLIC CAPITAL LETTER U (U+0423). */
1068 /* COMBINING BREVE (U+0306). */
1070 ASSERT_EQ (s
.size (), 1);
1071 ASSERT_EQ (s
[0].get_code (), 0x423);
1072 ASSERT_EQ (s
[0].get_combining_chars ().size (), 1);
1073 ASSERT_EQ (s
[0].get_combining_chars ()[0], 0x306);
1076 /* Run all selftests in this file. */
1079 text_art_styled_string_cc_tests ()
1081 test_combining_chars ();
1084 test_pi_from_utf8 ();
1085 test_emoji_from_utf8 ();
1086 test_emoji_variant_from_utf8 ();
1087 test_emoji_from_codepoint ();
1088 test_from_mixed_width_utf8 ();
1091 test_from_fmt_qs ();
1092 test_from_str_with_bold ();
1093 test_from_str_with_underscore ();
1094 test_from_str_with_blink ();
1095 test_from_str_with_color ();
1096 test_from_str_with_named_color ();
1097 test_from_str_with_8_bit_color ();
1098 test_from_str_with_24_bit_color ();
1099 test_from_str_combining_characters ();
1102 } // namespace selftest
1105 #endif /* #if CHECKING_P */