rusty-rich 0.3.0

Rich text and beautiful formatting in the terminal — a Rust port of Python's Rich library
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
//! Console — the central rendering engine. Equivalent to Rich's `console.py`.
//!
//! The `Console` is the main entry point for rendering. It manages terminal
//! detection, color system support, and dispatching renderables to produce
//! styled output.

use std::fmt;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};

use crate::align::AlignMethod;
use crate::color::{Color, ColorSystem};
use crate::segment::Segment;
use crate::style::Style;
use crate::text::Text;
use crate::theme::Theme;

// ---------------------------------------------------------------------------
// ConsoleDimensions
// ---------------------------------------------------------------------------

/// Size of the terminal in cells.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConsoleDimensions {
    pub width: usize,
    pub height: usize,
}

impl ConsoleDimensions {
    /// Detect the terminal size, falling back to 80x25 if detection fails.
    pub fn detect() -> Self {
        if let Some((w, h)) = terminal_size::terminal_size() {
            Self {
                width: w.0 as usize,
                height: h.0 as usize,
            }
        } else {
            Self {
                width: 80,
                height: 25,
            }
        }
    }
}

// ---------------------------------------------------------------------------
// OverflowMethod
// ---------------------------------------------------------------------------

/// How to handle text that overflows the available width.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OverflowMethod {
    /// Wrap text onto the next line.
    Fold,
    /// Crop text at the boundary.
    Crop,
    /// Crop and append "…".
    Ellipsis,
    /// Let text overflow (don't clip).
    Ignore,
}

// ---------------------------------------------------------------------------
// ConsoleOptions
// ---------------------------------------------------------------------------

/// Options passed to renderables during rendering.
#[derive(Debug, Clone)]
pub struct ConsoleOptions {
    /// Terminal size.
    pub size: ConsoleDimensions,
    /// True if output is a terminal.
    pub is_terminal: bool,
    /// The encoding (almost always UTF-8).
    pub encoding: String,
    /// Minimum render width.
    pub min_width: usize,
    /// Maximum render width.
    pub max_width: usize,
    /// Maximum height.
    pub max_height: usize,
    /// Override for text justification.
    pub justify: Option<AlignMethod>,
    /// Override for overflow handling.
    pub overflow: Option<OverflowMethod>,
    /// Disable text wrapping.
    pub no_wrap: bool,
    /// If true, use ASCII-only box characters.
    pub ascii_only: bool,
    /// If true, enable markup interpretation.
    pub markup: bool,
    /// If true, enable syntax highlighting of strings.
    pub highlight: bool,
    /// Optional fixed height for the renderable.
    pub height: Option<usize>,
    /// For legacy Windows console.
    pub legacy_windows: bool,
}

impl Default for ConsoleOptions {
    fn default() -> Self {
        Self {
            size: ConsoleDimensions::detect(),
            is_terminal: true,
            encoding: "utf-8".into(),
            min_width: 1,
            max_width: 80,
            max_height: 25,
            justify: None,
            overflow: None,
            no_wrap: false,
            ascii_only: false,
            markup: true,
            highlight: true,
            height: None,
            legacy_windows: false,
        }
    }
}

impl ConsoleOptions {
    /// Update the max width.
    pub fn update_width(&self, max_width: usize) -> Self {
        let mut opts = self.clone();
        opts.max_width = max_width;
        opts
    }

    /// Update the height.
    pub fn update_height(&self, height: usize) -> Self {
        let mut opts = self.clone();
        opts.height = Some(height);
        opts
    }

    /// Shrink the max width by an amount (for padding).
    pub fn shrink_width(&self, amount: usize) -> Self {
        let mut opts = self.clone();
        opts.max_width = opts.max_width.saturating_sub(amount);
        opts
    }
}

// ---------------------------------------------------------------------------
// Renderable trait
// ---------------------------------------------------------------------------

/// A single item in a render result — either a final `Segment` or a nested
/// renderable that will be recursively flattened by `Console::render()`.
///
/// Equivalent to Python Rich's `RenderResult = Iterable[Union[Segment, RenderableType]]`.
#[derive(Clone)]
pub enum RenderItem {
    /// A fully-rendered [`Segment`].
    Segment(Segment),
    /// A nested [`DynRenderable`] that will be recursively flattened.
    Nested(DynRenderable),
}

impl fmt::Debug for RenderItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Segment(s) => write!(f, "Segment({})", &s.text),
            Self::Nested(_) => write!(f, "Nested(...)"),
        }
    }
}

impl From<Segment> for RenderItem {
    fn from(s: Segment) -> Self { Self::Segment(s) }
}

impl From<DynRenderable> for RenderItem {
    fn from(r: DynRenderable) -> Self { Self::Nested(r) }
}

/// The result of rendering: a list of lines, each line being a list of
/// segments.  Also carries an optional `items` list for recursive rendering.
#[derive(Debug, Clone)]
pub struct RenderResult {
    /// Flat line-oriented segments (backward-compatible).
    pub lines: Vec<Vec<Segment>>,
    /// Optional render items for recursive flattening. When present,
    /// `Console::render()` recurses into nested renderables.
    pub items: Vec<RenderItem>,
}

impl RenderResult {
    /// Create an empty [`RenderResult`].
    pub fn new() -> Self {
        Self { lines: Vec::new(), items: Vec::new() }
    }

    /// Create a [`RenderResult`] from a plain text string.
    ///
    /// The text becomes a single line with one segment.
    pub fn from_text(text: &str) -> Self {
        Self {
            lines: vec![vec![Segment::new(text)]],
            items: vec![RenderItem::Segment(Segment::new(text))],
        }
    }

    /// Create a [`RenderResult`] from a list of [`Segment`]s on a single line.
    pub fn from_segments(segments: Vec<Segment>) -> Self {
        let items: Vec<RenderItem> = segments.iter().map(|s| RenderItem::Segment(s.clone())).collect();
        Self { lines: vec![segments], items }
    }

    /// Create a [`RenderResult`] from pre-computed lines of [`Segment`]s.
    pub fn from_lines(lines: Vec<Vec<Segment>>) -> Self {
        Self { lines, items: Vec::new() }
    }

    /// Create a [`RenderResult`] from [`RenderItem`]s for recursive flattening.
    pub fn from_items(items: Vec<RenderItem>) -> Self {
        Self { lines: Vec::new(), items }
    }

    /// Push a segment item.
    pub fn push_item(&mut self, item: impl Into<RenderItem>) {
        self.items.push(item.into());
    }

    /// Push a nested renderable for recursive flattening.
    pub fn push_renderable(&mut self, r: impl Renderable + Send + Sync + 'static) {
        self.items.push(RenderItem::Nested(DynRenderable::new(r)));
    }

