r3bl_tui 0.7.2

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
Documentation
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
/*
 *   Copyright (c) 2023-2025 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

use std::fmt::{Display, Formatter, Result};

use smallvec::{SmallVec, smallvec};
use strum_macros::EnumCount;

use crate::{ASTColor, BufTextStorage, ColIndex, ColWidth, GCString, InlineString,
            InlineVec, PixelChar, SgrCode, TuiStyle, WriteToBuf, inline_string,
            tui_color,
            tui_style::tui_style_attrib::{Bold, Dim, Hidden, Italic, Reverse,
                                          Strikethrough, Underline}};

/// Please don't create this struct directly, use [`crate::ast()`], [`crate::ast_line`!],
/// [`crate::ast_lines`!] or the constructor functions like [`fg_red()`], [`fg_green()`],
/// [`fg_blue()`], etc.
///
/// The main struct that we have to consider is `AnsiStyledText` or `AST`. It has two
/// fields:
/// - `text` - the text to print.
/// - `styles` - a list of [`ASTStyle`] to apply to the text. This is owned in a stack
///   allocated buffer, which can spill to the heap if it gets larger than
///   `sizing::MAX_ANSI_STYLED_TEXT_STYLE_ATTRIB_SIZE`.
/// - Once created, either directly or using constructor functions like [`fg_red()`], you
///   can then use [`Self::bg_dark_gray()`] to add a background color to the text.
/// - If you want even more flexibility you can use constructor function [`fg_color()`]
///   and [`Self::bg_color()`] to create a styled text with a specific RGB color.
///
/// # Example usage:
///
/// ```
/// # use r3bl_tui::{
/// #     TuiStyle, tui_color, new_style,
/// #     ast, fg_red, dim, ASText, fg_color,
/// #     ASTStyle, ASTColor,
/// # };
///
/// // Use ast() to create a styled text. Use this.
/// let styled_text = ast("Hello", new_style!(bold));
/// println!("{styled_text}");
/// styled_text.println();
///
/// // Using the constructor functions.
/// let red_text = fg_red("This is red text.");
/// let red_text_on_dark_gray = red_text.bg_dark_gray();
/// println!("{red_text_on_dark_gray}");
/// red_text_on_dark_gray.println();
///
/// // Combine constructor functions.
/// let dim_red_text_on_dark_gray = dim("text").fg_color(tui_color!(255, 0, 0)).bg_color(tui_color!(50, 50, 50));
/// println!("{dim_red_text_on_dark_gray}");
/// dim_red_text_on_dark_gray.println();
///
/// // Flexible construction using RGB color codes.
/// let blue_text = fg_color(tui_color!(blue), "This is blue text.");
/// let blue_text_on_white = blue_text.bg_color(tui_color!(white));
/// println!("{blue_text_on_white}");
/// blue_text_on_white.println();
///
/// // Verbose struct construction (don't use this).
/// ASText {
///     text: "Print a formatted (bold, italic, underline) string w/ ANSI color codes.".into(),
///     styles: smallvec::smallvec![
///         ASTStyle::Bold,
///         ASTStyle::Italic,
///         ASTStyle::Underline,
///         ASTStyle::Foreground(ASTColor::Rgb((50, 50, 50).into())),
///         ASTStyle::Background(ASTColor::Rgb((100, 200, 1).into())),
///     ],
/// }
/// .println();
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnsiStyledText {
    pub text: InlineString,
    /// You can supply this directly, or use [`crate::new_style`!] to create a
    /// [`crate::TuiStyle`] and convert it to this type using `.into()`.
    pub styles: ASTextStyles,
}

// Type aliases for better readability.

pub type ASText = AnsiStyledText;
pub type ASTextLine = InlineVec<AnsiStyledText>;
pub type ASTextLines = InlineVec<ASTextLine>;
pub type ASTextStyles = sizing::InlineVecASTextStyles;

pub(in crate::core::ansi) mod sizing {
    use super::{ASTStyle, SmallVec};

    /// Attributes are: `color_fg`, `color_bg`, bold, dim, italic, underline, reverse,
    /// hidden, etc. which are in [`crate::ASTStyle`].
    pub const MAX_ANSI_STYLED_TEXT_STYLE_ATTRIB_SIZE: usize = 12;
    pub type InlineVecASTextStyles =
        SmallVec<[ASTStyle; MAX_ANSI_STYLED_TEXT_STYLE_ATTRIB_SIZE]>;
}

/// Easy to use constructor function, instead of creating a new [`AnsiStyledText`] struct
/// directly. If you need to assemble a bunch of these together, you can use
/// [`crate::ast_line!`] to create a list of them.
#[must_use]
pub fn ast(arg_text: impl AsRef<str>, arg_styles: impl Into<ASTextStyles>) -> ASText {
    ASText {
        text: arg_text.as_ref().into(),
        styles: arg_styles.into(),
    }
}

/// String together a bunch of [`AnsiStyledText`] structs into a single
/// [`crate::InlineVec<AnsiStyledText>`]. This is useful for creating a list of
/// [`AnsiStyledText`] structs that can be printed on a single line.
#[macro_export]
macro_rules! ast_line {
    (
        $( $ast_chunk:expr ),* $(,)?
    ) => {{
        use $crate::{InlineVec, ASTextLine};
        let mut acc: ASTextLine = InlineVec::new();
        $(
            acc.push($ast_chunk);
        )*
        acc
    }};
}

/// String together a bunch of formatted lines into a single
/// [`crate::InlineVec<InlineVec<AnsiStyledText>>`]. This is useful for assembling
/// multiline formatted text which is used in multi line headers, for example.
#[macro_export]
macro_rules! ast_lines {
    (
        $( $ast_line:expr ),* $(,)?
    ) => {{
        use $crate::{InlineVec, ASTextLines};
        let mut acc: ASTextLines = InlineVec::new();
        $(
            acc.push($ast_line);
        )*
        acc
    }};
}

pub mod ansi_styled_text_impl {
    use super::{ASText, AnsiStyledText, ColIndex, ColWidth, GCString, InlineString,
                InlineVec, PixelChar, TuiStyle, inline_string};

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
    pub struct ASTextConvertOptions {
        pub start: Option<ColIndex>,
        pub end: Option<ColIndex>,
    }

    impl From<ColWidth> for ASTextConvertOptions {
        fn from(col_width: ColWidth) -> Self {
            Self {
                start: Some(0.into()),
                end: Some(col_width.convert_to_col_index()),
            }
        }
    }

    impl AnsiStyledText {
        pub fn println(&self) {
            println!("{self}");
        }

        pub fn print(&self) {
            print!("{self}");
        }

        /// This is different from the [`std::fmt::Display`] trait implementation, because
        /// it doesn't allocate a new [`String`], but instead allocates an inline
        /// buffer on the stack.
        #[must_use]
        pub fn to_small_str(&self) -> InlineString { inline_string!("{self}") }

        /// This is a convenience function to clip the text to a certain display width.
        /// You can also clip it to any given start and end index (inclusive).
        #[must_use]
        pub fn clip(&self, arg_options: impl Into<ASTextConvertOptions>) -> ASText {
            let ir_text = self.convert(arg_options);

            let mut acc = InlineString::with_capacity(self.text.len());
            for pixel_char in &ir_text {
                if let PixelChar::PlainText {
                    display_char,
                    maybe_style: _,
                } = pixel_char
                {
                    use std::fmt::Write;
                    // We don't care about the result of this operation.
                    write!(acc, "{display_char}").ok();
                }
            }

            ASText {
                text: acc,
                styles: self.styles.clone(),
            }
        }

        /// Converts the text to a vector of [`PixelChar`]s. This is used for rendering
        /// the text on the screen.
        /// - To clip the text to a certain display width you can pass in the [`ColWidth`]
        ///   to this function.
        /// - To convert the entire text, just pass in [`ASTextConvertOptions::default()`]
        ///   function.
        /// - To convert a range of text, pass in the start and end indices. Note that it
        ///   will be inclusive (not the default Rust behavior), so the end index will be
        ///   included in the result.
        pub fn convert(
            &self,
            arg_options: impl Into<ASTextConvertOptions>,
        ) -> InlineVec<PixelChar> {
            let ASTextConvertOptions { start, end } = arg_options.into();

            // 1. Early return if the text is empty.
            if self.text.is_empty() {
                return InlineVec::new();
            }

            // 2. Convert self.styles to Option<TuiStyle>.
            let maybe_tui_style: Option<TuiStyle> = if self.styles.is_empty() {
                None
            } else {
                Some(self.styles.clone().into())
            };

            // 3. Iterate through characters and create PixelChar with maybe_tui_style.
            let pixel_chars = {
                let mut acc: InlineVec<PixelChar> =
                    InlineVec::with_capacity(self.text.len());
                let gc_string = GCString::from(&self.text);
                for item in &gc_string {
                    // Convert the grapheme cluster to a single char
                    // For multi-char graphemes, use the first char or fallback to
                    // replacement char
                    let display_char = item.chars().next().unwrap_or('�');
                    let pixel_char = PixelChar::PlainText {
                        display_char,
                        maybe_style: maybe_tui_style,
                    };
                    acc.push(pixel_char);
                }
                acc
            };

            // 4. Handle start and end inclusive indices, and slice the result.
            let end_index = match end {
                None => pixel_chars.len().saturating_sub(1),
                Some(it) => it.as_usize(),
            };
            let start_index = match start {
                None => 0,
                Some(it) => it.as_usize(),
            };

            // 4.1. Validate indices, and return empty InlineVec if invalid.
            if pixel_chars.is_empty()
                || start_index > end_index
                || start_index >= pixel_chars.len()
                || end_index >= pixel_chars.len()
            {
                return InlineVec::new();
            }

            // Slice and collect into a new InlineVec
            pixel_chars[start_index..=end_index]
                .iter()
                .copied()
                .collect()
        }
    }
}

// The following functions are convenience functions for providing ANSI attributes.

#[must_use]
pub fn bold(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Bold),
    }
}

#[must_use]
pub fn italic(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Italic),
    }
}

#[must_use]
pub fn underline(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Underline),
    }
}

#[must_use]
pub fn strikethrough(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Strikethrough),
    }
}

#[must_use]
pub fn dim(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Dim),
    }
}

#[must_use]
pub fn dim_underline(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Dim, ASTStyle::Underline),
    }
}

// The following function is a convenience function for providing any color.

#[must_use]
pub fn fg_color(arg_color: impl Into<ASTColor>, text: &str) -> ASText {
    ASText {
        text: text.into(),
        styles: smallvec!(ASTStyle::Foreground(arg_color.into())),
    }
}

// The following functions are convenience functions for providing ANSI colors.

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_dark_gray(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(236.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_black(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(0.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_yellow(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(226.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_green(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(34.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_blue(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(27.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_red(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(196.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_white(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(231.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_cyan(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(51.into()))),
    }
}

/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
#[must_use]
pub fn fg_magenta(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(ASTColor::Ansi(201.into()))),
    }
}

// The following colors are a convenience for using the [crate::tui_color!] macro.

#[must_use]
pub fn fg_medium_gray(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(medium_gray).into())),
    }
}

#[must_use]
pub fn fg_light_cyan(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(light_cyan).into())),
    }
}

#[must_use]
pub fn fg_light_purple(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(light_purple).into())),
    }
}

#[must_use]
pub fn fg_deep_purple(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(deep_purple).into())),
    }
}

#[must_use]
pub fn fg_soft_pink(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(soft_pink).into())),
    }
}

#[must_use]
pub fn fg_hot_pink(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(hot_pink).into())),
    }
}

#[must_use]
pub fn fg_light_yellow_green(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(
            crate::tui_color!(light_yellow_green).into()
        )),
    }
}

#[must_use]
pub fn fg_dark_teal(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(dark_teal).into())),
    }
}

#[must_use]
pub fn fg_bright_cyan(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(bright_cyan).into())),
    }
}

#[must_use]
pub fn fg_dark_purple(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(dark_purple).into())),
    }
}

#[must_use]
pub fn fg_sky_blue(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(sky_blue).into())),
    }
}

#[must_use]
pub fn fg_lavender(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(lavender).into())),
    }
}

#[must_use]
pub fn fg_dark_lizard_green(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(
            crate::tui_color!(dark_lizard_green).into()
        )),
    }
}

#[must_use]
pub fn fg_orange(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(orange).into())),
    }
}

#[must_use]
pub fn fg_silver_metallic(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(
            crate::tui_color!(silver_metallic).into()
        )),
    }
}

#[must_use]
pub fn fg_lizard_green(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(lizard_green).into())),
    }
}

#[must_use]
pub fn fg_pink(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(pink).into())),
    }
}

#[must_use]
pub fn fg_dark_pink(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(dark_pink).into())),
    }
}

#[must_use]
pub fn fg_frozen_blue(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(frozen_blue).into())),
    }
}

#[must_use]
pub fn fg_guards_red(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(guards_red).into())),
    }
}

#[must_use]
pub fn fg_slate_gray(text: impl AsRef<str>) -> ASText {
    ASText {
        text: text.as_ref().into(),
        styles: smallvec!(ASTStyle::Foreground(crate::tui_color!(slate_gray).into())),
    }
}

impl ASText {
    #[must_use]
    pub fn dim(mut self) -> Self {
        self.styles.push(ASTStyle::Dim);
        self
    }

    #[must_use]
    pub fn italic(mut self) -> Self {
        self.styles.push(ASTStyle::Italic);
        self
    }

    #[must_use]
    pub fn bold(mut self) -> Self {
        self.styles.push(ASTStyle::Bold);
        self
    }

    #[must_use]
    pub fn underline(mut self) -> Self {
        self.styles.push(ASTStyle::Underline);
        self
    }

    #[must_use]
    pub fn bg_color(mut self, arg_color: impl Into<ASTColor>) -> Self {
        let color: ASTColor = arg_color.into();
        self.styles.push(ASTStyle::Background(color));
        self
    }

    #[must_use]
    pub fn fg_color(mut self, arg_color: impl Into<ASTColor>) -> Self {
        let color: ASTColor = arg_color.into();
        self.styles.push(ASTStyle::Foreground(color));
        self
    }

    #[must_use]
    pub fn bg_cyan(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(ASTColor::Ansi(51.into())));
        self
    }

    #[must_use]
    pub fn bg_yellow(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(ASTColor::Ansi(226.into())));
        self
    }

    #[must_use]
    pub fn bg_green(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(ASTColor::Ansi(34.into())));
        self
    }

    #[must_use]
    pub fn bg_slate_gray(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(crate::tui_color!(slate_gray).into()));
        self
    }

    #[must_use]
    pub fn bg_dark_gray(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(ASTColor::Ansi(236.into())));
        self
    }

    #[must_use]
    pub fn bg_night_blue(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(tui_color!(night_blue).into()));
        self
    }

    #[must_use]
    pub fn bg_moonlight_blue(mut self) -> Self {
        self.styles
            .push(ASTStyle::Background(tui_color!(moonlight_blue).into()));
        self
    }
}

/// This enum isn't the same as the [`TuiStyle`] struct. This enum can only hold a single
/// variant. The [`TuiStyle`] struct can hold multiple variants. This is a low level enum
/// that shouldn't be used directly. It is best to use [`TuiStyle`] and
/// [`crate::new_style`!] to create a [`TuiStyle`] and convert it to this type using
/// `.into()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumCount)]
pub enum ASTStyle {
    Foreground(ASTColor),
    Background(ASTColor),
    Bold,
    Dim,
    Italic,
    Underline,
    Overline,
    RapidBlink,
    SlowBlink,
    Invert,
    Hidden,
    Strikethrough,
}

mod convert_vec_ast_style_to_tui_style {
    use super::{ASTStyle, ASTextStyles, Bold, Dim, Hidden, Italic, Reverse,
                Strikethrough, TuiStyle, Underline};

    impl From<ASTextStyles> for TuiStyle {
        fn from(styles: ASTextStyles) -> Self {
            let mut tui_style = TuiStyle::default();
            for style in styles {
                match style {
                    ASTStyle::Foreground(color) => {
                        tui_style.color_fg = Some(color.into());
                    }
                    ASTStyle::Background(color) => {
                        tui_style.color_bg = Some(color.into());
                    }
                    ASTStyle::Bold => tui_style.bold = Some(Bold),
                    ASTStyle::Dim => tui_style.dim = Some(Dim),
                    ASTStyle::Italic => tui_style.italic = Some(Italic),
                    ASTStyle::Underline => tui_style.underline = Some(Underline),
                    ASTStyle::Invert => tui_style.reverse = Some(Reverse),
                    ASTStyle::Hidden => tui_style.hidden = Some(Hidden),
                    ASTStyle::Strikethrough => {
                        tui_style.strikethrough = Some(Strikethrough);
                    }
                    // TuiStyle doesn't have direct equivalents for these:
                    ASTStyle::Overline | ASTStyle::RapidBlink | ASTStyle::SlowBlink => {}
                }
            }
            tui_style
        }
    }
}

mod convert_tui_style_to_vec_ast_style {
    use super::{ASTStyle, TuiStyle, sizing, sizing::InlineVecASTextStyles};

    impl From<TuiStyle> for sizing::InlineVecASTextStyles {
        fn from(tui_style: TuiStyle) -> Self {
            let mut styles = InlineVecASTextStyles::new();
            if tui_style.bold.is_some() {
                styles.push(ASTStyle::Bold);
            }
            if tui_style.dim.is_some() {
                styles.push(ASTStyle::Dim);
            }
            if tui_style.italic.is_some() {
                styles.push(ASTStyle::Italic);
            }
            if tui_style.underline.is_some() {
                styles.push(ASTStyle::Underline);
            }
            if tui_style.reverse.is_some() {
                styles.push(ASTStyle::Invert);
            }
            // Not supported:
            // - Overline,
            // - RapidBlink,
            // - SlowBlink,
            if tui_style.hidden.is_some() {
                styles.push(ASTStyle::Hidden);
            }
            if tui_style.strikethrough.is_some() {
                styles.push(ASTStyle::Strikethrough);
            }
            if let Some(color_fg) = tui_style.color_fg {
                styles.push(ASTStyle::Foreground(color_fg.into()));
            }
            if let Some(color_bg) = tui_style.color_bg {
                styles.push(ASTStyle::Background(color_bg.into()));
            }
            styles
        }
    }
}

mod style_impl {
    use super::{ASTStyle, BufTextStorage, Display, Formatter, Result, WriteToBuf};

    impl Display for ASTStyle {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
            // Delegate to WriteToBuf for consistency.
            let mut acc = BufTextStorage::new();
            self.write_to_buf(&mut acc)?;
            self.write_buf_to_fmt(&acc, f)
        }
    }
}

impl WriteToBuf for ASTStyle {
    fn write_to_buf(&self, buf: &mut BufTextStorage) -> Result {
        use super::{ColorSupport, TransformColor, global_color_support};

        // Helper function to convert color to appropriate SgrCode.
        fn color_to_sgr(
            color_support: ColorSupport,
            color: ASTColor,
            is_foreground: bool,
        ) -> SgrCode {
            match color_support {
                ColorSupport::Ansi256 => {
                    let ansi = color.as_ansi();
                    if is_foreground {
                        SgrCode::ForegroundAnsi256(ansi.index)
                    } else {
                        SgrCode::BackgroundAnsi256(ansi.index)
                    }
                }
                ColorSupport::Grayscale => {
                    let gray = color.as_grayscale();
                    if is_foreground {
                        SgrCode::ForegroundAnsi256(gray.index)
                    } else {
                        SgrCode::BackgroundAnsi256(gray.index)
                    }
                }
                _ => {
                    let rgb = color.as_rgb();
                    if is_foreground {
                        SgrCode::ForegroundRGB(rgb.red, rgb.green, rgb.blue)
                    } else {
                        SgrCode::BackgroundRGB(rgb.red, rgb.green, rgb.blue)
                    }
                }
            }
        }

        let color_support = global_color_support::detect();

        match self {
            ASTStyle::Foreground(color) => {
                color_to_sgr(color_support, *color, true).write_to_buf(buf)
            }
            ASTStyle::Background(color) => {
                color_to_sgr(color_support, *color, false).write_to_buf(buf)
            }
            ASTStyle::Bold => SgrCode::Bold.write_to_buf(buf),
            ASTStyle::Dim => SgrCode::Dim.write_to_buf(buf),
            ASTStyle::Italic => SgrCode::Italic.write_to_buf(buf),
            ASTStyle::Underline => SgrCode::Underline.write_to_buf(buf),
            ASTStyle::SlowBlink => SgrCode::SlowBlink.write_to_buf(buf),
            ASTStyle::RapidBlink => SgrCode::RapidBlink.write_to_buf(buf),
            ASTStyle::Invert => SgrCode::Invert.write_to_buf(buf),
            ASTStyle::Hidden => SgrCode::Hidden.write_to_buf(buf),
            ASTStyle::Strikethrough => SgrCode::Strikethrough.write_to_buf(buf),
            ASTStyle::Overline => SgrCode::Overline.write_to_buf(buf),
        }
    }
}

impl WriteToBuf for ASText {
    fn write_to_buf(&self, acc: &mut BufTextStorage) -> Result {
        // Write all styles to buffer.
        for style in &self.styles {
            style.write_to_buf(acc)?;
        }

        // Write text content.
        acc.push_str(&self.text);

        // Write reset code.
        SgrCode::Reset.write_to_buf(acc)?;

        Ok(())
    }
}

mod display_trait_impl {
    use super::{ASText, BufTextStorage, Display, Formatter, Result, WriteToBuf};

    impl Display for ASText {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
            // Use BufTextStorage buffer for building the complete output.
            let mut acc = BufTextStorage::new();
            self.write_to_buf(&mut acc)?;

            // Single write to formatter.
            f.write_str(&acc)
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use serial_test::serial;
    use smallvec::smallvec;

    use super::dim;
    use crate::{ASTColor, ASTStyle, ASText, ASTextStyles, ColIndex, ColorSupport,
                InlineVec, PixelChar, TuiColor, TuiStyle,
                ansi::sizing::InlineVecASTextStyles,
                ansi_styled_text::ansi_styled_text_impl::ASTextConvertOptions,
                global_color_support, tui_color,
                tui_style::tui_style_attrib::{Bold, Dim, Hidden, Italic, Reverse,
                                              Strikethrough, Underline},
                width};

    #[serial]
    #[test]
    fn test_convert_tui_style_to_vec_ast_style() {
        {
            let tui_style = TuiStyle {
                bold: Some(Bold),
                dim: None,
                italic: Some(Italic),
                underline: None,
                reverse: None,
                hidden: None,
                strikethrough: Some(Strikethrough),
                ..Default::default()
            };
            let ast_styles: InlineVecASTextStyles = tui_style.into();
            assert_eq!(
                ast_styles.as_ref(),
                &[ASTStyle::Bold, ASTStyle::Italic, ASTStyle::Strikethrough]
            );
        }

        {
            let tui_style = TuiStyle {
                bold: None,
                dim: Some(Dim),
                italic: None,
                underline: Some(Underline),
                reverse: Some(Reverse),
                hidden: Some(Hidden),
                strikethrough: None,
                ..Default::default()
            };
            let ast_styles: InlineVecASTextStyles = tui_style.into();
            assert_eq!(
                ast_styles.as_ref(),
                &[
                    ASTStyle::Dim,
                    ASTStyle::Underline,
                    ASTStyle::Invert,
                    ASTStyle::Hidden
                ]
            );
        }

        {
            let tui_style = TuiStyle {
                bold: Some(Bold),
                dim: Some(Dim),
                italic: Some(Italic),
                underline: Some(Underline),
                reverse: Some(Reverse),
                hidden: Some(Hidden),
                strikethrough: Some(Strikethrough),
                ..Default::default()
            };
            let ast_styles: InlineVecASTextStyles = tui_style.into();
            assert_eq!(
                ast_styles.as_ref(),
                &[
                    ASTStyle::Bold,
                    ASTStyle::Dim,
                    ASTStyle::Italic,
                    ASTStyle::Underline,
                    ASTStyle::Invert,
                    ASTStyle::Hidden,
                    ASTStyle::Strikethrough
                ]
            );
        }

        {
            let tui_style = TuiStyle {
                ..Default::default()
            };
            let ast_styles: InlineVecASTextStyles = tui_style.into();
            assert!(ast_styles.is_empty());
        }
    }

    #[serial]
    #[test]
    fn test_fg_color_on_bg_color() {
        let eg_1 = ASText {
            text: "Hello".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Rgb((0, 0, 0).into())),
            ),
        };
        println!("{eg_1:?}");
        println!("{eg_1}");
        assert_eq!(
            format!("{:?}", eg_1),
            r#"AnsiStyledText { text: "Hello", styles: [Bold, Foreground(Rgb(RgbValue { red: 0, green: 0, blue: 0 }))] }"#
        );

        let eg_2 = eg_1.bg_dark_gray();
        println!("{eg_2:?}");
        println!("{eg_2}");
        assert_eq!(
            format!("{:?}", eg_2),
            r#"AnsiStyledText { text: "Hello", styles: [Bold, Foreground(Rgb(RgbValue { red: 0, green: 0, blue: 0 })), Background(Ansi(AnsiValue { index: 236 }))] }"#
        );
    }

    #[serial]
    #[test]
    fn test_fg_bg_combo() {
        let eg_1 = dim("hello")
            .fg_color(tui_color!(0, 0, 0))
            .bg_color(tui_color!(1, 1, 1));
        println!("{eg_1:?}");
        println!("{eg_1}");
        assert_eq!(
            format!("{:?}", eg_1),
            r#"AnsiStyledText { text: "hello", styles: [Dim, Foreground(Rgb(RgbValue { red: 0, green: 0, blue: 0 })), Background(Rgb(RgbValue { red: 1, green: 1, blue: 1 }))] }"#
        );
    }

    #[serial]
    #[test]
    #[allow(clippy::missing_errors_doc)]
    fn test_formatted_string_creation_ansi256() -> Result<(), String> {
        global_color_support::set_override(ColorSupport::Ansi256);
        let eg_1 = ASText {
            text: "Hello".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Rgb((0, 0, 0).into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        assert_eq!(
            format!("{0}", eg_1),
            "\x1b[1m\x1b[38;5;16m\x1b[48;5;16mHello\x1b[0m".to_string()
        );

        let eg_2 = ASText {
            text: "World".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Ansi(150.into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        assert_eq!(
            format!("{0}", eg_2),
            "\x1b[1m\x1b[38;5;150m\x1b[48;5;16mWorld\x1b[0m".to_string()
        );

        Ok(())
    }

    #[serial]
    #[test]
    #[allow(clippy::missing_errors_doc)]
    fn test_formatted_string_creation_truecolor() -> Result<(), String> {
        global_color_support::set_override(ColorSupport::Truecolor);
        let eg_1 = ASText {
            text: "Hello".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Rgb((0, 0, 0).into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        assert_eq!(
            format!("{0}", eg_1),
            "\x1b[1m\x1b[38;2;0;0;0m\x1b[48;2;1;1;1mHello\x1b[0m".to_string()
        );

        let eg_2 = ASText {
            text: "World".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Ansi(150.into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        assert_eq!(
            format!("{0}", eg_2),
            "\x1b[1m\x1b[38;2;175;215;135m\x1b[48;2;1;1;1mWorld\x1b[0m".to_string()
        );

        Ok(())
    }

    #[serial]
    #[test]
    #[allow(clippy::missing_errors_doc)]
    fn test_formatted_string_creation_grayscale() -> Result<(), String> {
        global_color_support::set_override(ColorSupport::Grayscale);
        let eg_1 = ASText {
            text: "Hello".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Rgb((0, 0, 0).into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        println!("{:?}", format!("{0}", eg_1));

        assert_eq!(
            format!("{0}", eg_1),
            "\u{1b}[1m\u{1b}[38;5;16m\u{1b}[48;5;16mHello\u{1b}[0m".to_string()
        );

        let eg_2 = ASText {
            text: "World".into(),
            styles: smallvec!(
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Ansi(150.into())),
                ASTStyle::Background(ASTColor::Rgb((1, 1, 1).into())),
            ),
        };

        println!("{:?}", format!("{0}", eg_2));

        assert_eq!(
            format!("{0}", eg_2),
            "\u{1b}[1m\u{1b}[38;5;251m\u{1b}[48;5;16mWorld\u{1b}[0m".to_string()
        );

        Ok(())
    }

    #[serial]
    #[test]
    fn test_convert_vec_ast_style_to_tui_style() {
        // Test case 1: Mix of styles
        let ast_styles_1: ASTextStyles = smallvec![
            ASTStyle::Bold,
            ASTStyle::Foreground(ASTColor::Ansi(196.into())), // Red
            ASTStyle::Italic,
            ASTStyle::Background(ASTColor::Rgb((50, 50, 50).into())), // Dark Gray RGB
            ASTStyle::Underline,
            ASTStyle::Overline, // This should be ignored
        ];
        let expected_tui_style_1 = TuiStyle {
            bold: Some(Bold),
            italic: Some(Italic),
            underline: Some(Underline),
            color_fg: Some(TuiColor::Ansi(196.into())),
            color_bg: Some(TuiColor::Rgb((50, 50, 50).into())),
            ..Default::default()
        };
        let converted_tui_style_1: TuiStyle = ast_styles_1.into();
        assert_eq!(converted_tui_style_1, expected_tui_style_1);

        // Test case 2: Only attributes
        let ast_styles_2: ASTextStyles = smallvec![
            ASTStyle::Dim,
            ASTStyle::Strikethrough,
            ASTStyle::Hidden,
            ASTStyle::RapidBlink, // This should be ignored
        ];
        let expected_tui_style_2 = TuiStyle {
            dim: Some(Dim),
            strikethrough: Some(Strikethrough),
            hidden: Some(Hidden),
            ..Default::default()
        };
        let converted_tui_style_2: TuiStyle = ast_styles_2.into();
        assert_eq!(converted_tui_style_2, expected_tui_style_2);

        // Test case 3: Only colors
        let ast_styles_3: ASTextStyles = smallvec![
            ASTStyle::Foreground(ASTColor::Ansi(34.into())), // Green
            ASTStyle::Background(ASTColor::Ansi(226.into())), // Yellow
        ];
        let expected_tui_style_3 = TuiStyle {
            color_fg: Some(TuiColor::Ansi(34.into())),
            color_bg: Some(TuiColor::Ansi(226.into())),
            ..Default::default()
        };
        let converted_tui_style_3: TuiStyle = ast_styles_3.into();
        assert_eq!(converted_tui_style_3, expected_tui_style_3);

        // Test case 4: Empty styles
        let ast_styles_4: ASTextStyles = smallvec![];
        let expected_tui_style_4 = TuiStyle::default();
        let converted_tui_style_4: TuiStyle = ast_styles_4.into();
        assert_eq!(converted_tui_style_4, expected_tui_style_4);

        // Test case 5: Invert style
        let ast_styles_5: ASTextStyles = smallvec![ASTStyle::Invert];
        let expected_tui_style_5 = TuiStyle {
            reverse: Some(Reverse),
            ..Default::default()
        };
        let converted_tui_style_5: TuiStyle = ast_styles_5.into();
        assert_eq!(converted_tui_style_5, expected_tui_style_5);
    }

    #[serial]
    #[test]
    fn test_ast_convert_options_struct() {
        let options1 = ASTextConvertOptions {
            start: Some(ColIndex::new(5)),
            end: Some(ColIndex::new(10)),
        };
        assert_eq!(options1.start, Some(ColIndex::new(5)));
        assert_eq!(options1.end, Some(ColIndex::new(10)));

        let options2 = ASTextConvertOptions {
            start: None,
            end: None,
        };
        assert_eq!(options2.start, None);
        assert_eq!(options2.end, None);
    }

    #[serial]
    #[test]
    fn test_from_col_width_for_ast_convert_options() {
        let col_width = width(20);
        let options: ASTextConvertOptions = col_width.into();
        assert_eq!(options.start, Some(ColIndex::new(0)));
        // ColWidth 20 means indices 0-19.
        assert_eq!(options.end, Some(ColIndex::new(19)));

        let col_width_zero = width(0);
        let options_zero: ASTextConvertOptions = col_width_zero.into();
        assert_eq!(options_zero.start, Some(ColIndex::new(0)));
        // ColWidth(0) converts to ColIndex(0), which is technically index 0.
        assert_eq!(options_zero.end, Some(ColIndex::new(0)));
    }

    #[serial]
    #[test]
    fn test_ast_convert_method() {
        let tui_style = TuiStyle {
            bold: Some(Bold),
            color_fg: Some(TuiColor::Ansi(196.into())), // Red.
            ..Default::default()
        };
        let ast_style_vec: ASTextStyles = tui_style.into();

        let styled_text = ASText {
            text: "Hello World".into(),
            styles: ast_style_vec.clone(),
        };

        // Test case 1: Using From<ColWidth>
        {
            let col_width = width(5);
            let res: InlineVec<PixelChar> = styled_text.convert(col_width);
            assert_eq!(res.len(), 5); // "Hello"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'H',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[1],
                PixelChar::PlainText {
                    display_char: 'e',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[2],
                PixelChar::PlainText {
                    display_char: 'l',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[3],
                PixelChar::PlainText {
                    display_char: 'l',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[4],
                PixelChar::PlainText {
                    display_char: 'o',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 2: Convert full text (None, None)
        {
            let res: InlineVec<PixelChar> =
                styled_text.convert(ASTextConvertOptions::default());
            assert_eq!(res.len(), 11);
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'H',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[10],
                PixelChar::PlainText {
                    display_char: 'd',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 3: Convert partial text (start specified)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(6)),
                end: None,
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert_eq!(res.len(), 5); // "World"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'W',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[4],
                PixelChar::PlainText {
                    display_char: 'd',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 4: Convert partial text (end specified)
        {
            let opt = ASTextConvertOptions {
                start: None,
                end: Some(ColIndex::new(4)),
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert_eq!(res.len(), 5); // "Hello"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'H',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[4],
                PixelChar::PlainText {
                    display_char: 'o',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 5: Convert partial text (start and end specified)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(2)),
                end: Some(ColIndex::new(8)),
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert_eq!(res.len(), 7); // "llo Wor"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'l',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[6],
                PixelChar::PlainText {
                    display_char: 'r',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 6: Empty text
        {
            let empty_text = ASText {
                text: "".into(),
                styles: ast_style_vec.clone(),
            };
            let res: InlineVec<PixelChar> =
                empty_text.convert(ASTextConvertOptions::default());
            assert!(res.is_empty());
        }

        // Test case 7: No styles
        {
            let no_style_text = ASText {
                text: "Test".into(),
                styles: smallvec![],
            };
            let res: InlineVec<PixelChar> =
                no_style_text.convert(ASTextConvertOptions::default());
            assert_eq!(res.len(), 4);
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'T',
                    maybe_style: None
                }
            );
            assert_eq!(
                res[3],
                PixelChar::PlainText {
                    display_char: 't',
                    maybe_style: None
                }
            );
        }

        // Test case 8: Invalid range (start > end)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(5)),
                end: Some(ColIndex::new(3)),
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert!(res.is_empty());
        }

        // Test case 9: Invalid range (start out of bounds)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(11)),
                end: Some(ColIndex::new(12)),
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert!(res.is_empty());
        }

        // Test case 10: Invalid range (end out of bounds, but start is valid)
        // The current implementation returns empty if end >= len()
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(8)),
                end: Some(ColIndex::new(11)), /* index 11 is out of bounds for "Hello
                                               * World" (len 11) */
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert!(res.is_empty());
        }

        // Test case 10.1: Valid range, end is last index
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(8)),
                end: Some(ColIndex::new(10)), // index 10 is the last valid index
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert_eq!(res.len(), 3); // "rld"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'r',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[2],
                PixelChar::PlainText {
                    display_char: 'd',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 11: Single character range
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(6)),
                end: Some(ColIndex::new(6)),
            };
            let res: InlineVec<PixelChar> = styled_text.convert(opt);
            assert_eq!(res.len(), 1); // "W"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'W',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 12: Unicode characters
        {
            let unicode_text = ASText {
                text: "你好世界".into(), // "Hello World" in Chinese
                styles: ast_style_vec.clone(),
            };
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(1)),
                end: Some(ColIndex::new(2)),
            };
            let res: InlineVec<PixelChar> = unicode_text.convert(opt);
            assert_eq!(res.len(), 2); // "好世"
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: '好',
                    maybe_style: Some(tui_style)
                }
            );
            assert_eq!(
                res[1],
                PixelChar::PlainText {
                    display_char: '世',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 13: ColWidth(0)
        {
            let col_width_zero = width(0);
            let res: InlineVec<PixelChar> = styled_text.convert(col_width_zero);
            // start=0, end=0 -> should return the first char
            assert_eq!(res.len(), 1);
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'H',
                    maybe_style: Some(tui_style)
                }
            );
        }

        // Test case 14: ColWidth(1)
        {
            let col_width_one = width(1);
            let res: InlineVec<PixelChar> = styled_text.convert(col_width_one);
            // start=0, end=0 -> should return the first char
            assert_eq!(res.len(), 1);
            assert_eq!(
                res[0],
                PixelChar::PlainText {
                    display_char: 'H',
                    maybe_style: Some(tui_style)
                }
            );
        }
    }

    #[serial]
    #[test]
    fn test_ast_clip() {
        let tui_style = TuiStyle {
            bold: Some(Bold),
            color_fg: Some(TuiColor::Ansi(196.into())), // Red.
            ..Default::default()
        };
        let ast_style_vec: ASTextStyles = tui_style.into();

        let styled_text = ASText {
            text: "Hello World".into(),
            styles: ast_style_vec.clone(),
        };

        // Test case 1: Using From<ColWidth>
        {
            let col_width = width(4);
            let clipped_text = styled_text.clip(col_width);
            assert_eq!(clipped_text.text, "Hell");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 2: Clip full text (None, None)
        {
            let clipped_text = styled_text.clip(ASTextConvertOptions::default());
            assert_eq!(clipped_text.text, "Hello World");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 3: Clip partial text (start specified)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(6)),
                end: None,
            };
            let clipped_text = styled_text.clip(opt);
            assert_eq!(clipped_text.text, "World");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 4: Clip partial text (end specified)
        {
            let opt = ASTextConvertOptions {
                start: None,
                end: Some(ColIndex::new(4)),
            };
            let clipped_text = styled_text.clip(opt);
            assert_eq!(clipped_text.text, "Hello");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 5: Clip partial text (start and end specified)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(2)),
                end: Some(ColIndex::new(8)),
            };
            let clipped_text = styled_text.clip(opt);
            assert_eq!(clipped_text.text, "llo Wor");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 6: Empty text
        {
            let empty_text = ASText {
                text: "".into(),
                styles: ast_style_vec.clone(),
            };
            let clipped_text = empty_text.clip(ASTextConvertOptions::default());
            assert!(clipped_text.text.is_empty());
            assert_eq!(clipped_text.styles, empty_text.styles);
        }

        // Test case 7: No styles
        {
            let no_style_text = ASText {
                text: "Test".into(),
                styles: smallvec![],
            };
            let clipped_text = no_style_text.clip(ASTextConvertOptions::default());
            assert_eq!(clipped_text.text, "Test");
            assert_eq!(clipped_text.styles, no_style_text.styles);
        }

        // Test case 8: Invalid range (start > end)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(5)),
                end: Some(ColIndex::new(3)),
            };
            let clipped_text = styled_text.clip(opt);
            assert!(clipped_text.text.is_empty());
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 9: Invalid range (start out of bounds)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(11)),
                end: Some(ColIndex::new(12)),
            };
            let clipped_text = styled_text.clip(opt);
            assert!(clipped_text.text.is_empty());
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 10: Invalid range (end out of bounds, but start is valid)
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(8)),
                end: Some(ColIndex::new(11)), // index 11 is out of bounds
            };
            let clipped_text = styled_text.clip(opt);
            assert!(clipped_text.text.is_empty()); // convert returns empty for invalid end
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 10.1: Valid range, end is last index
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(8)),
                end: Some(ColIndex::new(10)), // index 10 is the last valid index
            };
            let clipped_text = styled_text.clip(opt);
            assert_eq!(clipped_text.text, "rld");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 11: Single character range
        {
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(6)),
                end: Some(ColIndex::new(6)),
            };
            let clipped_text = styled_text.clip(opt);
            assert_eq!(clipped_text.text, "W");
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 12: Unicode characters
        {
            let unicode_text = ASText {
                text: "你好世界".into(), // "Hello World" in Chinese
                styles: ast_style_vec.clone(),
            };
            let opt = ASTextConvertOptions {
                start: Some(ColIndex::new(1)),
                end: Some(ColIndex::new(2)),
            };
            let clipped_text = unicode_text.clip(opt);
            assert_eq!(clipped_text.text, "好世");
            assert_eq!(clipped_text.styles, unicode_text.styles);
        }

        // Test case 13: ColWidth(0)
        {
            let col_width_zero = width(0);
            let clipped_text = styled_text.clip(col_width_zero);
            assert_eq!(clipped_text.text, "H"); // start=0, end=0 -> first char
            assert_eq!(clipped_text.styles, styled_text.styles);
        }

        // Test case 14: ColWidth(1)
        {
            let col_width_one = width(1);
            let clipped_text = styled_text.clip(col_width_one);
            assert_eq!(clipped_text.text, "H"); // start=0, end=0 -> first char
            assert_eq!(clipped_text.styles, styled_text.styles);
        }
    }
}

