rich_rust 0.2.1

A Rust port of Python's Rich library for beautiful terminal output
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
//! Syntax highlighting renderable.
//!
//! This module provides syntax highlighting for code using the syntect library.
//! It supports numerous programming languages and themes out of the box.
//!
//! # Feature Flag
//!
//! This module requires the `syntax` feature to be enabled:
//!
//! ```toml
//! [dependencies]
//! rich_rust = { version = "0.1", features = ["syntax"] }
//! ```
//!
//! Or enable all optional features with:
//!
//! ```toml
//! rich_rust = { version = "0.1", features = ["full"] }
//! ```
//!
//! # Dependencies
//!
//! Enabling this feature adds the [`syntect`](https://docs.rs/syntect) crate as a dependency,
//! which provides the underlying syntax definitions and theme support.
//!
//! # Basic Usage
//!
//! ```rust,ignore
//! use rich_rust::renderables::syntax::Syntax;
//!
//! // Create a syntax-highlighted code block
//! let code = r#"fn main() { println!("Hello"); }"#;
//! let syntax = Syntax::new(code, "rust");
//! let segments = syntax.render(None)?;
//! ```
//!
//! # Line Numbers and Themes
//!
//! ```rust,ignore
//! use rich_rust::renderables::syntax::Syntax;
//!
//! let code = "def hello():\n    print('world')";
//! let syntax = Syntax::new(code, "python")
//!     .line_numbers(true)
//!     .start_line(10)  // Start numbering from line 10
//!     .theme("python-rich-default");
//!
//! let segments = syntax.render(None)?;
//! ```
//!
//! # Loading from Files
//!
//! ```rust,ignore
//! use rich_rust::renderables::syntax::Syntax;
//!
//! // Auto-detect language from file extension
//! let syntax = Syntax::from_path("src/main.rs")?
//!     .line_numbers(true)
//!     .theme("InspiredGitHub");
//! ```
//!
//! # Available Themes
//!
//! Call [`Syntax::available_themes()`] to list all built-in themes. Common themes include:
//! - `python-rich-default` (default compatibility mode)
//! - `base16-ocean.light`
//! - `InspiredGitHub`
//! - `Solarized (dark)`
//! - `Solarized (light)`
//!
//! # Supported Languages
//!
//! Call [`Syntax::available_languages()`] to list all supported languages. Syntect includes
//! syntax definitions for 100+ languages including Rust, Python, JavaScript, TypeScript,
//! Go, Java, C/C++, Ruby, and many more.
//!
//! # Known Limitations
//!
//! - **Theme loading**: Custom `.tmTheme` loading is opt-in and requires reading and parsing theme
//!   files from disk. Prefer reusing loaded theme sets to avoid repeated parsing.
//! - **Syntax definitions**: Custom `.sublime-syntax` loading is opt-in and requires reading and
//!   parsing syntax definitions from disk. Prefer reusing loaded syntax sets to avoid repeated parsing.
//! - **Large files**: Rendering very large files may be slow due to per-line highlighting.
//! - **Word wrap**: Wrap is supported (use `word_wrap(Some(width))`), and is whitespace-preserving
//!   (tuned for code rather than prose reflow).

use crate::cells;
use crate::color::Color;
use crate::segment::Segment;
use crate::style::Style;
use crate::text::Text;

use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::sync::LazyLock;

use syntect::easy::HighlightLines;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;

static SYNTAX_SET: LazyLock<SyntaxSet> = LazyLock::new(SyntaxSet::load_defaults_newlines);
static THEME_SET: LazyLock<ThemeSet> = LazyLock::new(ThemeSet::load_defaults);

const PYTHON_RICH_THEME: &str = "python-rich-default";
const PYTHON_RICH_FALLBACK_THEME: &str = "base16-ocean.dark";

/// Error type for syntax highlighting operations.
#[derive(Debug, Clone)]
pub enum SyntaxError {
    /// The specified language is not supported.
    UnknownLanguage(String),
    /// The specified theme is not found.
    UnknownTheme(String),
    /// Failed to read the file.
    IoError(String),
    /// Failed to load syntect assets from disk.
    LoadError(String),
}

impl std::fmt::Display for SyntaxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownLanguage(lang) => write!(f, "Unknown language: {lang}"),
            Self::UnknownTheme(theme) => write!(f, "Unknown theme: {theme}"),
            Self::IoError(msg) => write!(f, "IO error: {msg}"),
            Self::LoadError(msg) => write!(f, "Load error: {msg}"),
        }
    }
}

impl std::error::Error for SyntaxError {}

/// A syntax-highlighted code block renderable.
///
/// Uses syntect for syntax highlighting with support for themes,
/// line numbers, and background colors.
#[derive(Debug, Clone)]
pub struct Syntax {
    /// The source code to highlight.
    code: String,
    /// The language for syntax highlighting.
    language: String,
    /// Whether to show line numbers.
    line_numbers: bool,
    /// The starting line number (for excerpts).
    start_line: usize,
    /// The theme name to use.
    theme_name: String,
    /// Optional background color override.
    background_color: Option<Color>,
    /// Whether to show indentation guides.
    indent_guides: bool,
    /// Tab size for rendering.
    tab_size: usize,
    /// Optional word wrap width.
    word_wrap: Option<usize>,
    /// Style for the line number column.
    line_number_style: Style,
    /// Padding around the code block.
    padding: (usize, usize),
    /// Optional custom syntax set (loaded from user paths).
    custom_syntax_set: Option<Arc<SyntaxSet>>,
    /// Optional custom theme set (loaded from user paths).
    custom_theme_set: Option<Arc<ThemeSet>>,
}

impl Default for Syntax {
    fn default() -> Self {
        Self {
            code: String::new(),
            language: String::from("text"),
            line_numbers: false,
            start_line: 1,
            theme_name: String::from(PYTHON_RICH_THEME),
            background_color: None,
            indent_guides: false,
            tab_size: 4,
            word_wrap: None,
            line_number_style: Style::new().color_str("bright_black").unwrap_or_default(),
            padding: (0, 0),
            custom_syntax_set: None,
            custom_theme_set: None,
        }
    }
}