    /// Recursively flatten items into segments using the given options.
    /// This is called by `Console::render()` to resolve nested renderables.
    pub fn flatten(&self, options: &ConsoleOptions) -> Vec<Segment> {
        let mut out: Vec<Segment> = Vec::new();
        flatten_items(&self.items, options, &mut out);
        // Also flatten lines for backward compat
        if out.is_empty() {
            for line in &self.lines {
                for seg in line {
                    out.push(seg.clone());
                }
            }
        }
        out
    }

    /// Flatten all segments into a single ANSI string.
    pub fn to_ansi(&self) -> String {
        let mut out = String::new();
        // Use items if present, otherwise fall back to lines
        if !self.items.is_empty() {
            let flat = self.flatten(&ConsoleOptions::default());
            for seg in &flat {
                out.push_str(&seg.to_ansi());
            }
        } else {
            for line in &self.lines {
                for seg in line {
                    out.push_str(&seg.to_ansi());
                }
            }
        }
        out
    }
}

/// Recursively flatten `RenderItem`s into a `Vec<Segment>`.
fn flatten_items(items: &[RenderItem], options: &ConsoleOptions, out: &mut Vec<Segment>) {
    for item in items {
        match item {
            RenderItem::Segment(seg) => out.push(seg.clone()),
            RenderItem::Nested(renderable) => {
                let nested = renderable.render(options);
                flatten_items(&nested.items, options, out);
            }
        }
    }
}

/// Trait for anything that can be rendered to the console.
///
/// Equivalent to `__rich_console__` in Python Rich.
pub trait Renderable {
    /// Render this object into a [`RenderResult`] using the provided options.
    ///
    /// Implementing types produce [`Segment`]s or nested [`Renderable`]s
    /// that are recursively flattened by [`Console::render`].
    fn render(&self, options: &ConsoleOptions) -> RenderResult;

    /// Optional width-measurement hook (equivalent to `__rich_measure__`).
    /// Override to provide min/max width constraints for layout.
    fn measure(&self, _options: &ConsoleOptions) -> Option<crate::measure::Measurement> {
        None
    }
}

// -- Implementations for common types ---------------------------------------

/// Allows a [`String`] to be used as a renderable.
impl Renderable for String {
    fn render(&self, options: &ConsoleOptions) -> RenderResult {
        self.as_str().render(options)
    }
}

/// Allows a [`&str`] to be used as a renderable (rendered as plain text).
impl Renderable for &str {
    fn render(&self, _options: &ConsoleOptions) -> RenderResult {
        RenderResult::from_text(self)
    }
}

/// Allows a [`Text`] object to be used as a renderable.
impl Renderable for Text {
    fn render(&self, _options: &ConsoleOptions) -> RenderResult {
        let rendered = self.render();
        // Simple: just treat the rendered ANSI string as one segment per line
        let lines: Vec<Vec<Segment>> = rendered
            .lines()
            .map(|l| vec![Segment::new(l)])
            .collect();
        RenderResult { lines, items: Vec::new() }
    }
}

/// A wrapper that provides `Clone` + `Debug` for trait-object renderables.
///
/// [`DynRenderable`] boxes any [`Renderable`] behind an [`Arc`] so it can be
/// stored in collections like [`Group`] and [`Panel`](crate::Panel).
#[derive(Clone)]
pub struct DynRenderable {
    inner: Arc<dyn Renderable + Send + Sync>,
}

impl DynRenderable {
    /// Wrap a [`Renderable`] in a [`DynRenderable`].
    pub fn new(r: impl Renderable + Send + Sync + 'static) -> Self {
        Self { inner: Arc::new(r) }
    }
}

impl fmt::Debug for DynRenderable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DynRenderable").finish()
    }
}

/// Delegates rendering to the inner trait object.
impl Renderable for DynRenderable {
    fn render(&self, options: &ConsoleOptions) -> RenderResult {
        self.inner.render(options)
    }

    fn measure(&self, options: &ConsoleOptions) -> Option<crate::measure::Measurement> {
        self.inner.measure(options)
    }
}

/// A renderable that renders multiple children one after another (vertically).
///
/// Equivalent to Python Rich's `Group`.
#[derive(Debug, Clone)]
pub struct Group {
    /// The child renderables to render in sequence.
    pub children: Vec<DynRenderable>,
}

impl Group {
    /// Create an empty [`Group`].
    pub fn new() -> Self {
        Self { children: Vec::new() }
    }

    /// Add a renderable child to the group.
    pub fn add(&mut self, renderable: impl Renderable + Send + Sync + 'static) {
        self.children.push(DynRenderable::new(renderable));
    }
}

/// Renders each child sequentially and concatenates their output lines.
impl Renderable for Group {
    fn render(&self, options: &ConsoleOptions) -> RenderResult {
        let mut all_lines: Vec<Vec<Segment>> = Vec::new();
        for child in &self.children {
            let result = child.render(options);
            all_lines.extend(result.lines);
        }
        RenderResult { lines: all_lines, items: Vec::new() }
    }
}

// ---------------------------------------------------------------------------
// Capture system — redirect console output to a buffer
// ---------------------------------------------------------------------------

/// Private writer that captures output into a shared buffer.
struct CaptureWriter {
    buf: Arc<Mutex<Vec<u8>>>,
}

impl Write for CaptureWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut data = self.buf.lock().unwrap();
        data.extend_from_slice(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

/// Captured console output. Created by [`Console::end_capture`].
pub struct Capture {
    buf: Arc<Mutex<Vec<u8>>>,
}

impl Capture {
    /// Create an empty Capture (not connected to any console).
    pub fn new(_console: &Console) -> Self {
        Self { buf: Arc::new(Mutex::new(Vec::new())) }
    }

    /// Get the captured text.
    pub fn get(&self) -> String {
        let data = self.buf.lock().unwrap();
        String::from_utf8_lossy(&data).to_string()
    }
}

// Re-export pager types from the dedicated pager module
pub use crate::pager::{Pager, PagerContext, SystemPager};

// ---------------------------------------------------------------------------
// CaptureError
// ---------------------------------------------------------------------------

/// Error type for capture operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CaptureError {
    /// Capture is already in progress.
    AlreadyCapturing,
    /// No capture is currently active.
    NotCapturing,
    /// The captured output could not be decoded as UTF-8.
    InvalidUtf8,
}

impl fmt::Display for CaptureError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AlreadyCapturing => write!(f, "capture already in progress"),
            Self::NotCapturing => write!(f, "no capture active"),
            Self::InvalidUtf8 => write!(f, "captured output is not valid UTF-8"),
        }
    }
}

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

// ---------------------------------------------------------------------------
// NewLine / NoChange renderables
// ---------------------------------------------------------------------------

/// A renderable that outputs a single newline.
pub struct NewLine;

