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
,
568 text
.args_ptr
= args
;
569 text
.format_spec
= fmt
;
571 pp_show_color (&pp
) = true;
572 pp
.url_format
= URL_FORMAT_DEFAULT
;
573 pp_format_decoder (&pp
) = format_decoder
;
574 pp_format (&pp
, &text
);
575 pp_output_formatted_text (&pp
);
576 styled_string
result (sm
, pp_formatted_text (&pp
));
581 styled_string::from_fmt (style_manager
&sm
,
582 printer_fn format_decoder
,
583 const char *fmt
, ...)
587 styled_string result
= from_fmt_va (sm
, format_decoder
, fmt
, &ap
);
593 styled_string::calc_canvas_width () const
596 for (auto ch
: m_chars
)
597 result
+= ch
.get_canvas_width ();
602 styled_string::append (const styled_string
&suffix
)
604 m_chars
.insert
<std::vector
<styled_unichar
>::const_iterator
> (m_chars
.end (),
610 styled_string::set_url (style_manager
&sm
, const char *url
)
612 for (auto& ch
: m_chars
)
614 const style
&existing_style
= sm
.get_style (ch
.get_style_id ());
615 style
with_url (existing_style
);
616 with_url
.set_style_url (url
);
617 ch
.m_style_id
= sm
.get_or_create_id (with_url
);
626 test_combining_chars ()
628 /* This really ought to be in libcpp, but we don't have
630 ASSERT_FALSE (cpp_is_combining_char (0));
631 ASSERT_FALSE (cpp_is_combining_char ('a'));
633 /* COMBINING BREVE (U+0306). */
634 ASSERT_TRUE (cpp_is_combining_char (0x0306));
636 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57. */
637 ASSERT_FALSE (cpp_is_combining_char (0x5B57));
639 /* U+FE0F VARIATION SELECTOR-16. */
640 ASSERT_FALSE (cpp_is_combining_char (0xFE0F));
647 styled_string
s (sm
, "");
648 ASSERT_EQ (s
.size (), 0);
649 ASSERT_EQ (s
.calc_canvas_width (), 0);
652 /* Test of a pure ASCII string with no escape codes. */
657 const char *c_str
= "hello world!";
659 styled_string
s (sm
, c_str
);
660 ASSERT_EQ (s
.size (), strlen (c_str
));
661 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (c_str
));
662 for (size_t i
= 0; i
< strlen (c_str
); i
++)
664 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)c_str
[i
]);
665 ASSERT_EQ (s
[i
].get_style_id (), 0);
669 /* Test of decoding UTF-8. */
674 /* U+03C0 "GREEK SMALL LETTER PI". */
675 const char * const pi_utf8
= "\xCF\x80";
678 styled_string
s (sm
, pi_utf8
);
679 ASSERT_EQ (s
.size (), 1);
680 ASSERT_EQ (s
.calc_canvas_width (), 1);
681 ASSERT_EQ (s
[0].get_code (), 0x03c0);
682 ASSERT_EQ (s
[0].emoji_variant_p (), false);
683 ASSERT_EQ (s
[0].double_width_p (), false);
684 ASSERT_EQ (s
[0].get_style_id (), 0);
687 /* Test of double-width character. */
690 test_emoji_from_utf8 ()
692 /* U+1F642 "SLIGHTLY SMILING FACE". */
693 const char * const emoji_utf8
= "\xF0\x9F\x99\x82";
696 styled_string
s (sm
, emoji_utf8
);
697 ASSERT_EQ (s
.size (), 1);
698 ASSERT_EQ (s
.calc_canvas_width (), 2);
699 ASSERT_EQ (s
[0].get_code (), 0x1f642);
700 ASSERT_EQ (s
[0].double_width_p (), true);
701 ASSERT_EQ (s
[0].get_style_id (), 0);
704 /* Test of handling U+FE0F VARIATION SELECTOR-16 to select the emoji
705 variation for the previous character. */
708 test_emoji_variant_from_utf8 ()
710 const char * const emoji_utf8
711 = (/* U+26A0 WARNING SIGN. */
713 /* U+FE0F VARIATION SELECTOR-16 (emoji variation selector). */
717 styled_string
s (sm
, emoji_utf8
);
718 ASSERT_EQ (s
.size (), 1);
719 ASSERT_EQ (s
.calc_canvas_width (), 1);
720 ASSERT_EQ (s
[0].get_code (), 0x26a0);
721 ASSERT_EQ (s
[0].emoji_variant_p (), true);
722 ASSERT_EQ (s
[0].double_width_p (), false);
723 ASSERT_EQ (s
[0].get_style_id (), 0);
727 test_emoji_from_codepoint ()
729 styled_string
s ((cppchar_t
)0x1f642);
730 ASSERT_EQ (s
.size (), 1);
731 ASSERT_EQ (s
.calc_canvas_width (), 2);
732 ASSERT_EQ (s
[0].get_code (), 0x1f642);
733 ASSERT_EQ (s
[0].double_width_p (), true);
734 ASSERT_EQ (s
[0].get_style_id (), 0);
738 test_from_mixed_width_utf8 ()
740 /* This UTF-8 string literal is of the form
741 before mojibake after
742 where the Japanese word "mojibake" is written as the following
743 four unicode code points:
744 U+6587 CJK UNIFIED IDEOGRAPH-6587
745 U+5B57 CJK UNIFIED IDEOGRAPH-5B57
746 U+5316 CJK UNIFIED IDEOGRAPH-5316
747 U+3051 HIRAGANA LETTER KE.
748 Each of these is 3 bytes wide when encoded in UTF-8, whereas the
749 "before" and "after" are 1 byte per unicode character. */
750 const char * const mixed_width_utf8
753 /* U+6587 CJK UNIFIED IDEOGRAPH-6587
754 UTF-8: 0xE6 0x96 0x87
755 C octal escaped UTF-8: \346\226\207. */
758 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57
759 UTF-8: 0xE5 0xAD 0x97
760 C octal escaped UTF-8: \345\255\227. */
763 /* U+5316 CJK UNIFIED IDEOGRAPH-5316
764 UTF-8: 0xE5 0x8C 0x96
765 C octal escaped UTF-8: \345\214\226. */
768 /* U+3051 HIRAGANA LETTER KE
769 UTF-8: 0xE3 0x81 0x91
770 C octal escaped UTF-8: \343\201\221. */
776 styled_string
s (sm
, mixed_width_utf8
);
777 ASSERT_EQ (s
.size (), 6 + 1 + 4 + 1 + 5);
778 ASSERT_EQ (sm
.get_num_styles (), 1);
780 // We expect the Japanese characters to be double width.
781 ASSERT_EQ (s
.calc_canvas_width (), 6 + 1 + (2 * 4) + 1 + 5);
783 ASSERT_EQ (s
[0].get_code (), 'b');
784 ASSERT_EQ (s
[0].double_width_p (), false);
785 ASSERT_EQ (s
[1].get_code (), 'e');
786 ASSERT_EQ (s
[2].get_code (), 'f');
787 ASSERT_EQ (s
[3].get_code (), 'o');
788 ASSERT_EQ (s
[4].get_code (), 'r');
789 ASSERT_EQ (s
[5].get_code (), 'e');
790 ASSERT_EQ (s
[6].get_code (), ' ');
791 ASSERT_EQ (s
[7].get_code (), 0x6587);
792 ASSERT_EQ (s
[7].double_width_p (), true);
793 ASSERT_EQ (s
[8].get_code (), 0x5B57);
794 ASSERT_EQ (s
[9].get_code (), 0x5316);
795 ASSERT_EQ (s
[10].get_code (), 0x3051);
796 ASSERT_EQ (s
[11].get_code (), ' ');
797 ASSERT_EQ (s
[12].get_code (), 'a');
798 ASSERT_EQ (s
[13].get_code (), 'f');
799 ASSERT_EQ (s
[14].get_code (), 't');
800 ASSERT_EQ (s
[15].get_code (), 'e');
801 ASSERT_EQ (s
[16].get_code (), 'r');
803 ASSERT_EQ (s
[0].get_style_id (), 0);
807 assert_style_urleq (const location
&loc
,
809 const char *expected_str
)
811 ASSERT_EQ_AT (loc
, s
.m_url
.size (), strlen (expected_str
));
812 for (size_t i
= 0; i
< s
.m_url
.size (); i
++)
813 ASSERT_EQ_AT (loc
, s
.m_url
[i
], (cppchar_t
)expected_str
[i
]);
816 #define ASSERT_STYLE_URLEQ(STYLE, EXPECTED_STR) \
817 assert_style_urleq ((SELFTEST_LOCATION), (STYLE), (EXPECTED_STR))
826 (sm
, "\33]8;;http://example.com\33\\This is a link\33]8;;\33\\");
827 const char *expected
= "This is a link";
828 ASSERT_EQ (s
.size (), strlen (expected
));
829 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (expected
));
830 ASSERT_EQ (sm
.get_num_styles (), 2);
831 for (size_t i
= 0; i
< strlen (expected
); i
++)
833 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)expected
[i
]);
834 ASSERT_EQ (s
[i
].get_style_id (), 1);
836 ASSERT_STYLE_URLEQ (sm
.get_style (1), "http://example.com");
843 (sm
, "\33]8;;http://example.com\aThis is a link\33]8;;\a");
844 const char *expected
= "This is a link";
845 ASSERT_EQ (s
.size (), strlen (expected
));
846 ASSERT_EQ (s
.calc_canvas_width (), (int)strlen (expected
));
847 ASSERT_EQ (sm
.get_num_styles (), 2);
848 for (size_t i
= 0; i
< strlen (expected
); i
++)
850 ASSERT_EQ (s
[i
].get_code (), (cppchar_t
)expected
[i
]);
851 ASSERT_EQ (s
[i
].get_style_id (), 1);
853 ASSERT_STYLE_URLEQ (sm
.get_style (1), "http://example.com");
861 styled_string
s (styled_string::from_fmt (sm
, NULL
, "%%i: %i", 42));
862 ASSERT_EQ (s
[0].get_code (), '%');
863 ASSERT_EQ (s
[1].get_code (), 'i');
864 ASSERT_EQ (s
[2].get_code (), ':');
865 ASSERT_EQ (s
[3].get_code (), ' ');
866 ASSERT_EQ (s
[4].get_code (), '4');
867 ASSERT_EQ (s
[5].get_code (), '2');
868 ASSERT_EQ (s
.size (), 6);
869 ASSERT_EQ (s
.calc_canvas_width (), 6);
875 auto_fix_quotes fix_quotes
;
876 open_quote
= "\xe2\x80\x98";
877 close_quote
= "\xe2\x80\x99";
880 styled_string
s (styled_string::from_fmt (sm
, NULL
, "%qs", "msg"));
881 ASSERT_EQ (sm
.get_num_styles (), 2);
882 ASSERT_EQ (s
[0].get_code (), 0x2018);
883 ASSERT_EQ (s
[0].get_style_id (), 0);
884 ASSERT_EQ (s
[1].get_code (), 'm');
885 ASSERT_EQ (s
[1].get_style_id (), 1);
886 ASSERT_EQ (s
[2].get_code (), 's');
887 ASSERT_EQ (s
[2].get_style_id (), 1);
888 ASSERT_EQ (s
[3].get_code (), 'g');
889 ASSERT_EQ (s
[3].get_style_id (), 1);
890 ASSERT_EQ (s
[4].get_code (), 0x2019);
891 ASSERT_EQ (s
[4].get_style_id (), 0);
892 ASSERT_EQ (s
.size (), 5);
895 // Test of parsing SGR codes.
898 test_from_str_with_bold ()
901 /* This is the result of pp_printf (pp, "%qs", "foo")
902 with auto_fix_quotes. */
903 styled_string
s (sm
, "`\33[01m\33[Kfoo\33[m\33[K'");
904 ASSERT_EQ (s
[0].get_code (), '`');
905 ASSERT_EQ (s
[0].get_style_id (), 0);
906 ASSERT_EQ (s
[1].get_code (), 'f');
907 ASSERT_EQ (s
[1].get_style_id (), 1);
908 ASSERT_EQ (s
[2].get_code (), 'o');
909 ASSERT_EQ (s
[2].get_style_id (), 1);
910 ASSERT_EQ (s
[3].get_code (), 'o');
911 ASSERT_EQ (s
[3].get_style_id (), 1);
912 ASSERT_EQ (s
[4].get_code (), '\'');
913 ASSERT_EQ (s
[4].get_style_id (), 0);
914 ASSERT_EQ (s
.size (), 5);
915 ASSERT_TRUE (sm
.get_style (1).m_bold
);
919 test_from_str_with_underscore ()
922 styled_string
s (sm
, "\33[04m\33[KA");
923 ASSERT_EQ (s
[0].get_code (), 'A');
924 ASSERT_EQ (s
[0].get_style_id (), 1);
925 ASSERT_TRUE (sm
.get_style (1).m_underscore
);
929 test_from_str_with_blink ()
932 styled_string
s (sm
, "\33[05m\33[KA");
933 ASSERT_EQ (s
[0].get_code (), 'A');
934 ASSERT_EQ (s
[0].get_style_id (), 1);
935 ASSERT_TRUE (sm
.get_style (1).m_blink
);
938 // Test of parsing SGR codes.
941 test_from_str_with_color ()
947 SGR_SEQ (COLOR_FG_RED
)
951 SGR_SEQ (COLOR_FG_GREEN
)
955 ASSERT_EQ (s
.size (), 5);
956 ASSERT_EQ (sm
.get_num_styles (), 3);
957 ASSERT_EQ (s
[0].get_code (), '0');
958 ASSERT_EQ (s
[0].get_style_id (), 0);
959 ASSERT_EQ (s
[1].get_code (), 'R');
960 ASSERT_EQ (s
[1].get_style_id (), 1);
961 ASSERT_EQ (s
[2].get_code (), '2');
962 ASSERT_EQ (s
[2].get_style_id (), 0);
963 ASSERT_EQ (s
[3].get_code (), 'G');
964 ASSERT_EQ (s
[3].get_style_id (), 2);
965 ASSERT_EQ (s
[4].get_code (), '4');
966 ASSERT_EQ (s
[4].get_style_id (), 0);
967 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::named_color::RED
);
968 ASSERT_EQ (sm
.get_style (2).m_fg_color
, style::named_color::GREEN
);
972 test_from_str_with_named_color ()
977 SGR_SEQ (COLOR_FG_BLACK
) "F"
978 SGR_SEQ (COLOR_FG_RED
) "F"
979 SGR_SEQ (COLOR_FG_GREEN
) "F"
980 SGR_SEQ (COLOR_FG_YELLOW
) "F"
981 SGR_SEQ (COLOR_FG_BLUE
) "F"
982 SGR_SEQ (COLOR_FG_MAGENTA
) "F"
983 SGR_SEQ (COLOR_FG_CYAN
) "F"
984 SGR_SEQ (COLOR_FG_WHITE
) "F"
985 SGR_SEQ (COLOR_FG_BRIGHT_BLACK
) "F"
986 SGR_SEQ (COLOR_FG_BRIGHT_RED
) "F"
987 SGR_SEQ (COLOR_FG_BRIGHT_GREEN
) "F"
988 SGR_SEQ (COLOR_FG_BRIGHT_YELLOW
) "F"
989 SGR_SEQ (COLOR_FG_BRIGHT_BLUE
) "F"
990 SGR_SEQ (COLOR_FG_BRIGHT_MAGENTA
) "F"
991 SGR_SEQ (COLOR_FG_BRIGHT_CYAN
) "F"
992 SGR_SEQ (COLOR_FG_BRIGHT_WHITE
) "F"
993 SGR_SEQ (COLOR_BG_BLACK
) "B"
994 SGR_SEQ (COLOR_BG_RED
) "B"
995 SGR_SEQ (COLOR_BG_GREEN
) "B"
996 SGR_SEQ (COLOR_BG_YELLOW
) "B"
997 SGR_SEQ (COLOR_BG_BLUE
) "B"
998 SGR_SEQ (COLOR_BG_MAGENTA
) "B"
999 SGR_SEQ (COLOR_BG_CYAN
) "B"
1000 SGR_SEQ (COLOR_BG_WHITE
) "B"
1001 SGR_SEQ (COLOR_BG_BRIGHT_BLACK
) "B"
1002 SGR_SEQ (COLOR_BG_BRIGHT_RED
) "B"
1003 SGR_SEQ (COLOR_BG_BRIGHT_GREEN
) "B"
1004 SGR_SEQ (COLOR_BG_BRIGHT_YELLOW
) "B"
1005 SGR_SEQ (COLOR_BG_BRIGHT_BLUE
) "B"
1006 SGR_SEQ (COLOR_BG_BRIGHT_MAGENTA
) "B"
1007 SGR_SEQ (COLOR_BG_BRIGHT_CYAN
) "B"
1008 SGR_SEQ (COLOR_BG_BRIGHT_WHITE
) "B"));
1009 ASSERT_EQ (s
.size (), 33);
1010 for (size_t i
= 0; i
< s
.size (); i
++)
1011 ASSERT_EQ (s
[i
].get_style_id (), i
);
1012 for (size_t i
= 0; i
< 17; i
++)
1013 ASSERT_EQ (s
[i
].get_code (), 'F');
1014 for (size_t i
= 17; i
< 33; i
++)
1015 ASSERT_EQ (s
[i
].get_code (), 'B');
1019 test_from_str_with_8_bit_color ()
1023 styled_string
s (sm
,
1024 ("\e[38;5;232m\e[KF"));
1025 ASSERT_EQ (s
.size (), 1);
1026 ASSERT_EQ (s
[0].get_code (), 'F');
1027 ASSERT_EQ (s
[0].get_style_id (), 1);
1028 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::color (232));
1032 styled_string
s (sm
,
1033 ("\e[48;5;231m\e[KB"));
1034 ASSERT_EQ (s
.size (), 1);
1035 ASSERT_EQ (s
[0].get_code (), 'B');
1036 ASSERT_EQ (s
[0].get_style_id (), 1);
1037 ASSERT_EQ (sm
.get_style (1).m_bg_color
, style::color (231));
1042 test_from_str_with_24_bit_color ()
1046 styled_string
s (sm
,
1047 ("\e[38;2;243;250;242m\e[KF"));
1048 ASSERT_EQ (s
.size (), 1);
1049 ASSERT_EQ (s
[0].get_code (), 'F');
1050 ASSERT_EQ (s
[0].get_style_id (), 1);
1051 ASSERT_EQ (sm
.get_style (1).m_fg_color
, style::color (243, 250, 242));
1055 styled_string
s (sm
,
1056 ("\e[48;2;253;247;231m\e[KB"));
1057 ASSERT_EQ (s
.size (), 1);
1058 ASSERT_EQ (s
[0].get_code (), 'B');
1059 ASSERT_EQ (s
[0].get_style_id (), 1);
1060 ASSERT_EQ (sm
.get_style (1).m_bg_color
, style::color (253, 247, 231));
1065 test_from_str_combining_characters ()
1068 styled_string
s (sm
,
1069 /* CYRILLIC CAPITAL LETTER U (U+0423). */
1071 /* COMBINING BREVE (U+0306). */
1073 ASSERT_EQ (s
.size (), 1);
1074 ASSERT_EQ (s
[0].get_code (), 0x423);
1075 ASSERT_EQ (s
[0].get_combining_chars ().size (), 1);
1076 ASSERT_EQ (s
[0].get_combining_chars ()[0], 0x306);
1079 /* Run all selftests in this file. */
1082 text_art_styled_string_cc_tests ()
1084 test_combining_chars ();
1087 test_pi_from_utf8 ();
1088 test_emoji_from_utf8 ();
1089 test_emoji_variant_from_utf8 ();
1090 test_emoji_from_codepoint ();
1091 test_from_mixed_width_utf8 ();
1094 test_from_fmt_qs ();
1095 test_from_str_with_bold ();
1096 test_from_str_with_underscore ();
1097 test_from_str_with_blink ();
1098 test_from_str_with_color ();
1099 test_from_str_with_named_color ();
1100 test_from_str_with_8_bit_color ();
1101 test_from_str_with_24_bit_color ();
1102 test_from_str_combining_characters ();
1105 } // namespace selftest
1108 #endif /* #if CHECKING_P */