impl Syntax {
    /// Create a new syntax highlighted code block.
    ///
    /// # Arguments
    ///
    /// * `code` - The source code to highlight
    /// * `language` - The programming language (e.g., "rust", "python", "javascript")
    #[must_use]
    pub fn new(code: impl Into<String>, language: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            language: language.into(),
            ..Default::default()
        }
    }

    /// Load syntax from a file path, auto-detecting the language.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read.
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, SyntaxError> {
        let path = path.as_ref();
        let code = fs::read_to_string(path).map_err(|e| SyntaxError::IoError(e.to_string()))?;

        // Auto-detect language from extension
        let language = path
            .extension()
            .and_then(|ext| ext.to_str())
            .map_or_else(|| String::from("text"), Self::extension_to_language);

        Ok(Self::new(code, language))
    }

    /// Load a syntect syntax set from a folder of `.sublime-syntax` definitions.
    ///
    /// Use this to opt-in to custom / user-provided syntax definitions.
    ///
    /// # Errors
    ///
    /// Returns an error if the folder can't be read or the definitions can't be parsed.
    pub fn load_syntaxes_from_folder(
        folder: impl AsRef<Path>,
    ) -> Result<Arc<SyntaxSet>, SyntaxError> {
        SyntaxSet::load_from_folder(folder)
            .map(Arc::new)
            .map_err(|e| SyntaxError::LoadError(e.to_string()))
    }

    /// Load a syntect theme set from a folder of `.tmTheme` files.
    ///
    /// Use this to opt-in to custom / user-provided themes.
    ///
    /// # Errors
    ///
    /// Returns an error if the folder can't be read or the themes can't be parsed.
    pub fn load_themes_from_folder(folder: impl AsRef<Path>) -> Result<Arc<ThemeSet>, SyntaxError> {
        ThemeSet::load_from_folder(folder)
            .map(Arc::new)
            .map_err(|e| SyntaxError::LoadError(e.to_string()))
    }

    /// Provide a custom syntect syntax set (e.g. loaded via [`Self::load_syntaxes_from_folder`]).
    #[must_use]
    pub fn syntax_set(mut self, syntax_set: Arc<SyntaxSet>) -> Self {
        self.custom_syntax_set = Some(syntax_set);
        self
    }

    /// Provide a custom syntect theme set (e.g. loaded via [`Self::load_themes_from_folder`]).
    #[must_use]
    pub fn theme_set(mut self, theme_set: Arc<ThemeSet>) -> Self {
        self.custom_theme_set = Some(theme_set);
        self
    }

    /// Map file extension to language name.
    fn extension_to_language(ext: &str) -> String {
        match ext.to_lowercase().as_str() {
            "rs" => "rust",
            "py" => "python",
            "js" => "javascript",
            "ts" => "typescript",
            "jsx" => "javascript",
            "tsx" => "typescript",
            "rb" => "ruby",
            "go" => "go",
            "java" => "java",
            "c" => "c",
            "cpp" | "cxx" | "cc" => "c++",
            "h" | "hpp" => "c++",
            "cs" => "c#",
            "php" => "php",
            "swift" => "swift",
            "kt" | "kts" => "kotlin",
            "scala" => "scala",
            "sh" | "bash" => "bash",
            "zsh" => "zsh",
            "fish" => "fish",
            "ps1" => "powershell",
            "sql" => "sql",
            "html" | "htm" => "html",
            "css" => "css",
            "scss" => "scss",
            "less" => "less",
            "json" => "json",
            "yaml" | "yml" => "yaml",
            "toml" => "toml",
            "xml" => "xml",
            "md" | "markdown" => "markdown",
            "r" => "r",
            "lua" => "lua",
            "perl" | "pl" => "perl",
            "vim" => "vim",
            "dockerfile" => "dockerfile",
            "makefile" => "makefile",
            _ => ext,
        }
        .to_string()
    }

    /// Enable or disable line numbers.
    #[must_use]
    pub fn line_numbers(mut self, enabled: bool) -> Self {
        self.line_numbers = enabled;
        self
    }

    /// Set the starting line number (useful for code excerpts).
    #[must_use]
    pub fn start_line(mut self, line: usize) -> Self {
        self.start_line = line.max(1);
        self
    }

    /// Set the theme for syntax highlighting.
    ///
    /// Common themes: "python-rich-default", "base16-ocean.dark", "base16-ocean.light",
    /// `InspiredGitHub`, `Solarized (dark)`, `Solarized (light)`
    #[must_use]
    pub fn theme(mut self, theme_name: impl Into<String>) -> Self {
        self.theme_name = theme_name.into();
        self
    }

    /// Override the background color.
    #[must_use]
    pub fn background_color(mut self, color: Color) -> Self {
        self.background_color = Some(color);
        self
    }

    /// Enable or disable indentation guides.
    #[must_use]
    pub fn indent_guides(mut self, enabled: bool) -> Self {
        self.indent_guides = enabled;
        self
    }

    /// Set the tab size.
    #[must_use]
    pub fn tab_size(mut self, size: usize) -> Self {
        self.tab_size = size.max(1);
        self
    }

    /// Set word wrap width.
    #[must_use]
    pub fn word_wrap(mut self, width: Option<usize>) -> Self {
        self.word_wrap = width;
        self
    }

    /// Set the style for line numbers.
    #[must_use]
    pub fn line_number_style(mut self, style: Style) -> Self {
        self.line_number_style = style;
        self
    }

    /// Set padding around the code block (top/bottom, left/right).
    #[must_use]
    pub fn padding(mut self, vertical: usize, horizontal: usize) -> Self {
        self.padding = (vertical, horizontal);
        self
    }

    /// Get the list of available themes.
    #[must_use]
    pub fn available_themes() -> Vec<String> {
        let mut themes: Vec<String> = THEME_SET.themes.keys().cloned().collect();
        themes.push(PYTHON_RICH_THEME.to_string());
        themes.sort();
        themes.dedup();
        themes
    }

    /// Get the list of available languages.
    #[must_use]
    pub fn available_languages() -> Vec<String> {
        SYNTAX_SET
            .syntaxes()
            .iter()
            .map(|s| s.name.clone())
            .collect()
    }

    /// Render the syntax-highlighted code to segments.
    ///
    /// # Errors
    ///
    /// Returns an error if the theme or language is not found.
    pub fn render(&self, max_width: Option<usize>) -> Result<Vec<Segment<'_>>, SyntaxError> {
        let ps: &SyntaxSet = self.custom_syntax_set.as_deref().unwrap_or(&*SYNTAX_SET);
        let ts: &ThemeSet = self.custom_theme_set.as_deref().unwrap_or(&*THEME_SET);
        let use_python_rich_theme = self.uses_python_rich_theme();
        let use_python_rich_rust = use_python_rich_theme && Self::is_rust_language(&self.language);

        // Find the syntax definition
        let syntax = ps
            .find_syntax_by_token(&self.language)
            .or_else(|| ps.find_syntax_by_extension(&self.language))
            .ok_or_else(|| SyntaxError::UnknownLanguage(self.language.clone()))?;

        // Get the theme. The Python-Rich compatibility theme name is an alias; use a built-in
        // syntect theme as fallback for non-Rust languages and any behavior that still depends on
        // syntect internals.
        let resolved_theme_name = if use_python_rich_theme {
            PYTHON_RICH_FALLBACK_THEME
        } else {
            &self.theme_name
        };
        let theme = ts
            .themes
            .get(resolved_theme_name)
            .ok_or_else(|| SyntaxError::UnknownTheme(self.theme_name.clone()))?;

        let mut highlighter = HighlightLines::new(syntax, theme);
        let mut segments: Vec<Segment<'static>> = Vec::new();

        // Background used for padding/fill and for styling indent guides.
        let bg = if let Some(ref override_bg) = self.background_color {
            override_bg.clone()
        } else if use_python_rich_theme {
            Color::from_rgb(39, 40, 34)
        } else {
            let bg_color = theme
                .settings
                .background
                .unwrap_or(syntect::highlighting::Color::BLACK);
            Color::from_rgb(bg_color.r, bg_color.g, bg_color.b)
        };
        let base_bg_style = Style::new().bgcolor(bg.clone());
        let guide_style = base_bg_style.combine(&Style::new().dim());

        // Calculate line number width (digits only).
        let line_count = self.code.lines().count();
        let last_line = self.start_line.saturating_add(line_count.saturating_sub(1));
        let line_num_width = last_line.to_string().len();
        let line_number_padding = 2usize; // Rich-style line number gutter
        let line_prefix_width = if self.line_numbers {
            line_number_padding + line_num_width + 1 // +1 for trailing space after number
        } else {
            0
        };
        let line_number_style = base_bg_style.combine(&self.line_number_style);

        // If enabled, wrap the *code content* to this cell width (excluding gutter).
        //
        // Python Rich's `Syntax` wraps to the full available console width (minus any line number
        // gutter) and then applies padding externally, which can crop a character that no longer
        // fits once padding is added. To match that behavior, we *do not* subtract horizontal
        // padding here; instead we crop/pad the final segment stream to `max_width` below.
        //
        // Wrapping is whitespace-preserving (tuned for code rather than prose reflow).
        let wrap_width = self.word_wrap.and_then(|w| {
            if w == 0 {
                return None;
            }
            let cap = max_width.unwrap_or(usize::MAX);
            let available = cap.saturating_sub(line_prefix_width);
            if available == 0 {
                None
            } else {
                Some(w.min(available))
            }
        });

        // Add top padding
        for _ in 0..self.padding.0 {
            segments.push(Segment::line());
        }

        // Process each physical line (including an optional trailing newline per line).
        for (idx, line) in LinesWithEndings::from(&self.code).enumerate() {
            let line_num = self.start_line + idx;

            let normalized = line.replace("\r\n", "\n");
            let had_newline = normalized.ends_with('\n');
            let mut line_no_nl = normalized.as_str();
            if had_newline {
                line_no_nl = &line_no_nl[..line_no_nl.len().saturating_sub(1)];
            }

            // Expand tabs for stable display + wrapping.
            let tab_expanded = line_no_nl.replace('\t', &" ".repeat(self.tab_size));

            // Indentation guides: inject guide characters into leading whitespace, then style them
            // as dim while preserving the background.
            let leading_spaces = tab_expanded.chars().take_while(|c| *c == ' ').count();
            let line_for_highlight = if self.indent_guides && leading_spaces > 0 {
                apply_indent_guides(&tab_expanded, self.tab_size)
            } else {
                tab_expanded
            };

            let mut line_text = Text::new("");
            if use_python_rich_rust {
                for (text, style) in self.python_rich_rust_highlight(&line_for_highlight, &bg) {
                    line_text.append_styled(&text, style);
                }
            } else {
                // Highlight the line (no trailing newline).
                let ranges = highlighter
                    .highlight_line(&line_for_highlight, ps)
                    .unwrap_or_else(|_| {
                        vec![(
                            syntect::highlighting::Style::default(),
                            line_for_highlight.as_str(),
                        )]
                    });

                let mut col = 0usize;
                for (style, text) in ranges {
                    if text.is_empty() {
                        continue;
                    }
                    let rich_style = if use_python_rich_theme {
                        self.syntect_style_to_python_rich_compat(style, text, &bg, theme)
                    } else {
                        self.syntect_style_to_rich(style, theme)
                    };
                    append_syntax_text(
                        &mut line_text,
                        text,
                        &rich_style,
                        leading_spaces,
                        &mut col,
                        &guide_style,
                    );
                }
            }

            let visual_lines: Vec<Text> = if let Some(wrap_width) = wrap_width {
                wrap_text_preserving_whitespace(&line_text, wrap_width)
            } else {
                vec![line_text]
            };

            for (visual_idx, visual_line) in visual_lines.iter().cloned().enumerate() {
                // Left padding (styled with background so the block background is continuous).
                if self.padding.1 > 0 {
                    segments.push(Segment::new(
                        " ".repeat(self.padding.1),
                        Some(base_bg_style.clone()),
                    ));
                }

                // In Python Rich, horizontal padding is applied by an outer `Padding` renderable
                // that crops/pads the *inner* content to `width - left - right`, and then appends
                // the left/right pad segments. To match that behavior, we:
                // 1) render the line content (gutter + code),
                // 2) crop/pad it to the inner width (when a max width is known),
                // 3) append right padding afterwards.
                let mut content_line: Vec<Segment<'static>> = Vec::new();

                // Line number gutter (Rich-style: two-space gutter, number, trailing space).
                if self.line_numbers {
                    let gutter = if visual_idx == 0 {
                        format!(
                            "{}{:>width$} ",
                            " ".repeat(line_number_padding),
                            line_num,
                            width = line_num_width
                        )
                    } else {
                        " ".repeat(line_number_padding + line_num_width + 1)
                    };
                    content_line.push(Segment::new(gutter, Some(line_number_style.clone())));
                }

                // Highlighted code for this visual line.
                content_line.extend(visual_line.render("").into_iter().map(Segment::into_owned));

                if let Some(cap) = max_width.filter(|value| *value > 0)
                    && self.padding.1 > 0
                {
                    let inner_width = cap.saturating_sub(self.padding.1.saturating_mul(2));
                    content_line = crate::segment::adjust_line_length(
                        content_line,
                        inner_width,
                        Some(base_bg_style.clone()),
                        true,
                    );
                }

                segments.extend(content_line);

                // Right padding
                if self.padding.1 > 0 {
                    segments.push(Segment::new(
                        " ".repeat(self.padding.1),
                        Some(base_bg_style.clone()),
                    ));
                }

                // Newline between wrapped visual lines; preserve the original newline if present.
                let is_last_visual = visual_idx + 1 == visual_lines.len();
                if !is_last_visual || had_newline {
                    segments.push(Segment::line());
                }
            }
        }

        // Add bottom padding
        for _ in 0..self.padding.0 {
            segments.push(Segment::line());
        }

        if let Some(width) = max_width.filter(|value| *value > 0) {
            Ok(pad_segments_to_width(segments, width, Some(&base_bg_style)))
        } else {
            Ok(segments)
        }
    }

    /// Convert syntect style to rich Style.
    fn syntect_style_to_rich(&self, style: syntect::highlighting::Style, theme: &Theme) -> Style {
        let fg = Color::from_rgb(style.foreground.r, style.foreground.g, style.foreground.b);

        let bg = if let Some(ref override_bg) = self.background_color {
            override_bg.clone()
        } else {
            let bg_color = theme
                .settings
                .background
                .unwrap_or(syntect::highlighting::Color::BLACK);
            Color::from_rgb(bg_color.r, bg_color.g, bg_color.b)
        };

        let mut rich_style = Style::new().color(fg).bgcolor(bg);

        // Apply font style modifiers
        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::BOLD)
        {
            rich_style = rich_style.bold();
        }
        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::ITALIC)
        {
            rich_style = rich_style.italic();
        }
        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::UNDERLINE)
        {
            rich_style = rich_style.underline();
        }

        rich_style
    }

    fn syntect_style_to_python_rich_compat(
        &self,
        style: syntect::highlighting::Style,
        text: &str,
        background: &Color,
        theme: &Theme,
    ) -> Style {
        let mut rich_style = self.syntect_style_to_rich(style, theme);

        if self.background_color.is_none() {
            rich_style = rich_style.bgcolor(background.clone());
        }

        let mapped_color = python_rich_mapped_foreground(style.foreground, text);
        rich_style.color(mapped_color)
    }

    fn uses_python_rich_theme(&self) -> bool {
        self.theme_name == PYTHON_RICH_THEME
    }

    fn is_rust_language(language: &str) -> bool {
        matches!(language.to_ascii_lowercase().as_str(), "rust" | "rs")
    }

    fn python_rich_style(token_kind: RustTokenKind, background: &Color) -> Style {
        let fg = match token_kind {
            RustTokenKind::Keyword => Color::from_rgb(102, 217, 239),
            RustTokenKind::Function => Color::from_rgb(166, 226, 46),
            RustTokenKind::Operator => Color::from_rgb(255, 70, 137),
            RustTokenKind::Number => Color::from_rgb(174, 129, 255),
            RustTokenKind::String => Color::from_rgb(230, 219, 116),
            RustTokenKind::Comment => Color::from_rgb(117, 113, 94),
            RustTokenKind::Plain => Color::from_rgb(248, 248, 242),
        };

        let mut style = Style::new().color(fg).bgcolor(background.clone());
        if matches!(token_kind, RustTokenKind::Comment) {
            style = style.italic();
        }
        style
    }

    fn python_rich_rust_highlight(&self, line: &str, background: &Color) -> Vec<(String, Style)> {
        let mut out: Vec<(String, Style)> = Vec::new();
        let bytes = line.as_bytes();
        let mut i = 0usize;

        while i < bytes.len() {
            let b = bytes[i];

            if b.is_ascii_whitespace() {
                let start = i;
                i += 1;
                while i < bytes.len() && bytes[i].is_ascii_whitespace() {
                    i += 1;
                }
                out.push((
                    line[start..i].to_string(),
                    Self::python_rich_style(RustTokenKind::Plain, background),
                ));
                continue;
            }

            if b == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'/' {
                out.push((
                    line[i..].to_string(),
                    Self::python_rich_style(RustTokenKind::Comment, background),
                ));
                break;
            }

            if b == b'"' {
                out.push((
                    "\"".to_string(),
                    Self::python_rich_style(RustTokenKind::String, background),
                ));
                i += 1;

                let string_start = i;
                let mut escaped = false;
                while i < bytes.len() {
                    let current = bytes[i];
                    if escaped {
                        escaped = false;
                        i += 1;
                        continue;
                    }
                    if current == b'\\' {
                        escaped = true;
                        i += 1;
                        continue;
                    }
                    if current == b'"' {
                        break;
                    }
                    i += 1;
                }

                if string_start < i {
                    out.push((
                        line[string_start..i].to_string(),
                        Self::python_rich_style(RustTokenKind::String, background),
                    ));
                }

                if i < bytes.len() && bytes[i] == b'"' {
                    out.push((
                        "\"".to_string(),
                        Self::python_rich_style(RustTokenKind::String, background),
                    ));
                    i += 1;
                }
                continue;
            }

            if b.is_ascii_digit() {
                let start = i;
                i += 1;
                while i < bytes.len() && (bytes[i].is_ascii_digit() || bytes[i] == b'_') {
                    i += 1;
                }
                out.push((
                    line[start..i].to_string(),
                    Self::python_rich_style(RustTokenKind::Number, background),
                ));
                continue;
            }

            if is_identifier_start(b) {
                let start = i;
                i += 1;
                while i < bytes.len() && is_identifier_continue(bytes[i]) {
                    i += 1;
                }

                let mut end = i;
                if end < bytes.len() && bytes[end] == b'!' {
                    end += 1;
                    i = end;
                }

                let lexeme = &line[start..end];
                let keyword_lexeme = lexeme.strip_suffix('!').unwrap_or(lexeme);
                let next_non_whitespace = next_non_whitespace_byte(bytes, i);

                let kind = if is_rust_keyword(keyword_lexeme) {
                    RustTokenKind::Keyword
                } else if lexeme.ends_with('!') || next_non_whitespace == Some(b'(') {
                    RustTokenKind::Function
                } else {
                    RustTokenKind::Plain
                };

                out.push((
                    lexeme.to_string(),
                    Self::python_rich_style(kind, background),
                ));
                continue;
            }

            if is_operator_byte(b) {
                let start = i;
                i += 1;
                while i < bytes.len() && is_operator_byte(bytes[i]) {
                    i += 1;
                }
                out.push((
                    line[start..i].to_string(),
                    Self::python_rich_style(RustTokenKind::Operator, background),
                ));
                continue;
            }

            let ch_len = line[i..].chars().next().map_or(1, char::len_utf8);
            out.push((
                line[i..i + ch_len].to_string(),
                Self::python_rich_style(RustTokenKind::Plain, background),
            ));
            i += ch_len;
        }

        out
    }

    /// Get the highlighted code as a concatenated string (for testing/preview).
    #[must_use]
    pub fn plain_text(&self) -> String {
        self.code.clone()
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum RustTokenKind {
    Keyword,
    Function,
    Operator,
    Number,
    String,
    Comment,
    Plain,
}

fn is_identifier_start(byte: u8) -> bool {
    byte == b'_' || byte.is_ascii_alphabetic()
}

fn is_identifier_continue(byte: u8) -> bool {
    is_identifier_start(byte) || byte.is_ascii_digit()
}

fn is_operator_byte(byte: u8) -> bool {
    matches!(
        byte,
        b'=' | b'+' | b'-' | b'*' | b'/' | b'%' | b'!' | b'<' | b'>' | b'&' | b'|' | b'^'
    )
}

fn next_non_whitespace_byte(bytes: &[u8], mut index: usize) -> Option<u8> {
    while index < bytes.len() {
        if !bytes[index].is_ascii_whitespace() {
            return Some(bytes[index]);
        }
        index += 1;
    }
    None
}

fn is_rust_keyword(token: &str) -> bool {
    matches!(
        token,
        "as" | "async"
            | "await"
            | "break"
            | "const"
            | "continue"
            | "crate"
            | "dyn"
            | "else"
            | "enum"
            | "extern"
            | "false"
            | "fn"
            | "for"
            | "if"
            | "impl"
            | "in"
            | "let"
            | "loop"
            | "match"
            | "mod"
            | "move"
            | "mut"
            | "pub"
            | "ref"
            | "return"
            | "self"
            | "Self"
            | "static"
            | "struct"
            | "super"
            | "trait"
            | "true"
            | "type"
            | "unsafe"
            | "use"
            | "where"
            | "while"
    )
}

fn python_rich_mapped_foreground(fg: syntect::highlighting::Color, text: &str) -> Color {
    if token_is_operator(text) {
        return Color::from_rgb(255, 70, 137);
    }
    if token_is_quote_literal(text) {
        return Color::from_rgb(230, 219, 116);
    }

    match (fg.r, fg.g, fg.b) {
        // base16-ocean.dark -> Python Rich default syntax palette remap
        (180, 142, 173) => Color::from_rgb(102, 217, 239), // keyword
        (143, 161, 179) => Color::from_rgb(166, 226, 46),  // function/name
        (192, 197, 206) => Color::from_rgb(248, 248, 242), // plain/punctuation
        (163, 190, 140) => Color::from_rgb(230, 219, 116), // string
        (208, 135, 112) => Color::from_rgb(174, 129, 255), // number
        (191, 97, 106) => Color::from_rgb(255, 70, 137),   // operator-ish
        (96, 129, 139) => Color::from_rgb(117, 113, 94),   // comment-ish
        _ => Color::from_rgb(fg.r, fg.g, fg.b),
    }
}

fn token_is_operator(text: &str) -> bool {
    !text.is_empty()
        && text
            .chars()
            .all(|c| "=+-*/%<>!&|^~".contains(c) || c.is_whitespace())
        && text.chars().any(|c| "=+-*/%<>!&|^~".contains(c))
}

fn token_is_quote_literal(text: &str) -> bool {
    !text.is_empty() && text.chars().all(|c| c == '"' || c == '\'')
}

fn apply_indent_guides(line: &str, tab_size: usize) -> String {
    if tab_size == 0 {
        return line.to_string();
    }

    let leading_spaces = line.chars().take_while(|c| *c == ' ').count();
    if leading_spaces < tab_size {
        return line.to_string();
    }

    let mut out = String::with_capacity(line.len());
    for (col, ch) in line.chars().enumerate() {
        if col < leading_spaces && ch == ' ' {
            // Rich-style indent guides: show a guide at the start of each indent level:
            // 4 spaces -> "│   ", 8 spaces -> "│   │   ", etc.
            if col.is_multiple_of(tab_size) {
                out.push('');
            } else {
                out.push(' ');
            }
        } else {
            out.push(ch);
        }
    }
    out
}

fn append_syntax_text(
    out: &mut Text,
    text: &str,
    token_style: &Style,
    leading_spaces: usize,
    col: &mut usize,
    guide_style: &Style,
) {
    let mut buf = String::new();
    let mut buf_is_guide = false;
    let mut started = false;

    for ch in text.chars() {
        let is_guide = *col < leading_spaces && ch == '';

        if started && is_guide != buf_is_guide {
            let seg_style = if buf_is_guide {
                guide_style
            } else {
                token_style
            };
            out.append_styled(&std::mem::take(&mut buf), seg_style.clone());
        }

        if !started {
            started = true;
        }
        buf_is_guide = is_guide;

        buf.push(ch);
        *col = (*col).saturating_add(1);
    }

    if !buf.is_empty() {
        let seg_style = if buf_is_guide {
            guide_style
        } else {
            token_style
        };
        out.append_styled(&buf, seg_style.clone());
    }
}

fn wrap_text_preserving_whitespace(line: &Text, width: usize) -> Vec<Text> {
    if width == 0 {
        return vec![Text::new("")];
    }

    if line.cell_len() <= width {
        return vec![line.clone()];
    }

    let chars: Vec<char> = line.plain().chars().collect();
    let mut out = Vec::new();
    let mut start = 0usize;

    while start < chars.len() {
        let mut cell_width = 0usize;
        let mut i = start;
        let mut last_whitespace: Option<usize> = None;

        while i < chars.len() {
            let w = cells::get_character_cell_size(chars[i]);
            if cell_width + w > width {
                break;
            }
            cell_width += w;
            if chars[i].is_whitespace() {
                last_whitespace = Some(i);
            }
            i += 1;
        }

        if i == start {
            // Can't fit even a single character in the width (e.g. width=1, wide char),
            // so we force progress by taking 1 char.
            out.push(line.slice(start, (start + 1).min(chars.len())));
            start = (start + 1).min(chars.len());
            continue;
        }

        if i >= chars.len() {
            out.push(line.slice(start, chars.len()));
            break;
        }

        if let Some(ws) = last_whitespace.filter(|ws| *ws >= start) {
            // Wrap after whitespace, keeping the whitespace at the end of the previous line.
            let end = (ws + 1).min(chars.len());
            out.push(line.slice(start, end));
            start = end;
        } else {
            out.push(line.slice(start, i));
            start = i;
        }
    }

    if out.is_empty() {
        out.push(Text::new(""));
    }

    out
}

fn pad_segments_to_width(
    segments: Vec<Segment<'static>>,
    width: usize,
    fill_style: Option<&Style>,
) -> Vec<Segment<'static>> {
    let fill_style = fill_style.cloned();
    let mut out: Vec<Segment<'static>> = Vec::new();
    let mut line: Vec<Segment<'static>> = Vec::new();

    for segment in segments {
        if segment.is_control() {
            line.push(segment);
            continue;
        }

        let style = segment.style.clone();
        let text = segment.text;
        let text_ref = text.as_ref();
        let mut start = 0usize;

        for (idx, ch) in text_ref.char_indices() {
            if ch == '\n' {
                let part = &text_ref[start..idx];
                if !part.is_empty() {
                    line.push(Segment::new(part.to_string(), style.clone()));
                }

                // Python Rich uses `Segment.split_and_crop_lines(...)` downstream of padding; that
                // has the effect of cropping any characters that no longer fit once padding is
                // applied. We replicate that here by truncating/padding each final line to width.
                let adjusted = crate::segment::adjust_line_length(
                    std::mem::take(&mut line),
                    width,
                    fill_style.clone(),
                    true,
                );
                out.extend(adjusted);
                out.push(Segment::line());
                start = idx + 1;
            }
        }

        let tail = &text_ref[start..];
        if !tail.is_empty() {
            line.push(Segment::new(tail.to_string(), style));
        }
    }

    // If there's a trailing partial line (no newline), match prior behavior by padding/truncating
    // it to width and emitting a final newline.
    if !line.is_empty() {
        let adjusted = crate::segment::adjust_line_length(line, width, fill_style, true);
        out.extend(adjusted);
        out.push(Segment::line());
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn test_syntax_new() {
        let syntax = Syntax::new("let x = 1;", "rust");
        assert_eq!(syntax.code, "let x = 1;");
        assert_eq!(syntax.language, "rust");
        assert!(!syntax.line_numbers);
    }

    #[test]
    fn test_syntax_builder() {
        let syntax = Syntax::new("print('hello')", "python")
            .line_numbers(true)
            .start_line(10)
            .theme("base16-ocean.dark")
            .indent_guides(true)
            .tab_size(2)
            .padding(1, 2);

        assert!(syntax.line_numbers);
        assert_eq!(syntax.start_line, 10);
        assert_eq!(syntax.theme_name, "base16-ocean.dark");
        assert!(syntax.indent_guides);
        assert_eq!(syntax.tab_size, 2);
        assert_eq!(syntax.padding, (1, 2));
    }

    #[test]
    fn test_extension_to_language() {
        assert_eq!(Syntax::extension_to_language("rs"), "rust");
        assert_eq!(Syntax::extension_to_language("py"), "python");
        assert_eq!(Syntax::extension_to_language("js"), "javascript");
        assert_eq!(Syntax::extension_to_language("ts"), "typescript");
        assert_eq!(Syntax::extension_to_language("go"), "go");
        assert_eq!(Syntax::extension_to_language("unknown"), "unknown");
    }

    #[test]
    fn test_available_themes() {
        let themes = Syntax::available_themes();
        assert!(!themes.is_empty());
        assert!(themes.iter().any(|t| t.contains("base16")));
    }

    #[test]
    fn test_available_languages() {
        let langs = Syntax::available_languages();
        assert!(!langs.is_empty());
    }

    #[test]
    fn test_render_simple() {
        let code = r#"fn main() {
    println!("Hello, world!");
}"#;
        let syntax = Syntax::new(code, "rust");
        let result = syntax.render(None);
        assert!(result.is_ok());
        let segments = result.unwrap();
        assert!(!segments.is_empty());
    }

    #[test]
    fn test_render_with_line_numbers() {
        let code = "x = 1\ny = 2";
        let syntax = Syntax::new(code, "python").line_numbers(true);
        let result = syntax.render(None);
        assert!(result.is_ok());
        let segments = result.unwrap();
        // Should contain line number segments
        let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
        assert!(text.contains('1'));
        assert!(text.contains('2'));
    }

    #[test]
    fn test_render_unknown_language() {
        let syntax = Syntax::new("code", "nonexistent_lang_xyz");
        let result = syntax.render(None);
        assert!(
            matches!(result, Err(SyntaxError::UnknownLanguage(ref lang)) if lang == "nonexistent_lang_xyz")
        );
    }

    #[test]
    fn test_render_unknown_theme() {
        let syntax = Syntax::new("let x = 1", "rust").theme("nonexistent_theme_xyz");
        let result = syntax.render(None);
        assert!(
            matches!(result, Err(SyntaxError::UnknownTheme(ref theme)) if theme == "nonexistent_theme_xyz")
        );
    }

    #[test]
    fn test_plain_text() {
        let code = "fn main() {}";
        let syntax = Syntax::new(code, "rust");
        assert_eq!(syntax.plain_text(), code);
    }

    #[test]
    fn test_background_color_override() {
        let syntax = Syntax::new("code", "text").background_color(Color::parse("red").unwrap());
        assert!(syntax.background_color.is_some());
    }

    #[test]
    fn test_start_line_minimum() {
        let syntax = Syntax::new("code", "text").start_line(0);
        assert_eq!(syntax.start_line, 1); // Should be at minimum 1
    }

    #[test]
    fn test_tab_size_minimum() {
        let syntax = Syntax::new("code", "text").tab_size(0);
        assert_eq!(syntax.tab_size, 1); // Should be at minimum 1
    }

    #[test]
    fn test_padding_does_not_shift_lines() {
        // Use "rust" as a valid language (syntect doesn't have a "text" syntax)
        let syntax = Syntax::new("a\nb", "rust").padding(0, 2);
        let text = syntax
            .render(None)
            .expect("render should succeed")
            .iter()
            .map(|s| s.text.as_ref())
            .collect::<String>();
        let lines: Vec<&str> = text.lines().collect();
        assert_eq!(lines, vec!["  a  ", "  b  "]);
    }

    #[test]
    fn test_render_crlf_strips_carriage_returns() {
        let code = "let x = 1;\r\nlet y = 2;\r\n";
        let syntax = Syntax::new(code, "rust");
        let text = syntax
            .render(None)
            .expect("render should succeed")
            .iter()
            .map(|s| s.text.as_ref())
            .collect::<String>();
        assert!(!text.contains('\r'));
        assert!(text.contains("let x = 1;"));
        assert!(text.contains("let y = 2;"));
    }

    // =========================================================================
    // Runtime Theme Switching Tests (bd-hf2k)
    // =========================================================================

    #[test]
    fn test_theme_switching_via_builder() {
        // Test that calling theme() multiple times correctly updates the theme
        let syntax = Syntax::new("fn main() {}", "rust")
            .theme("base16-ocean.dark")
            .theme("InspiredGitHub"); // Switch to a different theme

        assert_eq!(syntax.theme_name, "InspiredGitHub");
    }

    #[test]
    fn test_different_themes_produce_different_styles() {
        let code = "fn main() { println!(\"hello\"); }";

        // Render with two different themes
        let syntax_dark = Syntax::new(code, "rust").theme("base16-ocean.dark");
        let syntax_light = Syntax::new(code, "rust").theme("InspiredGitHub");

        let segments_dark = syntax_dark.render(None).expect("dark theme render");
        let segments_light = syntax_light.render(None).expect("light theme render");

        // Collect styles from each render
        let styles_dark: Vec<_> = segments_dark
            .iter()
            .filter_map(|s| s.style.as_ref())
            .collect();
        let styles_light: Vec<_> = segments_light
            .iter()
            .filter_map(|s| s.style.as_ref())
            .collect();

        // Both should have styles (non-empty)
        assert!(
            !styles_dark.is_empty(),
            "dark theme should produce styled segments"
        );
        assert!(
            !styles_light.is_empty(),
            "light theme should produce styled segments"
        );

        // The themes should produce different background colors
        // (dark themes have dark backgrounds, light themes have light backgrounds)
        // This is a soft check - we just verify they're not identical
        let dark_first = styles_dark.first().unwrap();
        let light_first = styles_light.first().unwrap();
        assert_ne!(
            dark_first.to_string(),
            light_first.to_string(),
            "different themes should produce different styles"
        );
    }

    #[test]
    fn test_render_with_all_available_themes() {
        let code = "let x = 42;";
        let themes = Syntax::available_themes();

        // Ensure we have multiple themes to test
        assert!(
            themes.len() >= 2,
            "expected multiple themes, got {}",
            themes.len()
        );

        // Render with each available theme - all should succeed
        for theme_name in &themes {
            let syntax = Syntax::new(code, "rust").theme(theme_name);
            let result = syntax.render(None);
            assert!(
                result.is_ok(),
                "rendering with theme '{theme_name}' should succeed",
            );
        }
    }

    #[test]
    fn test_clone_and_change_theme() {
        let original = Syntax::new("x = 1", "python").theme("base16-ocean.dark");

        // Clone and change theme
        let modified = original.clone().theme("InspiredGitHub");

        // Original should be unchanged
        assert_eq!(original.theme_name, "base16-ocean.dark");
        assert_eq!(modified.theme_name, "InspiredGitHub");

        // Both should render successfully
        assert!(original.render(None).is_ok());
        assert!(modified.render(None).is_ok());
    }

    #[test]
    fn test_background_color_override_takes_precedence_over_theme() {
        let code = "fn main() {}";
        let custom_bg = Color::parse("#ff0000").expect("parse red");

        let syntax = Syntax::new(code, "rust")
            .theme("base16-ocean.dark")
            .background_color(custom_bg.clone());

        let segments = syntax.render(None).expect("render");

        // Find a segment with a style and check its background
        let styled_segment = segments.iter().find(|s| s.style.is_some());
        assert!(styled_segment.is_some(), "should have styled segments");

        // The background should be our custom color, not the theme's background
        if let Some(seg) = styled_segment {
            let style = seg.style.as_ref().unwrap();
            // The style should contain our custom background
            // Note: exact comparison depends on internal representation
            let style_str = style.to_string();
            assert!(
                style_str.contains("on #ff0000")
                    || style_str.contains("on rgb(255,0,0)")
                    || style_str.contains("on color("),
                "expected custom background in style, got: {style_str}",
            );
        }
    }

    #[test]
    fn test_default_theme_is_python_rich_default() {
        let syntax = Syntax::new("code", "rust");
        assert_eq!(syntax.theme_name, "python-rich-default");
    }

    #[test]
    fn test_python_rich_default_theme_is_available() {
        let themes = Syntax::available_themes();
        assert!(themes.iter().any(|name| name == "python-rich-default"));
    }

    #[test]
    fn test_python_rich_rust_token_styles() {
        let syntax = Syntax::new("", "rust");
        let background = Color::from_rgb(39, 40, 34);
        let highlighted = syntax.python_rich_rust_highlight("let x = 1234;", &background);

        let eq_style = highlighted
            .iter()
            .find(|(text, _)| text == "=")
            .map(|(_, style)| style.clone())
            .expect("operator token should exist");
        assert_eq!(eq_style.color, Some(Color::from_rgb(255, 70, 137)));
        assert_eq!(eq_style.bgcolor, Some(background.clone()));

        let number_style = highlighted
            .iter()
            .find(|(text, _)| text == "1234")
            .map(|(_, style)| style.clone())
            .expect("number token should exist");
        assert_eq!(number_style.color, Some(Color::from_rgb(174, 129, 255)));
        assert_eq!(number_style.bgcolor, Some(background));
    }

    #[test]
    fn test_python_rich_non_rust_operator_and_quote_styles() {
        let syntax = Syntax::new("x = \"hi\"", "python");
        let segments = syntax.render(Some(40)).expect("render should succeed");

        let eq_style = segments
            .iter()
            .find(|segment| segment.text == "=")
            .and_then(|segment| segment.style.clone())
            .expect("operator segment style should exist");
        assert_eq!(eq_style.color, Some(Color::from_rgb(255, 70, 137)));
        assert_eq!(eq_style.bgcolor, Some(Color::from_rgb(39, 40, 34)));

        let quote_style = segments
            .iter()
            .find(|segment| segment.text == "\"")
            .and_then(|segment| segment.style.clone())
            .expect("quote segment style should exist");
        assert_eq!(quote_style.color, Some(Color::from_rgb(230, 219, 116)));
        assert_eq!(quote_style.bgcolor, Some(Color::from_rgb(39, 40, 34)));
    }

    #[test]
    fn test_word_wrap_builder() {
        let syntax = Syntax::new("code", "rust").word_wrap(Some(80));
        assert_eq!(syntax.word_wrap, Some(80));

        let syntax2 = Syntax::new("code", "rust").word_wrap(None);
        assert_eq!(syntax2.word_wrap, None);
    }

    #[test]
    fn test_indent_guides_place_guide_at_indent_start() {
        let syntax = Syntax::new("    x\n", "python")
            .line_numbers(true)
            .indent_guides(true)
            .tab_size(4);

        let text: String = syntax
            .render(None)
            .expect("render should succeed")
            .iter()
            .map(|s| s.text.as_ref())
            .collect();

        // Rich-style: 4 spaces of indent become "│   ".
        assert!(
            text.contains("│   x"),
            "expected indent guide to render as '│   x', got: {text:?}"
        );
    }

    #[test]
    fn test_word_wrap_preserves_whitespace_and_continuation_gutter() {
        let code = "def long():\n    x = 'this is a very long string that should wrap'\n";
        let syntax = Syntax::new(code, "python")
            .line_numbers(true)
            .indent_guides(true)
            .tab_size(4)
            // Wrap to a narrow width so the string literal is forced to wrap.
            .word_wrap(Some(36))
            .padding(0, 0);

        let text: String = syntax
            .render(None)
            .expect("render should succeed")
            .iter()
            .map(|s| s.text.as_ref())
            .collect();

        // Ensure we wrapped across a whitespace boundary without deleting the whitespace:
        // we should see a space immediately before a newline.
        assert!(
            text.contains("string \n"),
            "expected trailing whitespace to be preserved before wrap, got: {text:?}"
        );

        // Wrapped continuation lines should be aligned under the code column (spaces where the
        // line number gutter was).
        assert!(
            text.contains("\n    that should"),
            "expected wrapped continuation line to start with the gutter width, got: {text:?}"
        );
    }

    #[test]
    fn test_line_number_style_builder() {
        use crate::style::Attributes;

        let custom_style = Style::new()
            .bold()
            .color_str("cyan")
            .expect("cyan should be a valid color");
        let syntax = Syntax::new("code", "rust").line_number_style(custom_style);

        // The line number style should be set with bold attribute
        assert!(
            syntax
                .line_number_style
                .attributes
                .contains(Attributes::BOLD)
        );
    }

    #[test]
    fn test_custom_theme_and_syntax_loading_from_folder() {
        // Create a unique temp folder (we don't delete it; this avoids any ambiguity
        // about destructive filesystem operations in agent environments).
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time")
            .as_nanos();
        let root = std::env::temp_dir().join(format!("rich_rust_syntect_custom_{nonce}"));
        std::fs::create_dir_all(&root).expect("create temp dir");

        // Minimal tmTheme (plist) that syntect can parse.
        let theme_path = root.join("TestTheme.tmTheme");
        let theme = r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>name</key>
  <string>TestTheme</string>
  <key>settings</key>
  <array>
    <dict>
      <key>settings</key>
      <dict>
        <key>background</key><string>#ffffff</string>
        <key>foreground</key><string>#000000</string>
      </dict>
    </dict>
  </array>
</dict>
</plist>
"#;
        std::fs::write(&theme_path, theme).expect("write theme");

        // Minimal sublime-syntax definition.
        let syntax_path = root.join("TestLang.sublime-syntax");
        let syntax_def = r"%YAML 1.2
---
name: TestLang
file_extensions:
  - testlang
scope: source.testlang
contexts:
  main:
    - match: '.+'
      scope: text.plain
...
";
        std::fs::write(&syntax_path, syntax_def).expect("write syntax");

        let theme_set = Syntax::load_themes_from_folder(&root).expect("load themes");
        assert!(
            theme_set.themes.contains_key("TestTheme"),
            "expected TestTheme in loaded theme set"
        );
        let syntax_set = Syntax::load_syntaxes_from_folder(&root).expect("load syntaxes");

        let code = "hello\n";
        let syntax = Syntax::new(code, "testlang")
            .syntax_set(syntax_set)
            .theme_set(theme_set)
            .theme("TestTheme");

        let rendered = syntax.render(Some(40)).expect("render with custom assets");
        let plain: String = rendered.iter().map(|s| s.text.as_ref()).collect();
        assert!(plain.contains("hello"));
    }
}