impl Renderable for NewLine {
    fn render(&self, _options: &ConsoleOptions) -> RenderResult {
        RenderResult::from_text("\n")
    }
}

/// A renderable that outputs nothing (used as a sentinel).
pub struct NoChange;

impl Renderable for NoChange {
    fn render(&self, _options: &ConsoleOptions) -> RenderResult {
        RenderResult::new()
    }
}

// ---------------------------------------------------------------------------
// RenderHook — modify render output before display
// ---------------------------------------------------------------------------

/// A hook that can modify render output before display.
pub struct RenderHook {
    hook: Box<dyn Fn(&[Vec<Segment>]) -> Vec<Vec<Segment>> + Send>,
}

impl RenderHook {
    /// Create a new RenderHook from a closure.
    pub fn new<F: Fn(&[Vec<Segment>]) -> Vec<Vec<Segment>> + Send + 'static>(f: F) -> Self {
        Self { hook: Box::new(f) }
    }

    /// Apply the hook to a set of rendered lines.
    pub fn apply(&self, lines: &[Vec<Segment>]) -> Vec<Vec<Segment>> {
        (self.hook)(lines)
    }
}

impl fmt::Debug for RenderHook {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RenderHook").finish()
    }
}

// ---------------------------------------------------------------------------
// ThemeContext — temporarily switch themes with RAII restoration
// ---------------------------------------------------------------------------

/// A RAII guard that restores a previous theme when dropped.
///
/// Created by [`Console::use_theme`]. While alive, the console uses the new
/// theme. When the context is dropped, the original theme is restored.
pub struct ThemeContext {
    console_ptr: *mut Console,
    previous_theme: Theme,
}

// SAFETY: ThemeContext is not Send or Sync because of the raw pointer.
// It must only be used on the same thread as the Console.
// The pointer is valid because Console creates ThemeContext and outlives it.

impl ThemeContext {
    /// Create a new ThemeContext (internal — use [`Console::use_theme`]).
    pub(crate) fn new(console: &mut Console, previous_theme: Theme) -> Self {
        Self {
            console_ptr: console as *mut Console,
            previous_theme,
        }
    }
}

impl Drop for ThemeContext {
    fn drop(&mut self) {
        unsafe {
            (*self.console_ptr).theme = std::mem::take(&mut self.previous_theme);
        }
    }
}

// ---------------------------------------------------------------------------
// Console
// ---------------------------------------------------------------------------

/// The main console for rendering rich output.
pub struct Console {
    /// The output writer.
    pub file: Box<dyn Write + Send>,
    /// Detected color system.
    pub color_system: ColorSystem,
    /// Current theme.
    pub theme: Theme,
    /// Default options.
    pub options: ConsoleOptions,
    /// Current width (may be overridden).
    width: Option<usize>,
    /// Current height (may be overridden).
    height: Option<usize>,
    /// Is this output a terminal?
    is_terminal: bool,
    /// If true, suppress all output.
    pub quiet: bool,
    /// If true, text wraps at word boundaries.
    pub soft_wrap: bool,
    /// Is the alternate screen active?
    alt_screen: bool,
    /// Is the cursor visible?
    cursor_visible: bool,
    /// Active render hooks that modify output before display.
    render_hooks: Vec<RenderHook>,
    /// Captured output buffer (active when capturing).
    capture_buf: Option<Arc<Mutex<Vec<u8>>>>,
    /// Original file writer saved during capture.
    saved_file: Option<Box<dyn Write + Send>>,
}

impl Console {
    /// Create a new Console writing to stdout.
    pub fn new() -> Self {
        let is_terminal = atty::is(atty::Stream::Stdout);
        let color_system = detect_color_system();

        let size = ConsoleDimensions::detect();

        Self {
            file: Box::new(io::stdout()) as Box<dyn Write + Send>,
            color_system,
            theme: crate::theme::default_theme(),
            options: ConsoleOptions {
                size,
                is_terminal,
                max_width: size.width,
                max_height: size.height,
                ..Default::default()
            },
            width: None,
            height: None,
            is_terminal,
            quiet: false,
            soft_wrap: false,
            alt_screen: false,
            cursor_visible: true,
            render_hooks: Vec::new(),
            capture_buf: None,
            saved_file: None,
        }
    }

    /// Create a Console that writes to a file.
    pub fn with_file(file: Box<dyn Write + Send>) -> Self {
        let _is_terminal = false;
        Self {
            file,
            color_system: ColorSystem::Standard,
            theme: crate::theme::default_theme(),
            options: ConsoleOptions {
                size: ConsoleDimensions { width: 80, height: 25 },
                is_terminal: false,
                max_width: 80,
                max_height: 25,
                ..Default::default()
            },
            width: None,
            height: None,
            is_terminal: false,
            quiet: false,
            soft_wrap: false,
            alt_screen: false,
            cursor_visible: true,
            render_hooks: Vec::new(),
            capture_buf: None,
            saved_file: None,
        }
    }

    /// Set the console width (overrides auto-detected terminal width).
    pub fn set_width(&mut self, width: usize) {
        self.width = Some(width);
        self.options.max_width = width;
    }

    /// Set the console height.
    pub fn set_height(&mut self, height: usize) {
        self.height = Some(height);
        self.options.max_height = height;
    }

    /// Get the effective width.
    pub fn width(&self) -> usize {
        self.width.unwrap_or(self.options.size.width)
    }

    /// Get the effective height.
    pub fn height(&self) -> usize {
        self.height.unwrap_or(self.options.size.height)
    }

    /// Render a renderable and return the segment lines.
    pub fn render_lines(
        &self,
        renderable: &dyn Renderable,
        options: &ConsoleOptions,
        style: Option<&Style>,
        _pad: bool,
    ) -> Vec<Vec<Segment>> {
        let result = renderable.render(options);

        if let Some(st) = style {
            result
                .lines
                .into_iter()
                .map(|line| {
                    line.into_iter()
                        .map(|seg| {
                            let new_style = if let Some(ref s) = seg.style {
                                s.combine(st)
                            } else {
                                st.clone()
                            };
                            Segment::styled(seg.text, new_style)
                        })
                        .collect()
                })
                .collect()
        } else {
            result.lines
        }
    }

    /// Look up a style by name from the theme.
    pub fn get_style(&self, name: &str, default: &str) -> Option<Style> {
        self.theme
            .get(name)
            .cloned()
            .or_else(|| {
                if !default.is_empty() {
                    Some(Style::from_str(default))
                } else {
                    None
                }
            })
    }

    /// Render a string (with optional style).
    pub fn render_str(&self, text: &str, style: &str) -> Text {
        let st = self.get_style(style, "");
        let mut t = Text::new(text);
        if let Some(s) = st {
            t = t.style(s);
        }
        t
    }