#[cfg(test)]
mod bench_tests {
    extern crate test;
    use smallvec::smallvec;
    use test::Bencher;

    use super::*;

    // Benchmark data setup
    fn simple_text() -> ASText {
        ASText {
            text: "Hello, World!".into(),
            styles: smallvec![],
        }
    }

    fn single_style_text() -> ASText {
        ASText {
            text: "Hello, World!".into(),
            styles: smallvec![ASTStyle::Bold],
        }
    }

    fn multiple_styles_text() -> ASText {
        ASText {
            text: "Hello, World!".into(),
            styles: smallvec![ASTStyle::Bold, ASTStyle::Italic, ASTStyle::Underline,],
        }
    }

    fn colored_text() -> ASText {
        ASText {
            text: "Hello, World!".into(),
            styles: smallvec![
                ASTStyle::Foreground(ASTColor::Ansi(196.into())),
                ASTStyle::Background(ASTColor::Ansi(236.into())),
            ],
        }
    }

    fn rgb_colored_text() -> ASText {
        ASText {
            text: "Hello, World!".into(),
            styles: smallvec![
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Rgb((255, 0, 0).into())),
                ASTStyle::Background(ASTColor::Rgb((0, 0, 255).into())),
            ],
        }
    }

    fn complex_styled_text() -> ASText {
        ASText {
            text: "Hello, World! This is a longer text with more content.".into(),
            styles: smallvec![
                ASTStyle::Bold,
                ASTStyle::Italic,
                ASTStyle::Underline,
                ASTStyle::Foreground(ASTColor::Rgb((255, 128, 0).into())),
                ASTStyle::Background(ASTColor::Ansi(236.into())),
            ],
        }
    }

    fn long_text() -> ASText {
        ASText {
            // <!-- cspell:disable -->
            text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
                   Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                .into(),
            // <!-- cspell:enable -->
            styles: smallvec![
                ASTStyle::Bold,
                ASTStyle::Foreground(ASTColor::Ansi(34.into())),
            ],
        }
    }

    // Display benchmarks
    #[bench]
    fn bench_display_simple_text(b: &mut Bencher) {
        let ast = simple_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_single_style(b: &mut Bencher) {
        let ast = single_style_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_multiple_styles(b: &mut Bencher) {
        let ast = multiple_styles_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_ansi_colors(b: &mut Bencher) {
        let ast = colored_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_rgb_colors(b: &mut Bencher) {
        let ast = rgb_colored_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_complex_styled(b: &mut Bencher) {
        let ast = complex_styled_text();
        b.iter(|| format!("{ast}"));
    }

    #[bench]
    fn bench_display_long_text(b: &mut Bencher) {
        let ast = long_text();
        b.iter(|| format!("{ast}"));
    }

    // Benchmark creating styled text and displaying
    #[bench]
    fn bench_create_and_display_fg_red(b: &mut Bencher) {
        b.iter(|| {
            let ast = fg_red("Hello, World!");
            format!("{ast}")
        });
    }

    #[bench]
    fn bench_create_and_display_complex(b: &mut Bencher) {
        b.iter(|| {
            let ast = bold("Hello, World!")
                .fg_color(tui_color!(255, 0, 0))
                .bg_color(tui_color!(0, 0, 255));
            format!("{ast}")
        });
    }

    // Benchmark multiple ASText in sequence (simulating real usage)
    #[bench]
    fn bench_display_multiple_ast_sequence(b: &mut Bencher) {
        let texts = vec![
            fg_red("Error: "),
            bold("Failed to compile"),
            dim(" at line "),
            fg_yellow("42"),
        ];

        b.iter(|| {
            let mut result = String::new();
            for ast in &texts {
                result.push_str(&format!("{ast}"));
            }
            result
        });
    }
}