    // -----------------------------------------------------------------------
    // print / log methods
    // -----------------------------------------------------------------------

    /// Print one or more renderable objects, separated by `sep`, ending with
    /// `end`.
    pub fn print(&mut self, objects: &[&dyn Renderable], sep: &str, end: &str) {
        if self.quiet { return; }
        let mut first = true;
        for obj in objects {
            if !first {
                let _ = write!(self.file, "{sep}");
            }
            first = false;
            let result = obj.render(&self.options);
            let ansi = result.to_ansi();
            let _ = write!(self.file, "{ansi}");
        }
        let _ = write!(self.file, "{end}");
        let _ = self.file.flush();
    }

    /// Print a single renderable followed by a newline.
    pub fn println(&mut self, renderable: &dyn Renderable) {
        if self.quiet { return; }
        let result = renderable.render(&self.options);
        let ansi = result.to_ansi();
        let _ = writeln!(self.file, "{ansi}");
        let _ = self.file.flush();
    }

    /// Print a plain string (supports markup by default when `markup` is
    /// enabled).
    pub fn print_str(&mut self, text: &str) {
        if self.quiet { return; }
        let ansi = if self.options.markup {
            let parsed = crate::markup::render(text);
            parsed.render()
        } else {
            text.to_string()
        };
        let _ = write!(self.file, "{ansi}");
        let _ = self.file.flush();
    }

    /// Print formatted JSON.
    pub fn print_json(&mut self, data: &serde_json::Value) {
        if self.quiet { return; }
        let formatted = crate::json::render_json(data);
        let result = formatted.render(&self.options);
        let ansi = result.to_ansi();
        let _ = writeln!(self.file, "{ansi}");
        let _ = self.file.flush();
    }

    /// Clear the screen.
    pub fn clear(&mut self) {
        if self.quiet { return; }
        let _ = write!(self.file, "\x1b[2J\x1b[H");
        let _ = self.file.flush();
    }

    /// Show the cursor.
    pub fn show_cursor(&mut self) {
        self.cursor_visible = true;
        let _ = write!(self.file, "\x1b[?25h");
        let _ = self.file.flush();
    }

    /// Hide the cursor.
    pub fn hide_cursor(&mut self) {
        self.cursor_visible = false;
        let _ = write!(self.file, "\x1b[?25l");
        let _ = self.file.flush();
    }

    /// Set the terminal window title.
    pub fn set_window_title(&mut self, title: &str) {
        let _ = write!(self.file, "\x1b]0;{title}\x07");
        let _ = self.file.flush();
    }

    /// Get the ANSI escape string for a given color as this console supports.
    pub fn color_ansi(&self, color: &Color) -> String {
        let downgraded = color.downgrade(self.color_system);
        downgraded.to_string()
    }

    // -- Recursive rendering ------------------------------------------------

    /// Render a renderable by recursively flattening nested items into
    /// segments.  This is equivalent to Python Rich's `Console.render()`.
    /// It handles `Group` composition and any renderable that yields other
    /// renderables.
    pub fn render(&self, renderable: &dyn Renderable, options: &ConsoleOptions) -> Vec<Segment> {
        let result = renderable.render(options);
        result.flatten(options)
    }

    /// Measure a renderable's width constraints.
    /// Equivalent to Python Rich's `Measurement.get(console, options, renderable)`.
    pub fn measure(&self, renderable: &dyn Renderable, options: &ConsoleOptions) -> crate::measure::Measurement {
        if let Some(m) = renderable.measure(options) {
            return m;
        }
        let segments = self.render(renderable, options);
        let max_w = segments.iter()
            .map(|s| s.cell_length())
            .max()
            .unwrap_or(0);
        crate::measure::Measurement::new(max_w, options.max_width)
    }

    // -- Convenience render methods -----------------------------------------

    /// Render a rule with the given title.
    /// Equivalent to `Console.rule()`.
    pub fn rule(
        &mut self,
        title: impl Into<String>,
        characters: Option<&str>,
        style: Option<Style>,
        align: Option<AlignMethod>,
    ) {
        if self.quiet { return; }
        let mut rule = crate::rule::Rule::new().title(title);
        if let Some(chars) = characters { rule = rule.characters(chars); }
        if let Some(st) = style { rule = rule.style(st); }
        if let Some(a) = align { rule = rule.align(a); }
        let result = rule.render(&self.options);
        let ansi = result.to_ansi();
        let _ = write!(self.file, "{ansi}");
        let _ = self.file.flush();
    }

    /// Output a bell character.
    pub fn bell(&mut self) {
        if self.quiet { return; }
        let _ = write!(self.file, "\x07");
        let _ = self.file.flush();
    }

    /// Output blank lines.
    pub fn line(&mut self, count: usize) {
        if self.quiet { return; }
        for _ in 0..count {
            let _ = writeln!(self.file);
        }
        let _ = self.file.flush();
    }

    /// Output a log entry with timestamp, caller info.
    pub fn log(&mut self, objects: &[&dyn Renderable]) {
        if self.quiet { return; }
        let now = chrono::Local::now();
        let time_str = format!("[{}]", now.format("%H:%M:%S"));
        let _ = write!(self.file, "{} ", Style::new().dim(true).to_ansi());
        let _ = write!(self.file, "{time_str} ");
        let _ = write!(self.file, "{}", Style::new().reset_ansi());
        self.print(objects, " ", "\n");
    }

    // -- Theme stack --------------------------------------------------------

    /// Push a theme onto the stack.
    pub fn push_theme(&mut self, theme: Theme) {
        let mut new_theme = theme.clone();
        new_theme.inherit = Some(Box::new(self.theme.clone()));
        self.theme = new_theme;
    }

    /// Pop the current theme, restoring the previous one.
    pub fn pop_theme(&mut self) {
        if let Some(ref inherit) = self.theme.inherit {
            self.theme = *inherit.clone();
        }
    }

    // -- Export methods ------------------------------------------------------

    /// Export the current console output as an HTML document.
    ///
    /// Renders the given renderable and wraps it in a styled HTML page.
    pub fn export_html(&self, renderable: &dyn Renderable) -> String {
        let result = renderable.render(&self.options);
        let ansi = result.to_ansi();
        crate::export::export_html(&crate::export::ExportHtmlOptions {
            code: crate::export::strip_ansi_escapes(&ansi),
            ..Default::default()
        })
    }

    /// Save rendered output as an HTML file.
    pub fn save_html(&self, path: impl AsRef<std::path::Path>, renderable: &dyn Renderable) -> std::io::Result<()> {
        let html = self.export_html(renderable);
        crate::export::save_html(path, &crate::export::ExportHtmlOptions {
            code: html,
            ..Default::default()
        })
    }

    /// Export the current console output as an SVG document.
    pub fn export_svg(&self, renderable: &dyn Renderable) -> String {
        let result = renderable.render(&self.options);
        let ansi = result.to_ansi();
        crate::export::export_svg(&crate::export::ExportSvgOptions {
            code: crate::export::strip_ansi_escapes(&ansi),
            ..Default::default()
        })
    }

    /// Save rendered output as an SVG file.
    pub fn save_svg(&self, path: impl AsRef<std::path::Path>, renderable: &dyn Renderable) -> std::io::Result<()> {
        let svg = self.export_svg(renderable);
        crate::export::save_svg(path, &crate::export::ExportSvgOptions {
            code: svg,
            ..Default::default()
        })
    }

    /// Export the current console output as plain text (strips ANSI).
    pub fn export_text(&self, renderable: &dyn Renderable) -> String {
        let result = renderable.render(&self.options);
        let ansi = result.to_ansi();
        crate::export::export_text(&crate::export::ExportTextOptions {
            text: ansi,
            strip_ansi: true,
        })
    }

    /// Save rendered output as a plain text file.
    pub fn save_text(&self, path: impl AsRef<std::path::Path>, renderable: &dyn Renderable) -> std::io::Result<()> {
        let text = self.export_text(renderable);
        crate::export::save_text(path, &crate::export::ExportTextOptions {
            text,
            strip_ansi: false,
        })
    }

    // -- Quiet / Soft-wrap setters ------------------------------------------

    /// Set the quiet flag (suppress all output when true).
    pub fn set_quiet(&mut self, quiet: bool) {
        self.quiet = quiet;
    }

    /// Builder-style setter for quiet.
    pub fn quiet(mut self, quiet: bool) -> Self {
        self.quiet = quiet;
        self
    }

    /// Set the soft-wrap flag (wrap text at word boundaries when true).
    pub fn set_soft_wrap(&mut self, soft_wrap: bool) {
        self.soft_wrap = soft_wrap;
    }

    /// Builder-style setter for soft_wrap.
    pub fn soft_wrap(mut self, soft_wrap: bool) -> Self {
        self.soft_wrap = soft_wrap;
        self
    }

    // -- Input --------------------------------------------------------------

    /// Read a line of input from the user.
    ///
    /// Writes `prompt` to the console, then reads a line from stdin.
    /// When `password` is true, the input is masked with `*` characters
    /// (using raw terminal mode via crossterm).
    pub fn input(&mut self, prompt: &str, password: bool) -> String {
        let _ = write!(self.file, "{prompt}");
        let _ = self.file.flush();

        if password {
            self.read_password()
        } else {
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            input.trim().to_string()
        }
    }

    /// Read a password from stdin with character masking.
    fn read_password(&mut self) -> String {
        use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
        use std::io::Read;

        match enable_raw_mode() {
            Ok(()) => {
                let stdin = io::stdin();
                let mut handle = stdin.lock();
                let mut buf = [0u8; 1];
                let mut password = String::new();

                loop {
                    match handle.read_exact(&mut buf) {
                        Ok(()) => match buf[0] {
                            b'\r' | b'\n' => {
                                let _ = writeln!(self.file);
                                let _ = self.file.flush();
                                break;
                            }
                            b'\x03' => {
                                // Ctrl+C — break and return what we have
                                let _ = writeln!(self.file);
                                let _ = self.file.flush();
                                break;
                            }
                            b'\x7f' | b'\x08' => {
                                // Backspace
                                password.pop();
                            }
                            c => {
                                password.push(c as char);
                                let _ = write!(self.file, "*");
                                let _ = self.file.flush();
                            }
                        },
                        Err(_) => break,
                    }
                }
                let _ = disable_raw_mode();
                password
            }
            Err(_) => {
                // Fallback: read without masking
                let mut input = String::new();
                let _ = io::stdin().read_line(&mut input);
                input.trim().to_string()
            }
        }
    }

    // -- Screen / alternate screen ------------------------------------------

    /// Create a [`ScreenContext`](crate::screen::ScreenContext) that enters the
    /// alternate screen buffer. The context automatically exits the alternate
    /// screen when dropped.
    pub fn screen(&mut self) -> crate::screen::ScreenContext {
        let mut ctx = crate::screen::ScreenContext::new();
        ctx.enter();
        ctx
    }

    /// Enter or exit the alternate screen buffer by writing the corresponding
    /// escape sequences (`\x1b[?1049h` / `\x1b[?1049l`).
    pub fn set_alt_screen(&mut self, enable: bool) {
        self.alt_screen = enable;
        if enable {
            let _ = write!(self.file, "\x1b[?1049h");
        } else {
            let _ = write!(self.file, "\x1b[?1049l");
        }
        let _ = self.file.flush();
    }

    /// Get whether the output is a terminal.
    pub fn is_terminal(&self) -> bool {
        self.is_terminal
    }

    /// Set the terminal size (overrides auto-detected dimensions).
    pub fn set_size(&mut self, width: usize, height: usize) {
        self.width = Some(width);
        self.height = Some(height);
        self.options.max_width = width;
        self.options.max_height = height;
        self.options.size = crate::console::ConsoleDimensions { width, height };
    }

    /// Handle broken pipe errors gracefully.
    ///
    /// In Rust, `write()` returns `ErrorKind::BrokenPipe` instead of raising
    /// `SIGPIPE`, so broken pipes are not fatal. The Console already uses
    /// `let _ = write!(...)` throughout, which silently discards all write
    /// errors including EPIPE. This method is provided for API compatibility
    /// with Python Rich and as a documentation point.
    pub fn on_broken_pipe(&self) {
        // No-op: Rust handles EPIPE via ErrorKind, not signals.
        // All Console write operations use `let _ = write!()` which
        // already discards BrokenPipe errors without panicking.
    }
}

impl Default for Console {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for Console {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Console")
            .field("color_system", &self.color_system)
            .field("width", &self.width())
            .field("height", &self.height())
            .field("is_terminal", &self.is_terminal)
            .field("alt_screen", &self.alt_screen)
            .field("cursor_visible", &self.cursor_visible)
            .field("quiet", &self.quiet)
            .field("soft_wrap", &self.soft_wrap)
            .finish()
    }
}

// ===========================================================================
// New feature methods (Capture, Pager, Terminal Control, Hooks, etc.)
// ===========================================================================

impl Console {
    // -- Capture System ------------------------------------------------------

    /// Start capturing all output. All subsequent writes to this console are
    /// redirected to an internal buffer. Call [`end_capture`](Self::end_capture)
    /// to stop capturing and retrieve the captured content.
    pub fn begin_capture(&mut self) {
        let buf = Arc::new(Mutex::new(Vec::new()));
        let writer = Box::new(CaptureWriter { buf: buf.clone() });
        self.saved_file = Some(std::mem::replace(&mut self.file, writer));
        self.capture_buf = Some(buf);
    }

    /// End capture mode and return the [`Capture`] containing all output written
    /// while capturing was active. The console's output is restored to its
    /// original destination.
    pub fn end_capture(&mut self) -> Capture {
        let buf = self.capture_buf.take().expect("not currently capturing");
        if let Some(saved) = self.saved_file.take() {
            self.file = saved;
        }
        Capture { buf }
    }

    /// Run the given closure with output captured, returning the captured text.
    ///
    /// This is the most ergonomic way to capture output in Rust:
    ///
    /// ```rust,no_run
    /// # use rusty_rich::Console;
    /// let mut console = Console::new();
    /// let output = console.capture(|c| {
    ///     c.print_str("Hello, world!");
    /// });
    /// assert_eq!(output, "Hello, world!");
    /// ```
    pub fn capture<F: FnOnce(&mut Self)>(&mut self, f: F) -> String {
        self.begin_capture();
        f(self);
        let cap = self.end_capture();
        cap.get()
    }

    // -- Pager System --------------------------------------------------------

    /// Get a [`PagerContext`]. Content rendered while the context is alive is
    /// collected and displayed through the system pager (`$PAGER` or `less`)
    /// when the context is dropped.
    ///
    /// `styles` controls whether ANSI styles are preserved when paging.
    pub fn pager(&mut self, styles: bool) -> PagerContext {
        PagerContext::new(Pager::new().color(styles))
    }

    // -- Input with Renderable prompt ----------------------------------------

    /// Display a [`Renderable`] prompt and read a line of input from stdin.
    ///
    /// The prompt is rendered through the console's current options and theme.
    pub fn input_renderable(&mut self, prompt: &dyn Renderable) -> String {
        if !self.quiet {
            let result = prompt.render(&self.options);
            let ansi = result.to_ansi();
            let _ = write!(self.file, "{ansi}");
            let _ = self.file.flush();
        }
        let mut input = String::new();
        let _ = io::stdin().read_line(&mut input);
        input.trim().to_string()
    }

    // -- Exception / Traceback -----------------------------------------------

    /// Print the current exception as a rich traceback.
    ///
    /// In Rust, this is a best-effort rendering; it captures the current
    /// thread's panic info if available. `width` overrides the output width,
    /// and `extra_lines` controls how many lines of source context to show
    /// around each frame.
    pub fn print_exception(&mut self, _width: Option<usize>, _extra_lines: usize) {
        if self.quiet { return; }
        // Note: Rust does not have Python's sys.exc_info(). A full traceback
        // renderer would need std::panic::catch_unwind or custom error capture.
        // This method provides the API surface; for actual panic tracebacks
        // see crate::traceback::install().
        let msg = format!(
            "[bold red]Exception[/bold red]: No current exception info. "
        );
        let msg_text = crate::text::Text::from_markup(&msg);
        let result = msg_text.render();
        let _ = writeln!(self.file, "{result}");
        let _ = self.file.flush();
    }

    // -- JSON pretty-print (string overload) -----------------------------------

    /// Pretty-print a JSON string. Parses the string and renders it with
    /// syntax highlighting.
    pub fn print_json_str(&mut self, json: &str) {
        if self.quiet { return; }
        if let Ok(value) = serde_json::from_str::<serde_json::Value>(json) {
            self.print_json(&value);
        } else {
            let _ = writeln!(self.file, "[invalid JSON]");
            let _ = self.file.flush();
        }
    }

    // -- Render lines (simple version) ----------------------------------------

    /// Render a renderable to a vector of segment lines.
    ///
    /// This is the lower-level render entry point, returning raw lines instead
    /// of an ANSI string. Compare with [`render`](Self::render) which returns
    /// flat segments.
    pub fn render_to_lines(
        &self,
        renderable: &dyn Renderable,
        options: &ConsoleOptions,
    ) -> Vec<Vec<Segment>> {
        let result = renderable.render(options);
        let has_items = !result.items.is_empty();
        let mut lines = if result.lines.is_empty() && has_items {
            let flat = result.flatten(options);
            if flat.is_empty() {
                Vec::new() // also empty after flatten — keep empty
            } else {
                vec![flat]
            }
        } else {
            result.lines
        };
        // Apply any render hooks
        if !self.render_hooks.is_empty() {
            for hook in &self.render_hooks {
                lines = hook.apply(&lines);
            }
        }
        lines
    }

    // -- Render ANSI string ---------------------------------------------------

    /// Render a plain string to ANSI text, applying the current theme and
    /// styles. Returns the ANSI-formatted string.
    pub fn render_ansi(&self, text: &str) -> String {
        let t = self.render_str(text, "");
        t.render()
    }

    // -- Export SVG with options ----------------------------------------------

    /// Export the console output as an SVG document with explicit options.
    ///
    /// This delegates to [`crate::export::export_svg`] with the given
    /// [`ExportSvgOptions`](crate::export::ExportSvgOptions).
    pub fn export_svg_opts(&self, options: &crate::export::ExportSvgOptions) -> String {
        crate::export::export_svg(options)
    }

    // -- Console Properties ---------------------------------------------------

    /// Get the terminal size as [`ConsoleDimensions`].
    pub fn size(&self) -> ConsoleDimensions {
        ConsoleDimensions {
            width: self.width(),
            height: self.height(),
        }
    }

    /// Check if the terminal is a "dumb" terminal (no color support).
    pub fn is_dumb_terminal(&self) -> bool {
        std::env::var("TERM").map_or(false, |t| t == "dumb")
    }

    /// Check if the console is currently in the alternate screen buffer.
    pub fn is_alt_screen(&self) -> bool {
        self.alt_screen
    }

    // -- Terminal Control ----------------------------------------------------

    /// Show or hide the cursor based on the boolean parameter.
    ///
    /// `true` shows the cursor, `false` hides it. Tracks the current state
    /// so it can be queried via internal fields.
    pub fn set_cursor_visible(&mut self, visible: bool) {
        self.cursor_visible = visible;
        if visible {
            let _ = write!(self.file, "\x1b[?25h");
        } else {
            let _ = write!(self.file, "\x1b[?25l");
        }
        let _ = self.file.flush();
    }

    /// Temporarily switch to a different theme. Returns a [`ThemeContext`]
    /// that restores the original theme when dropped.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use rusty_rich::{Console, Theme};
    /// let mut console = Console::new();
    /// let custom = Theme::new();
    /// {
    ///     let _ctx = console.use_theme(custom);
    ///     // console uses custom theme here
    /// }
    /// // original theme restored here
    /// ```
    pub fn use_theme(&mut self, theme: Theme) -> ThemeContext {
        let prev = std::mem::replace(&mut self.theme, theme);
        ThemeContext::new(self, prev)
    }

    /// Clear the live display region. When in alt-screen mode, this clears
    /// the entire alternate screen. Otherwise, it's equivalent to
    /// [`clear`](Self::clear).
    pub fn clear_live(&mut self) {
        if self.alt_screen {
            let _ = write!(self.file, "\x1b[2J\x1b[H");
        } else {
            let _ = write!(self.file, "\x1b[2J\x1b[H");
        }
        let _ = self.file.flush();
    }

    /// Set the active live display. Stores a reference to the [`Live`]
    /// renderer for integration with the console's rendering pipeline.
    ///
    /// Note: [`Live`](crate::live::Live) manages its own refresh cycle;
    /// this method is primarily for API compatibility with Python Rich.
    pub fn set_live(&mut self, _live: &crate::live::Live) {
        // Live manages its own refresh cycle; this method provides the
        // API surface for attaching a live display to the console.
    }

    /// Update the full screen (enter alt-screen, render content, exit).
    ///
    /// Clears the screen and renders the given renderable. If `options` is
    /// `None`, the console's current options are used.
    pub fn update_screen(&mut self, renderable: &dyn Renderable, options: Option<&ConsoleOptions>) {
        let opts = options.unwrap_or(&self.options);
        let segments = self.render(renderable, opts);
        let mut output = String::new();
        for seg in &segments {
            output.push_str(&seg.to_ansi());
        }
        let _ = write!(self.file, "\x1b[2J\x1b[H{output}");
        let _ = self.file.flush();
    }

    /// Update the screen from pre-rendered lines of segments.
    ///
    /// Takes already-rendered lines and displays them as the full screen
    /// content, clearing existing content first.
    pub fn update_screen_lines(&mut self, lines: &[Vec<Segment>], options: Option<&ConsoleOptions>) {
        let _ = options;
        let mut output = String::new();
        for line in lines {
            for seg in line {
                output.push_str(&seg.to_ansi());
            }
            output.push('\n');
        }
        let _ = write!(self.file, "\x1b[2J\x1b[H{output}");
        let _ = self.file.flush();
    }

    // -- Render Hooks --------------------------------------------------------

    /// Add a [`RenderHook`] to the console. Hooks are applied in order and
    /// can modify the rendered lines before they are displayed.
    pub fn push_render_hook(&mut self, hook: RenderHook) {
        self.render_hooks.push(hook);
    }

    /// Remove and return the most recently added [`RenderHook`], if any.
    pub fn pop_render_hook(&mut self) -> Option<RenderHook> {
        self.render_hooks.pop()
    }
}

// ---------------------------------------------------------------------------
// Color system detection
// ---------------------------------------------------------------------------

/// Detect the terminal color system from environment variables.
///
/// Checks `COLORTERM`, `TERM`, `NO_COLOR`, and `CLICOLOR` to determine
/// whether the terminal supports true color, 8-bit, or standard 16 colors.
fn detect_color_system() -> ColorSystem {
    // Check common env vars
    if let Ok(val) = std::env::var("COLORTERM") {
        if val == "truecolor" || val == "24bit" {
            return ColorSystem::TrueColor;
        }
    }
    if let Ok(term) = std::env::var("TERM") {
        if term.contains("256color") {
            return ColorSystem::EightBit;
        }
        if term == "xterm-kitty" {
            return ColorSystem::TrueColor;
        }
    }
    // Check NO_COLOR / CLICOLOR
    if std::env::var("NO_COLOR").is_ok() {
        return ColorSystem::Standard;
    }
    // Default to true color on modern terminals
    if atty::is(atty::Stream::Stdout) {
        ColorSystem::TrueColor
    } else {
        ColorSystem::Standard
    }
}

// ---------------------------------------------------------------------------
// Global console instance (like Rich's `get_console()`)
// ---------------------------------------------------------------------------

use once_cell::sync::Lazy;

static GLOBAL_CONSOLE: Lazy<Mutex<Console>> = Lazy::new(|| {
    Mutex::new(Console::new())
});

/// Get a reference to the global Console.
pub fn get_console() -> std::sync::MutexGuard<'static, Console> {
    GLOBAL_CONSOLE.lock().unwrap()
}

// ---------------------------------------------------------------------------
// Convenience functions (like Rich's `print()`)
// ---------------------------------------------------------------------------

/// Print objects using the global console.
pub fn print_objects(objects: &[&dyn Renderable]) {
    let mut console = GLOBAL_CONSOLE.lock().unwrap();
    console.print(objects, " ", "\n");
}

/// Print a string with markup support.
pub fn print_str(text: &str) {
    let mut console = GLOBAL_CONSOLE.lock().unwrap();
    console.print_str(text);
}

/// Print formatted JSON.
pub fn print_json_val(data: &serde_json::Value) {
    let mut console = GLOBAL_CONSOLE.lock().unwrap();
    console.print_json(data);
}

// ---------------------------------------------------------------------------
// Reconfigure global console
// ---------------------------------------------------------------------------

/// Reconfigure the global Console singleton with new dimensions and/or
/// color system. This updates the shared global console instance used by
/// [`print_objects`], [`print_str`], and [`print_json_val`].
///
/// # Parameters
///
/// * `width` — New terminal width (None to keep current).
/// * `height` — New terminal height (None to keep current).
/// * `color_system` — New color system level (None to keep current).
pub fn reconfigure(
    width: Option<usize>,
    height: Option<usize>,
    color_system: Option<ColorSystem>,
) {
    let mut console = GLOBAL_CONSOLE.lock().unwrap();
    if let Some(w) = width {
        console.set_width(w);
    }
    if let Some(h) = height {
        console.set_height(h);
    }
    if let Some(cs) = color_system {
        console.color_system = cs;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_render_result_from_text() {
        let r = RenderResult::from_text("hello");
        assert_eq!(r.lines.len(), 1);
        assert_eq!(r.lines[0][0].text, "hello");
    }

    #[test]
    fn test_console_options_default() {
        let opts = ConsoleOptions::default();
        assert!(opts.markup);
    }

    #[test]
    fn test_console_quiet_default() {
        let console = Console::new();
        assert!(!console.quiet);
    }

    #[test]
    fn test_console_quiet_setter() {
        let mut console = Console::new();
        console.set_quiet(true);
        assert!(console.quiet);
    }

    #[test]
    fn test_console_quiet_builder() {
        let console = Console::new().quiet(true);
        assert!(console.quiet);
    }

    #[test]
    fn test_console_quiet_suppresses_print() {
        let mut console = Console::new();
        console.quiet = true;
        // Should not panic
        console.print(&[], " ", "\n");
        console.println(&"test");
        console.print_str("test");
    }

    #[test]
    fn test_console_soft_wrap_default() {
        let console = Console::new();
        assert!(!console.soft_wrap);
    }

    #[test]
    fn test_console_soft_wrap_setter() {
        let mut console = Console::new();
        console.set_soft_wrap(true);
        assert!(console.soft_wrap);
    }

    #[test]
    fn test_console_soft_wrap_builder() {
        let console = Console::new().soft_wrap(true);
        assert!(console.soft_wrap);
    }

    #[test]
    fn test_console_is_terminal() {
        let console = Console::new();
        // is_terminal depends on whether stdout is a terminal
        let detected = console.is_terminal();
        assert_eq!(detected, atty::is(atty::Stream::Stdout));
    }

    #[test]
    fn test_console_set_size() {
        let mut console = Console::new();
        console.set_size(120, 30);
        assert_eq!(console.width(), 120);
        assert_eq!(console.height(), 30);
        assert_eq!(console.options.max_width, 120);
        assert_eq!(console.options.max_height, 30);
    }

    #[test]
    fn test_console_set_alt_screen() {
        let mut console = Console::new();
        // Just ensure it doesn't panic
        console.set_alt_screen(true);
        console.set_alt_screen(false);
    }

    #[test]
    fn test_console_on_broken_pipe() {
        let console = Console::new();
        console.on_broken_pipe(); // no-op
    }

    #[test]
    fn test_console_input_normal() {
        // We can't easily test stdin in unit tests, but we can verify
        // the method signature compiles and matches.
        let _console = Console::new();
        // input() cannot be meaningfully tested without actual stdin.
    }

    #[test]
    fn test_console_debug() {
        let console = Console::new();
        let debug = format!("{:?}", console);
        assert!(debug.contains("Console"));
    }

    #[test]
    fn test_console_with_file_has_no_terminal() {
        let console = Console::with_file(Box::new(std::io::sink()));
        assert!(!console.is_terminal());
    }

    // -- New feature tests ---------------------------------------------------

    #[test]
    fn test_newline_renderable() {
        let nl = NewLine;
        let result = nl.render(&ConsoleOptions::default());
        let ansi = result.to_ansi();
        assert_eq!(ansi, "\n");
    }

    #[test]
    fn test_nochange_renderable() {
        let nc = NoChange;
        let result = nc.render(&ConsoleOptions::default());
        assert!(result.lines.is_empty());
        assert!(result.items.is_empty());
    }

    #[test]
    fn test_capture_begin_end() {
        let mut console = Console::with_file(Box::new(std::io::sink()));
        console.begin_capture();
        let _ = write!(console.file, "captured text");
        let cap = console.end_capture();
        assert_eq!(cap.get(), "captured text");
    }

    #[test]
    fn test_capture_with_closure() {
        let mut console = Console::with_file(Box::new(std::io::sink()));
        let output = console.capture(|c| {
            let _ = write!(c.file, "hello from capture");
        });
        assert_eq!(output, "hello from capture");
    }

    #[test]
    fn test_capture_new_empty() {
        let console = Console::new();
        let cap = Capture::new(&console);
        assert_eq!(cap.get(), "");
    }

    #[test]
    fn test_system_pager_default() {
        let pager = SystemPager::new();
        // SystemPager should be constructable and show() should not panic
        // when called with empty content (even if pager command doesn't exist)
        let _ = pager.show("");
    }

    #[test]
    fn test_pager_enabled() {
        let pager = Pager::new();
        assert!(pager.is_enabled());
        let disabled = pager.enabled(false);
        assert!(!disabled.is_enabled());
    }

    #[test]
    fn test_render_hook() {
        let hook = RenderHook::new(|lines| {
            // Add a bold "HOOKED" segment to every line
            let hooked: Vec<Vec<Segment>> = lines.iter().map(|line| {
                let mut new_line = line.clone();
                new_line.push(Segment::styled("HOOKED", Style::new().bold(true)));
                new_line
            }).collect();
            hooked
        });
        let lines = vec![vec![Segment::new("test")]];
        let result = hook.apply(&lines);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].len(), 2);
        assert_eq!(result[0][1].text, "HOOKED");
    }

    #[test]
    fn test_console_size() {
        let mut console = Console::new();
        console.set_size(100, 40);
        let dims = console.size();
        assert_eq!(dims.width, 100);
        assert_eq!(dims.height, 40);
    }

    #[test]
    fn test_console_is_dumb_terminal() {
        let console = Console::new();
        // In test environment, TERM is typically not "dumb"
        // Just verify it doesn't panic and returns a bool
        let _ = console.is_dumb_terminal();
    }

    #[test]
    fn test_console_is_alt_screen() {
        let mut console = Console::new();
        assert!(!console.is_alt_screen());
        console.alt_screen = true;
        assert!(console.is_alt_screen());
    }

    #[test]
    fn test_console_render_ansi() {
        let console = Console::new();
        let ansi = console.render_ansi("test");
        // Should return plain text if no style applied
        assert!(ansi.contains("test") || ansi.contains("\x1b["));
    }

    #[test]
    fn test_console_render_to_lines() {
        let console = Console::new();
        let opts = ConsoleOptions::default();
        let lines = console.render_to_lines(&"hello", &opts);
        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0][0].text, "hello");
    }

    #[test]
    fn test_console_input_renderable() {
        // input_renderable reads from stdin, which is hard to test
        // Verify the method signature compiles
        let _console = Console::new();
    }

    #[test]
    fn test_console_print_exception_noop() {
        let mut console = Console::new();
        // Should not panic
        console.print_exception(None, 3);
    }

    #[test]
    fn test_console_render_hooks_push_pop() {
        let mut console = Console::new();
        let hook = RenderHook::new(|lines| lines.to_vec());
        console.push_render_hook(hook);
        assert_eq!(console.render_hooks.len(), 1);
        let popped = console.pop_render_hook();
        assert!(popped.is_some());
        assert!(console.render_hooks.is_empty());
    }

    #[test]
    fn test_console_reconfigure() {
        // Test that reconfigure doesn't panic
        reconfigure(Some(120), Some(40), None);
        reconfigure(None, None, Some(ColorSystem::Standard));
        // Reset
        reconfigure(None, None, None);
    }

    #[test]
    fn test_pager_context_write() {
        let pager = Pager::new().enabled(false);
        let mut ctx = PagerContext::new(pager);
        ctx.feed("test content");
        // Drop should not panic since pager is disabled
    }

    #[test]
    fn test_theme_context() {
        let mut console = Console::new();
        let custom_theme = Theme::new();
        let original = console.theme.clone();
        {
            let _ctx = console.use_theme(custom_theme);
            // Theme should be the custom one now
        }
        // After ctx drops, original theme should be restored
        assert_eq!(console.theme.styles.len(), original.styles.len());
    }
}