rich_rust 0.1.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
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
# Proposed Architecture for `rich_rust`

> **Author:** Gemini
> **Date:** 2026-01-16
> **Reference:** `EXISTING_RICH_STRUCTURE_AND_ARCHITECTURE.md`

## Executive Summary

This document defines the Rust architecture for `rich_rust`. It translates the dynamic, protocol-based architecture of Python's Rich into a static, trait-based, and zero-cost architecture in Rust.

## 1. Core Traits (The Protocols)

In Python, Rich relies on `__rich_console__` and `__rich__` dunder methods. In Rust, we will define traits.

### 1.1 `ConsoleRender` (The Primary Trait)

This is the equivalent of `__rich_console__`. It produces an iterator of Segments.

```rust
pub trait ConsoleRender {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> RenderResult;
}

// RenderResult is likely an Iterator or a custom struct that implements Iterator
pub type RenderResult = Box<dyn Iterator<Item = Segment> + Send>; 
// OR: simplified to return a Vec<Segment> for Phase 1 simplicity
```

### 1.2 `RichDisplay` (The Conversion Trait)

Equivalent to `__rich__`. It converts a high-level object into something that implements `ConsoleRender` (usually `Text`).

```rust
pub trait RichDisplay {
    fn to_rich(&self) -> impl ConsoleRender;
}
```

### 1.3 `Measure` (The Layout Trait)

Equivalent to `__rich_measure__`.

```rust
pub trait Measure {
    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement;
}

pub struct Measurement {
    pub min: usize,
    pub max: usize,
}
```

## 2. Core Data Structures

### 2.1 `Console`

The coordinator.

```rust
pub struct Console {
    pub options: ConsoleOptions,
    writer: Box<dyn Write + Send + Sync>,
    // thread-local buffer?
}

impl Console {
    pub fn print(&self, renderable: &impl ConsoleRender) {
        // 1. Get iterator from renderable
        // 2. Iterate segments
        // 3. Diff styles
        // 4. Write ANSI codes + Text to stream
    }
}
```

### 2.2 `Style`

Optimized for size and copying.

```rust
use bitflags::bitflags;

#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct Style {
    pub foreground: Option<Color>,
    pub background: Option<Color>,
    pub attributes: Attributes,
}

bitflags! {
    #[derive(Default)]
    pub struct Attributes: u16 {
        const BOLD      = 1 << 0;
        const DIM       = 1 << 1;
        const ITALIC    = 1 << 2;
        const UNDERLINE = 1 << 3;
        const BLINK     = 1 << 4;
        const REVERSE   = 1 << 5;
        const HIDDEN    = 1 << 6;
        const STRIKE    = 1 << 7;
    }
}
```

### 2.3 `Text` and `Segment`

```rust
pub struct Segment {
    pub text: String, // Or Cow<'a, str> for optimization
    pub style: Style,
}

pub struct Text {
    pub spans: Vec<Span>,
    pub plain: String,
}

pub struct Span {
    pub start: usize,
    pub end: usize,
    pub style: Style,
}
```

## 3. Rendering Pipeline Strategy

### 3.1 Immediate Mode vs Buffering

Rich (Python) is largely immediate mode but buffers lines for layout (tables). `rich_rust` will strictly follow the **Iterator** pattern. Renderables will return Iterators that yield Segments lazily where possible.

### 3.2 ANSI Generation

We will use a dedicated module `ansi.rs` to handle the diffing of styles.

```rust
// Logic:
// current_style = Style::default();
// for segment in segments {
//     let diff_codes = current_style.diff(segment.style);
//     writer.write(diff_codes);
//     writer.write(segment.text);
//     current_style = segment.style;
// }
// writer.write(RESET);
```

## 4. Layout Engine

The `Table` implementation is the hardest part.

1.  **Measure Pass:** Call `measure()` on all cells to determine min/max widths.
2.  **Calculate Column Widths:** Use the same ratio/distribute algorithm as Python (ported to Rust).
3.  **Render Pass:** Call `render()` with the calculated column widths injected into `ConsoleOptions`.

## 5. Ecosystem Dependencies

| Component | Recommended Crate |
|-----------|-------------------|
| CLI Args | `clap` |
| Regex | `regex` (for markup parsing) |
| Colors | `palette` or custom struct |
| Terminal | `crossterm` (for detection/size) |
| Syntax | `syntect` |
| Markdown | `pulldown-cmark` |

## 6. Live Display System

> Reference: `RICH_SPEC.md` Section 16

The `Live` system enables dynamic, auto-refreshing displays for progress bars, spinners, and dashboards.

### 6.1 Core Types

```rust
/// Vertical overflow handling strategy
#[derive(Clone, Copy, Debug, Default)]
pub enum VerticalOverflow {
    Crop,       // Truncate excess lines
    #[default]
    Ellipsis,   // Show "..." for overflow
    Visible,    // Allow overflow (final render only)
}

/// Configuration for Live display
#[derive(Clone)]
pub struct LiveOptions {
    pub screen: bool,                      // Use alternate screen buffer
    pub auto_refresh: bool,                // Enable refresh thread (default: true)
    pub refresh_per_second: f64,           // Refresh rate (default: 4.0)
    pub transient: bool,                   // Clear display on exit
    pub redirect_stdout: bool,             // Capture stdout (default: true)
    pub redirect_stderr: bool,             // Capture stderr (default: true)
    pub vertical_overflow: VerticalOverflow,
}

impl Default for LiveOptions {
    fn default() -> Self {
        Self {
            screen: false,
            auto_refresh: true,
            refresh_per_second: 4.0,
            transient: false,
            redirect_stdout: true,
            redirect_stderr: true,
            vertical_overflow: VerticalOverflow::Ellipsis,
        }
    }
}
```

### 6.2 Live Struct

```rust
use std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering}};
use std::thread::{self, JoinHandle};
use std::time::Duration;

pub struct Live<'a> {
    // Content
    renderable: Arc<RwLock<Option<Box<dyn ConsoleRender + Send + Sync>>>>,
    get_renderable: Option<Box<dyn Fn() -> Box<dyn ConsoleRender + Send + Sync> + Send + Sync>>,

    // Console integration
    console: &'a Console,
    options: LiveOptions,

    // State
    started: AtomicBool,
    nested: AtomicBool,
    alt_screen_active: AtomicBool,

    // Refresh thread
    refresh_thread: Option<JoinHandle<()>>,
    refresh_stop: Arc<AtomicBool>,

    // Rendering state
    live_render: Arc<RwLock<LiveRender>>,
}

/// Tracks cursor position and rendered lines for refresh
pub struct LiveRender {
    shape: Option<(usize, usize)>,  // (width, height) of last render
    last_lines: usize,              // Lines rendered on last refresh
}
```

### 6.3 Builder Pattern

```rust
impl<'a> Live<'a> {
    pub fn new(console: &'a Console) -> Self {
        Self::with_options(console, LiveOptions::default())
    }

    pub fn with_options(console: &'a Console, options: LiveOptions) -> Self {
        assert!(options.refresh_per_second > 0.0, "refresh_per_second must be > 0");
        let mut opts = options;
        if opts.screen {
            opts.transient = true;  // Screen mode implies transient
        }
        Self {
            console,
            options: opts,
            renderable: Arc::new(RwLock::new(None)),
            get_renderable: None,
            started: AtomicBool::new(false),
            nested: AtomicBool::new(false),
            alt_screen_active: AtomicBool::new(false),
            refresh_thread: None,
            refresh_stop: Arc::new(AtomicBool::new(false)),
            live_render: Arc::new(RwLock::new(LiveRender::default())),
        }
    }

    pub fn renderable<R: ConsoleRender + Send + Sync + 'static>(mut self, r: R) -> Self {
        *self.renderable.write().unwrap() = Some(Box::new(r));
        self
    }

    pub fn get_renderable<F>(mut self, f: F) -> Self
    where
        F: Fn() -> Box<dyn ConsoleRender + Send + Sync> + Send + Sync + 'static,
    {
        self.get_renderable = Some(Box::new(f));
        self
    }
}
```

### 6.4 Lifecycle Management

```rust
impl<'a> Live<'a> {
    pub fn start(&mut self) -> Result<(), LiveError> {
        if self.started.swap(true, Ordering::SeqCst) {
            return Ok(());  // Already started
        }

        // Register with console (detect nesting)
        if !self.console.set_live(self) {
            self.nested.store(true, Ordering::SeqCst);
            return Ok(());  // Nested, delegate to parent
        }

        // Enable alternate screen
        if self.options.screen {
            self.console.set_alt_screen(true)?;
            self.alt_screen_active.store(true, Ordering::SeqCst);
        }

        // Hide cursor
        self.console.show_cursor(false)?;

        // Push render hook for output interception
        self.console.push_render_hook(self);

        // Initial refresh
        self.refresh()?;

        // Start refresh thread
        if self.options.auto_refresh {
            self.start_refresh_thread();
        }

        Ok(())
    }

    pub fn stop(&mut self) -> Result<(), LiveError> {
        if !self.started.swap(false, Ordering::SeqCst) {
            return Ok(());  // Already stopped
        }

        // Stop refresh thread
        if let Some(handle) = self.refresh_thread.take() {
            self.refresh_stop.store(true, Ordering::SeqCst);
            let _ = handle.join();
        }

        if self.nested.load(Ordering::SeqCst) {
            return Ok(());  // Nested, parent handles cleanup
        }

        // Pop render hook
        self.console.pop_render_hook();

        // Final refresh with transient handling
        if !self.options.transient {
            // Render one last time without overflow cropping
            self.refresh_with_overflow(VerticalOverflow::Visible)?;
            self.console.line()?;  // Newline after final content
        }

        // Restore cursor
        self.console.show_cursor(true)?;

        // Disable alternate screen
        if self.alt_screen_active.load(Ordering::SeqCst) {
            self.console.set_alt_screen(false)?;
        }

        // Unregister from console
        self.console.clear_live();

        Ok(())
    }
}

// Drop implementation ensures cleanup
impl<'a> Drop for Live<'a> {
    fn drop(&mut self) {
        let _ = self.stop();
    }
}
```

### 6.5 Refresh Thread

```rust
impl<'a> Live<'a> {
    fn start_refresh_thread(&mut self) {
        let renderable = Arc::clone(&self.renderable);
        let live_render = Arc::clone(&self.live_render);
        let stop = Arc::clone(&self.refresh_stop);
        let interval = Duration::from_secs_f64(1.0 / self.options.refresh_per_second);
        let console = self.console.clone();  // Console is Clone/Arc-based

        self.refresh_thread = Some(thread::spawn(move || {
            while !stop.load(Ordering::Relaxed) {
                thread::sleep(interval);
                if !stop.load(Ordering::Relaxed) {
                    let guard = renderable.read().unwrap();
                    if let Some(ref r) = *guard {
                        let mut lr = live_render.write().unwrap();
                        let _ = lr.refresh(&console, r.as_ref());
                    }
                }
            }
        }));
    }
}
```

### 6.6 Refresh Logic

```rust
impl LiveRender {
    /// Refresh the display with current content
    pub fn refresh(
        &mut self,
        console: &Console,
        renderable: &dyn ConsoleRender,
    ) -> Result<(), LiveError> {
        self.refresh_inner(console, renderable, VerticalOverflow::Ellipsis)
    }

    fn refresh_inner(
        &mut self,
        console: &Console,
        renderable: &dyn ConsoleRender,
        overflow: VerticalOverflow,
    ) -> Result<(), LiveError> {
        let (width, height) = console.size();
        let shape = (width, height);

        // Check if terminal resized
        let shape_changed = self.shape != Some(shape);
        self.shape = Some(shape);

        // Calculate available height for content
        let available_height = height.saturating_sub(1);  // Leave room for cursor

        // Render content to segments
        let options = ConsoleOptions {
            max_width: Some(width),
            ..console.options()
        };
        let segments: Vec<Segment> = renderable.render(console, &options).collect();

        // Split into lines and handle overflow
        let lines = Segment::split_lines(&segments);
        let (display_lines, needs_ellipsis) = match overflow {
            VerticalOverflow::Crop => {
                let cropped: Vec<_> = lines.take(available_height).collect();
                (cropped, false)
            }
            VerticalOverflow::Ellipsis => {
                let all_lines: Vec<_> = lines.collect();
                if all_lines.len() > available_height {
                    let mut display = all_lines.into_iter().take(available_height - 1).collect::<Vec<_>>();
                    (display, true)
                } else {
                    (all_lines, false)
                }
            }
            VerticalOverflow::Visible => {
                (lines.collect(), false)
            }
        };

        // Move cursor up to overwrite previous render
        if self.last_lines > 0 && !shape_changed {
            console.control(Control::move_up(self.last_lines))?;
        }

        // Clear and write new content
        for line in &display_lines {
            console.print_segments(line)?;
            console.control(Control::erase_line(EraseMode::ToEnd))?;
            console.line()?;
        }

        // Ellipsis indicator
        if needs_ellipsis {
            console.print_styled("...", Style::dim())?;
            console.control(Control::erase_line(EraseMode::ToEnd))?;
        }

        // Clear any remaining lines from previous render
        let current_lines = display_lines.len() + if needs_ellipsis { 1 } else { 0 };
        for _ in current_lines..self.last_lines {
            console.control(Control::erase_line(EraseMode::All))?;
            console.line()?;
        }

        self.last_lines = current_lines;
        console.flush()?;

        Ok(())
    }
}
```

### 6.7 Console Integration Points

The `Console` struct needs these additions for Live support:

```rust
impl Console {
    // Live registration (returns false if already has active Live)
    pub fn set_live(&self, live: &Live) -> bool;
    pub fn clear_live(&self);

    // Render hooks for output interception
    pub fn push_render_hook(&self, hook: &dyn RenderHook);
    pub fn pop_render_hook(&self);

    // Terminal control
    pub fn set_alt_screen(&self, enable: bool) -> Result<(), ConsoleError>;
    pub fn show_cursor(&self, show: bool) -> Result<(), ConsoleError>;
    pub fn control(&self, ctrl: Control) -> Result<(), ConsoleError>;
}

pub trait RenderHook {
    fn process(&self, segments: &[Segment]) -> RenderHookResult;
}

pub enum RenderHookResult {
    Passthrough,           // Continue normal output
    Intercept(Vec<Segment>), // Modified output
    Suppress,              // Suppress output entirely
}
```

### 6.8 Usage Examples

```rust
// Basic usage with context manager pattern
fn main() -> Result<(), Box<dyn Error>> {
    let console = Console::new();

    let mut live = Live::new(&console)
        .renderable(Panel::new("Loading..."));

    live.start()?;

    for i in 0..100 {
        live.update(Panel::new(format!("Progress: {}%", i)));
        thread::sleep(Duration::from_millis(50));
    }

    live.stop()?;
    Ok(())
}

// With dynamic get_renderable callback
let mut live = Live::new(&console)
    .get_renderable(|| Box::new(get_current_status()));

// Alternate screen mode for full-screen apps
let mut live = Live::with_options(&console, LiveOptions {
    screen: true,
    refresh_per_second: 30.0,
    ..Default::default()
});
```

### 6.9 Threading Considerations

- **RwLock for renderable:** Allows reads from refresh thread while main thread updates
- **AtomicBool for state:** Lock-free status checks
- **Arc sharing:** Console and LiveRender shared between main and refresh threads
- **Graceful shutdown:** `refresh_stop` flag checked before each refresh

## 7. Layout System

> Reference: `RICH_SPEC.md` Section 17

The `Layout` system creates dashboard-style interfaces by dividing screen space into rows and columns using ratio-based distribution.

### 7.1 Core Types

```rust
/// Rectangular region on screen
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Region {
    pub x: usize,      // Horizontal position (0 = left)
    pub y: usize,      // Vertical position (0 = top)
    pub width: usize,
    pub height: usize,
}

impl Region {
    pub fn new(x: usize, y: usize, width: usize, height: usize) -> Self {
        Self { x, y, width, height }
    }

    pub fn is_empty(&self) -> bool {
        self.width == 0 || self.height == 0
    }

    /// Slice region horizontally at offset, returning (left, right)
    pub fn split_horizontal(self, offset: usize) -> (Self, Self) {
        let left = Self { width: offset, ..self };
        let right = Self { x: self.x + offset, width: self.width.saturating_sub(offset), ..self };
        (left, right)
    }

    /// Slice region vertically at offset, returning (top, bottom)
    pub fn split_vertical(self, offset: usize) -> (Self, Self) {
        let top = Self { height: offset, ..self };
        let bottom = Self { y: self.y + offset, height: self.height.saturating_sub(offset), ..self };
        (top, bottom)
    }
}
```

### 7.2 Splitter Trait

```rust
/// Strategy for dividing a region among children
pub trait Splitter: Send + Sync {
    fn name(&self) -> &str;
    fn tree_icon(&self) -> &str;
    fn divide(&self, children: &[&Layout], region: Region) -> Vec<Region>;
}

/// Split horizontally (children side-by-side)
pub struct RowSplitter;

impl Splitter for RowSplitter {
    fn name(&self) -> &str { "row" }
    fn tree_icon(&self) -> &str { "⬌" }

    fn divide(&self, children: &[&Layout], region: Region) -> Vec<Region> {
        let widths = ratio_resolve(region.width, children);
        let mut regions = Vec::with_capacity(children.len());
        let mut x = region.x;

        for width in widths {
            regions.push(Region { x, width, ..region });
            x += width;
        }
        regions
    }
}

/// Split vertically (children stacked)
pub struct ColumnSplitter;

impl Splitter for ColumnSplitter {
    fn name(&self) -> &str { "column" }
    fn tree_icon(&self) -> &str { "⬍" }

    fn divide(&self, children: &[&Layout], region: Region) -> Vec<Region> {
        let heights = ratio_resolve(region.height, children);
        let mut regions = Vec::with_capacity(children.len());
        let mut y = region.y;

        for height in heights {
            regions.push(Region { y, height, ..region });
            y += height;
        }
        regions
    }
}
```

### 7.3 Edge Trait (for Ratio Resolution)

```rust
/// Element with size constraints for ratio distribution
pub trait Edge {
    fn size(&self) -> Option<usize>;      // Fixed size, or None for flexible
    fn ratio(&self) -> usize;             // Flex ratio (default: 1)
    fn minimum_size(&self) -> usize;      // Minimum allowed size (default: 1)
}
```

### 7.4 Layout Struct

```rust
use std::sync::{Arc, RwLock};
use std::collections::HashMap;

/// Result of rendering a layout region
pub struct LayoutRender {
    pub region: Region,
    pub lines: Vec<Vec<Segment>>,  // Rendered content as lines
}

pub type RenderMap = HashMap<String, LayoutRender>;

pub struct Layout {
    // Identity & content
    name: Option<String>,
    renderable: Box<dyn ConsoleRender + Send + Sync>,

    // Size constraints (implements Edge)
    size: Option<usize>,       // Fixed size (cells for row, lines for column)
    minimum_size: usize,       // Minimum size (default: 1)
    ratio: usize,              // Flex ratio (default: 1)

    // State
    visible: bool,             // Include in layout (default: true)
    splitter: Box<dyn Splitter>,
    children: Vec<Layout>,

    // Render cache (for nested access)
    render_map: Arc<RwLock<RenderMap>>,
}

impl Edge for Layout {
    fn size(&self) -> Option<usize> { self.size }
    fn ratio(&self) -> usize { self.ratio }
    fn minimum_size(&self) -> usize { self.minimum_size }
}
```

### 7.5 Builder Pattern

```rust
impl Layout {
    pub fn new() -> Self {
        Self {
            name: None,
            renderable: Box::new(Placeholder),
            size: None,
            minimum_size: 1,
            ratio: 1,
            visible: true,
            splitter: Box::new(ColumnSplitter),
            children: Vec::new(),
            render_map: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    pub fn renderable<R: ConsoleRender + Send + Sync + 'static>(mut self, r: R) -> Self {
        self.renderable = Box::new(r);
        self
    }

    pub fn size(mut self, size: usize) -> Self {
        self.size = Some(size);
        self
    }

    pub fn minimum_size(mut self, min: usize) -> Self {
        self.minimum_size = min;
        self
    }

    pub fn ratio(mut self, ratio: usize) -> Self {
        self.ratio = ratio;
        self
    }

    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }
}
```

### 7.6 Split Operations

```rust
impl Layout {
    /// Split into sub-layouts with given splitter
    pub fn split<S: Splitter + 'static>(&mut self, splitter: S, children: Vec<Layout>) {
        self.splitter = Box::new(splitter);
        self.children = children;
    }

    /// Split horizontally (children side-by-side)
    pub fn split_row(&mut self, children: Vec<Layout>) {
        self.split(RowSplitter, children);
    }

    /// Split vertically (children stacked)
    pub fn split_column(&mut self, children: Vec<Layout>) {
        self.split(ColumnSplitter, children);
    }

    /// Add children to existing split
    pub fn add_split(&mut self, children: Vec<Layout>) {
        self.children.extend(children);
    }

    /// Remove all children
    pub fn unsplit(&mut self) {
        self.children.clear();
    }

    /// Check if this layout has children
    pub fn is_split(&self) -> bool {
        !self.children.is_empty()
    }
}
```

### 7.7 Named Lookup

```rust
impl Layout {
    /// Get layout by name (recursive search)
    pub fn get(&self, name: &str) -> Option<&Layout> {
        if self.name.as_deref() == Some(name) {
            return Some(self);
        }
        for child in &self.children {
            if let Some(found) = child.get(name) {
                return Some(found);
            }
        }
        None
    }

    /// Get mutable layout by name
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Layout> {
        if self.name.as_deref() == Some(name) {
            return Some(self);
        }
        for child in &mut self.children {
            if let Some(found) = child.get_mut(name) {
                return Some(found);
            }
        }
        None
    }

    /// Update content of named layout
    pub fn update<R: ConsoleRender + Send + Sync + 'static>(&mut self, name: &str, renderable: R) {
        if let Some(layout) = self.get_mut(name) {
            layout.renderable = Box::new(renderable);
        }
    }
}

// Index syntax support
impl std::ops::Index<&str> for Layout {
    type Output = Layout;
    fn index(&self, name: &str) -> &Self::Output {
        self.get(name).expect("Layout not found")
    }
}

impl std::ops::IndexMut<&str> for Layout {
    fn index_mut(&mut self, name: &str) -> &mut Self::Output {
        self.get_mut(name).expect("Layout not found")
    }
}
```

### 7.8 Rendering Algorithm

```rust
impl Layout {
    /// Render into a region, returning region map
    pub fn render_to_region(
        &self,
        console: &Console,
        region: Region,
    ) -> RenderMap {
        let mut render_map = HashMap::new();
        self.render_recursive(console, region, &mut render_map);
        render_map
    }

    fn render_recursive(
        &self,
        console: &Console,
        region: Region,
        render_map: &mut RenderMap,
    ) {
        if !self.visible || region.is_empty() {
            return;
        }

        if self.is_split() {
            // Get visible children
            let visible: Vec<&Layout> = self.children.iter()
                .filter(|c| c.visible)
                .collect();

            // Divide region among children
            let child_regions = self.splitter.divide(&visible, region);

            // Recurse into each child
            for (child, child_region) in visible.into_iter().zip(child_regions) {
                child.render_recursive(console, child_region, render_map);
            }
        } else {
            // Leaf node: render content
            let options = ConsoleOptions {
                max_width: Some(region.width),
                height: Some(region.height),
                ..console.options()
            };

            let segments: Vec<Segment> = self.renderable
                .render(console, &options)
                .collect();

            // Split into lines and crop to region height
            let mut lines: Vec<Vec<Segment>> = Segment::split_lines(&segments)
                .take(region.height)
                .collect();

            // Pad to full height if needed
            while lines.len() < region.height {
                lines.push(vec![Segment::new(" ".repeat(region.width))]);
            }

            // Pad/crop each line to region width
            for line in &mut lines {
                let line_width: usize = line.iter().map(|s| s.cell_len()).sum();
                if line_width < region.width {
                    line.push(Segment::new(" ".repeat(region.width - line_width)));
                }
                // Cropping handled by ConsoleOptions::max_width
            }

            let layout_render = LayoutRender { region, lines };

            if let Some(name) = &self.name {
                render_map.insert(name.clone(), layout_render);
            }
        }
    }
}
```

### 7.9 ConsoleRender Implementation

```rust
impl ConsoleRender for Layout {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> RenderResult {
        let width = options.max_width.unwrap_or_else(|| console.width());
        let height = options.height.unwrap_or_else(|| console.height());

        let region = Region::new(0, 0, width, height);
        let render_map = self.render_to_region(console, region);

        // Composite all rendered regions into output lines
        let mut canvas: Vec<Vec<Segment>> = vec![vec![]; height];

        for (_, layout_render) in &render_map {
            let LayoutRender { region, lines } = layout_render;
            for (i, line) in lines.iter().enumerate() {
                let y = region.y + i;
                if y < canvas.len() {
                    // Insert segments at correct x position
                    // (In practice, use a more sophisticated compositor)
                    canvas[y].extend(line.iter().cloned());
                }
            }
        }

        // Flatten to segments
        let segments: Vec<Segment> = canvas.into_iter()
            .flat_map(|line| {
                let mut row = line;
                row.push(Segment::newline());
                row
            })
            .collect();

        Box::new(segments.into_iter())
    }
}
```

### 7.10 Placeholder Renderable

```rust
/// Default placeholder shown when no content is set
pub struct Placeholder;

impl ConsoleRender for Placeholder {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> RenderResult {
        let width = options.max_width.unwrap_or(80);
        let height = options.height.unwrap_or(1);

        let text = format!("({} x {})", width, height);
        let styled = Text::styled(&text, Style::dim());

        Box::new(styled.render(console, options))
    }
}
```

### 7.11 Usage Examples

```rust
// Basic 2-column layout
let mut layout = Layout::new();
layout.split_row(vec![
    Layout::new().name("left").renderable(Panel::new("Left pane")),
    Layout::new().name("right").renderable(Panel::new("Right pane")),
]);

// Dashboard with header, body (2 columns), footer
let mut layout = Layout::new();
layout.split_column(vec![
    Layout::new().name("header").size(3).renderable(Panel::new("Header")),
    Layout::new().name("body"),  // Will be split further
    Layout::new().name("footer").size(3).renderable(Panel::new("Footer")),
]);

layout["body"].split_row(vec![
    Layout::new().name("sidebar").size(20).renderable(list),
    Layout::new().name("main").ratio(3).renderable(content),
]);

// Update content dynamically
layout.update("main", new_content);

// Render to console
console.print(&layout)?;
```

### 7.12 Integration with Live

Layout is often used with Live for real-time dashboards:

```rust
let mut layout = build_dashboard_layout();
let mut live = Live::new(&console)
    .renderable(layout.clone());

live.start()?;

loop {
    // Update layout contents
    layout.update("status", get_current_status());
    layout.update("logs", get_recent_logs());

    live.update(layout.clone());
    thread::sleep(Duration::from_millis(100));
}
```

### 7.13 Thread Safety

- **RenderMap with Arc<RwLock>:** Allows safe access from multiple threads
- **Clone for updates:** Create new Layout trees rather than mutating shared state
- **Lock scope:** Keep locks brief during render; release before I/O

## 8. Directory Structure

```
src/
├── main.rs (CLI entry point for testing)
├── lib.rs
├── console.rs
├── style.rs
├── text.rs
├── segment.rs
├── measure.rs
├── terminal.rs
├── live/
│   ├── mod.rs
│   ├── live.rs
│   └── live_render.rs
├── layout/
│   ├── mod.rs
│   ├── layout.rs
│   ├── region.rs
│   └── splitter.rs
├── renderables/
│   ├── mod.rs
│   ├── table.rs
│   ├── panel.rs
│   └── ...
├── markup/
│   ├── mod.rs
│   └── parser.rs
├── logging/
│   ├── mod.rs
│   └── handler.rs
└── macros.rs (e.g., console_print!)
```

## 9. Logging Integration

> Reference: `RICH_SPEC.md` Section 18

Rust has two major logging ecosystems: `log` (simple facade) and `tracing` (async-aware, structured). We'll support both.

### 9.1 Architecture Overview

```
┌─────────────────┐     ┌─────────────────┐
│  log::Log       │     │ tracing::Layer  │
│  (log crate)    │     │ (tracing-sub.)  │
└────────┬────────┘     └────────┬────────┘
         │                       │
         └───────────┬───────────┘
              ┌──────────────┐
              │  RichHandler │
              │  (common)    │
              └──────┬───────┘
              ┌──────────────┐
              │   Console    │
              │   output     │
              └──────────────┘
```

### 9.2 Configuration

```rust
/// Configuration for Rich logging
#[derive(Clone)]
pub struct RichLogConfig {
    // Display columns
    pub show_time: bool,           // Show timestamp column (default: true)
    pub show_level: bool,          // Show log level column (default: true)
    pub show_target: bool,         // Show target/module (default: true)
    pub show_file: bool,           // Show file:line (default: true)
    pub enable_link_path: bool,    // Terminal hyperlinks (default: true)

    // Formatting
    pub time_format: TimeFormat,   // strftime or callback (default: "[%H:%M:%S]")
    pub omit_repeated_times: bool, // Skip duplicate times (default: true)
    pub level_width: usize,        // Level column width (default: 5)

    // Message styling
    pub markup: bool,              // Parse Rich markup (default: false)
    pub highlighter: Option<Box<dyn Highlighter>>,  // Message highlighter
    pub keywords: Vec<String>,     // Keywords to highlight (default: HTTP methods)

    // Tracebacks (tracing-error integration)
    pub rich_tracebacks: bool,     // Enable SpanTrace rendering (default: false)
    pub tracebacks_show_locals: bool,  // Show local variables (default: false)
}

impl Default for RichLogConfig {
    fn default() -> Self {
        Self {
            show_time: true,
            show_level: true,
            show_target: true,
            show_file: true,
            enable_link_path: true,
            time_format: TimeFormat::Strftime("[%H:%M:%S]".into()),
            omit_repeated_times: true,
            level_width: 5,
            markup: false,
            highlighter: None,
            keywords: vec![
                "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"
            ].into_iter().map(String::from).collect(),
            rich_tracebacks: false,
            tracebacks_show_locals: false,
        }
    }
}

pub enum TimeFormat {
    Strftime(String),
    Callback(Box<dyn Fn(&DateTime<Local>) -> Text + Send + Sync>),
}
```

### 9.3 Level Styling

```rust
/// Style names for log levels (theme-defined)
fn level_style(level: Level) -> &'static str {
    match level {
        Level::Error => "logging.level.error",     // Red, bold
        Level::Warn  => "logging.level.warning",   // Yellow
        Level::Info  => "logging.level.info",      // Green
        Level::Debug => "logging.level.debug",     // Blue, dim
        Level::Trace => "logging.level.trace",     // Dim
    }
}

fn format_level(level: Level, width: usize) -> Text {
    let name = match level {
        Level::Error => "ERROR",
        Level::Warn  => "WARN",
        Level::Info  => "INFO",
        Level::Debug => "DEBUG",
        Level::Trace => "TRACE",
    };
    Text::styled(format!("{:width$}", name), level_style(level))
}
```

### 9.4 Log Record Rendering

```rust
/// Renders a log record as a grid table row
pub struct LogRender {
    config: RichLogConfig,
    last_time: Option<Text>,
}

impl LogRender {
    pub fn render(
        &mut self,
        console: &Console,
        timestamp: DateTime<Local>,
        level: Level,
        target: &str,
        file: Option<&str>,
        line: Option<u32>,
        message: Text,
    ) -> Table {
        let mut grid = Table::grid().padding((0, 1));
        grid.expand = true;

        // Add columns based on config
        if self.config.show_time {
            grid.add_column(Column::new().style("log.time"));
        }
        if self.config.show_level {
            grid.add_column(Column::new()
                .style("log.level")
                .width(self.config.level_width));
        }
        grid.add_column(Column::new()
            .ratio(1)
            .style("log.message")
            .overflow(Overflow::Fold));
        if self.config.show_file && file.is_some() {
            grid.add_column(Column::new().style("log.path"));
        }

        // Build row
        let mut row = Vec::new();

        if self.config.show_time {
            let time_text = self.format_time(&timestamp);
            if self.config.omit_repeated_times && Some(&time_text) == self.last_time.as_ref() {
                row.push(Text::new(" ".repeat(time_text.len())));
            } else {
                self.last_time = Some(time_text.clone());
                row.push(time_text);
            }
        }

        if self.config.show_level {
            row.push(format_level(level, self.config.level_width));
        }

        row.push(message);

        if self.config.show_file {
            if let Some(f) = file {
                row.push(self.format_path(f, line));
            }
        }

        grid.add_row(row);
        grid
    }

    fn format_time(&self, dt: &DateTime<Local>) -> Text {
        match &self.config.time_format {
            TimeFormat::Strftime(fmt) => Text::new(dt.format(fmt).to_string()),
            TimeFormat::Callback(f) => f(dt),
        }
    }

    fn format_path(&self, file: &str, line: Option<u32>) -> Text {
        let mut text = Text::new();
        let filename = Path::new(file).file_name()
            .map(|s| s.to_string_lossy())
            .unwrap_or_default();

        if self.config.enable_link_path {
            text.append(&filename, Style::link(format!("file://{}", file)));
        } else {
            text.append(&filename, Style::default());
        }

        if let Some(n) = line {
            text.append(":", Style::default());
            if self.config.enable_link_path {
                text.append(&n.to_string(), Style::link(format!("file://{}#{}", file, n)));
            } else {
                text.append(&n.to_string(), Style::default());
            }
        }
        text
    }
}
```

### 9.5 Message Processing

```rust
impl LogRender {
    fn process_message(&self, msg: &str) -> Text {
        // 1. Parse markup if enabled
        let mut text = if self.config.markup {
            Text::from_markup(msg)
        } else {
            Text::new(msg)
        };

        // 2. Apply highlighter
        if let Some(h) = &self.config.highlighter {
            text = h.highlight(text);
        }

        // 3. Highlight keywords
        if !self.config.keywords.is_empty() {
            text.highlight_words(&self.config.keywords, "logging.keyword");
        }

        text
    }
}
```

### 9.6 Integration with `log` Crate

```rust
use log::{Log, Record, Level, Metadata, SetLoggerError};
use std::sync::Mutex;

pub struct RichLogger {
    console: Console,
    config: RichLogConfig,
    render: Mutex<LogRender>,
    level: Level,
}

impl RichLogger {
    pub fn new(console: Console, config: RichLogConfig) -> Self {
        Self {
            console,
            render: Mutex::new(LogRender::new(config.clone())),
            config,
            level: Level::Info,
        }
    }

    pub fn with_level(mut self, level: Level) -> Self {
        self.level = level;
        self
    }

    /// Install as the global logger
    pub fn install(self) -> Result<(), SetLoggerError> {
        let level = self.level;
        log::set_boxed_logger(Box::new(self))?;
        log::set_max_level(level.to_level_filter());
        Ok(())
    }
}

impl Log for RichLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= self.level
    }

    fn log(&self, record: &Record) {
        if !self.enabled(record.metadata()) {
            return;
        }

        let message = self.render.lock().unwrap()
            .process_message(&record.args().to_string());

        let table = self.render.lock().unwrap().render(
            &self.console,
            Local::now(),
            record.level(),
            record.target(),
            record.file(),
            record.line(),
            message,
        );

        // Thread-safe print
        let _ = self.console.print(&table);
    }

    fn flush(&self) {
        let _ = self.console.flush();
    }
}
```

### 9.7 Integration with `tracing` Crate

```rust
use tracing::{Event, Subscriber, span};
use tracing_subscriber::{Layer, layer::Context, registry::LookupSpan};

pub struct RichLayer {
    console: Console,
    config: RichLogConfig,
    render: Mutex<LogRender>,
}

impl<S> Layer<S> for RichLayer
where
    S: Subscriber + for<'a> LookupSpan<'a>,
{
    fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
        // Extract fields from event
        let mut visitor = FieldVisitor::default();
        event.record(&mut visitor);

        let level = match *event.metadata().level() {
            tracing::Level::ERROR => Level::Error,
            tracing::Level::WARN => Level::Warn,
            tracing::Level::INFO => Level::Info,
            tracing::Level::DEBUG => Level::Debug,
            tracing::Level::TRACE => Level::Trace,
        };

        let message = self.render.lock().unwrap()
            .process_message(&visitor.message);

        let table = self.render.lock().unwrap().render(
            &self.console,
            Local::now(),
            level,
            event.metadata().target(),
            event.metadata().file(),
            event.metadata().line(),
            message,
        );

        let _ = self.console.print(&table);
    }
}

#[derive(Default)]
struct FieldVisitor {
    message: String,
    fields: Vec<(String, String)>,
}

impl tracing::field::Visit for FieldVisitor {
    fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
        if field.name() == "message" {
            self.message = format!("{:?}", value);
        } else {
            self.fields.push((field.name().to_string(), format!("{:?}", value)));
        }
    }
}
```

### 9.8 Macro Helpers

```rust
/// Initialize Rich logging with defaults
#[macro_export]
macro_rules! init_rich_logging {
    () => {
        RichLogger::new(Console::stdout(), RichLogConfig::default())
            .install()
            .expect("Failed to install logger")
    };
    ($level:expr) => {
        RichLogger::new(Console::stdout(), RichLogConfig::default())
            .with_level($level)
            .install()
            .expect("Failed to install logger")
    };
}

/// Configure Rich logging for tracing
pub fn init_rich_tracing() {
    use tracing_subscriber::prelude::*;

    tracing_subscriber::registry()
        .with(RichLayer::new(Console::stdout(), RichLogConfig::default()))
        .init();
}
```

### 9.9 Usage Examples

```rust
// Basic log crate usage
use log::{info, warn, error};
use rich_rust::logging::{RichLogger, RichLogConfig};

fn main() {
    RichLogger::new(Console::stdout(), RichLogConfig::default())
        .with_level(log::Level::Debug)
        .install()
        .unwrap();

    info!("Server starting...");
    info!("GET /api/users 200 OK");
    warn!("High memory usage detected");
    error!("Connection refused");
}

// Output:
// [12:34:56] INFO  Server starting...                    main.rs:8
//           INFO  GET /api/users 200 OK                  main.rs:9
// [12:34:57] WARN  High memory usage detected            main.rs:10
//           ERROR Connection refused                     main.rs:11

// With tracing
use tracing::{info, instrument};

#[instrument]
fn process_request(id: u32) {
    info!(request_id = id, "Processing request");
}

fn main() {
    init_rich_tracing();
    process_request(42);
}
```

### 9.10 Theme Integration

Default styles in theme:

```rust
impl Theme {
    fn logging_styles() -> HashMap<&'static str, Style> {
        hashmap! {
            "log.time" => Style::dim(),
            "log.level" => Style::default(),
            "log.message" => Style::default(),
            "log.path" => Style::dim(),
            "logging.level.error" => Style::new().red().bold(),
            "logging.level.warning" => Style::new().yellow(),
            "logging.level.info" => Style::new().green(),
            "logging.level.debug" => Style::new().blue().dim(),
            "logging.level.trace" => Style::dim(),
            "logging.keyword" => Style::new().yellow().bold(),
        }
    }
}
```

### 9.11 Error Handling with tracing-error

```rust
use tracing_error::{SpanTrace, ErrorLayer};

/// Rich-formatted error with span trace
pub struct RichError {
    source: Box<dyn std::error::Error + Send + Sync>,
    span_trace: SpanTrace,
}

impl RichError {
    pub fn render(&self, console: &Console) -> Text {
        let mut text = Text::new();

        // Error message
        text.append(&self.source.to_string(), Style::new().red().bold());
        text.append("\n\n", Style::default());

        // Span trace (similar to Python traceback)
        text.append("Trace:\n", Style::dim());
        for span in self.span_trace.iter() {
            text.append(&format!("  {} at {}:{}\n",
                span.name(),
                span.file().unwrap_or("?"),
                span.line().unwrap_or(0)
            ), Style::dim());
        }

        text
    }
}
```

### 9.12 Design Decisions

| Decision | Choice | Rationale |
|----------|--------|-----------|
| Dual support | Both log and tracing | Cover all use cases |
| Thread safety | Mutex around LogRender | Simple, correct; contention unlikely |
| Keyword highlighting | Opt-in | Avoid false positives in structured logs |
| Time omission | Match Python behavior | Cleaner output for rapid logs |
| Hyperlinks | Default on | Modern terminals support it |
| Tracebacks | Via tracing-error | Native Rust span traces |

## 10. HTML/SVG Export

> Reference: `RICH_SPEC.md` Section 19

Export console output to static HTML and SVG for documentation, sharing, and embedding.

### 10.1 Architecture Overview

```
┌────────────────┐
│ Console        │ record=true
│ (record_buffer)│
└───────┬────────┘
        │ Vec<Segment>
┌────────────────┐
│ Segment        │
│ Processing     │ filter_control, simplify
└───────┬────────┘
   ┌────┴─────┐
   ▼          ▼
┌──────┐  ┌──────┐
│ HTML │  │ SVG  │
│ Exp. │  │ Exp. │
└──────┘  └──────┘
```

### 10.2 Terminal Theme

```rust
/// Color palette for export rendering
#[derive(Clone, Debug)]
pub struct TerminalTheme {
    pub background: ColorTriplet,
    pub foreground: ColorTriplet,
    pub ansi_colors: [ColorTriplet; 16],  // Standard + bright ANSI
}

impl TerminalTheme {
    pub fn new(
        background: (u8, u8, u8),
        foreground: (u8, u8, u8),
        normal: [(u8, u8, u8); 8],
        bright: Option<[(u8, u8, u8); 8]>,
    ) -> Self {
        let bright = bright.unwrap_or(normal);
        let mut ansi_colors = [ColorTriplet::default(); 16];
        for (i, c) in normal.iter().enumerate() {
            ansi_colors[i] = ColorTriplet::from(*c);
        }
        for (i, c) in bright.iter().enumerate() {
            ansi_colors[i + 8] = ColorTriplet::from(*c);
        }
        Self {
            background: ColorTriplet::from(background),
            foreground: ColorTriplet::from(foreground),
            ansi_colors,
        }
    }

    /// Resolve a Rich Color to RGB using this theme
    pub fn resolve_color(&self, color: &Color, foreground: bool) -> ColorTriplet {
        match color.color_type {
            ColorType::Default => {
                if foreground { self.foreground } else { self.background }
            }
            ColorType::Standard(n) | ColorType::Windows(n) => {
                self.ansi_colors[n as usize % 16]
            }
            ColorType::EightBit(n) => {
                EIGHT_BIT_PALETTE[n as usize]  // Standard 256-color lookup
            }
            ColorType::TrueColor(r, g, b) => {
                ColorTriplet::new(r, g, b)
            }
        }
    }
}

// Built-in themes
pub static DEFAULT_TERMINAL_THEME: Lazy<TerminalTheme> = Lazy::new(|| {
    TerminalTheme::new(
        (255, 255, 255), (0, 0, 0),
        LIGHT_NORMAL, Some(LIGHT_BRIGHT)
    )
});

pub static SVG_EXPORT_THEME: Lazy<TerminalTheme> = Lazy::new(|| {
    TerminalTheme::new(
        (41, 41, 41), (197, 200, 198),
        DARK_NORMAL, Some(DARK_BRIGHT)
    )
});

pub static MONOKAI: Lazy<TerminalTheme> = Lazy::new(|| { /* ... */ });
pub static DIMMED_MONOKAI: Lazy<TerminalTheme> = Lazy::new(|| { /* ... */ });
pub static NIGHT_OWLISH: Lazy<TerminalTheme> = Lazy::new(|| { /* ... */ });
```

### 10.3 Style to CSS Conversion

```rust
impl Style {
    /// Convert to CSS rules for HTML export
    pub fn to_css(&self, theme: &TerminalTheme) -> String {
        let mut rules = Vec::new();

        let (fg, bg) = if self.reverse {
            (self.bgcolor.as_ref(), self.color.as_ref())
        } else {
            (self.color.as_ref(), self.bgcolor.as_ref())
        };

        // Foreground color (with dim handling)
        if let Some(color) = fg {
            let mut rgb = theme.resolve_color(color, true);
            if self.dim {
                rgb = blend_rgb(&rgb, &theme.background, 0.5);
            }
            rules.push(format!("color: {}", rgb.hex()));
            rules.push(format!("text-decoration-color: {}", rgb.hex()));
        }

        // Background color
        if let Some(color) = bg {
            let rgb = theme.resolve_color(color, false);
            rules.push(format!("background-color: {}", rgb.hex()));
        }

        // Text attributes
        if self.bold { rules.push("font-weight: bold".into()); }
        if self.italic { rules.push("font-style: italic".into()); }
        if self.underline { rules.push("text-decoration: underline".into()); }
        if self.strike { rules.push("text-decoration: line-through".into()); }
        if self.overline { rules.push("text-decoration: overline".into()); }

        rules.join("; ")
    }

    /// Convert to CSS rules for SVG export (uses fill instead of color)
    pub fn to_svg_css(&self, theme: &TerminalTheme) -> String {
        let mut rules = Vec::new();

        let (fg, bg) = if self.reverse {
            (self.bgcolor.as_ref(), self.color.as_ref())
        } else {
            (self.color.as_ref(), self.bgcolor.as_ref())
        };

        // Fill color (SVG equivalent of color)
        let mut rgb = fg.map(|c| theme.resolve_color(c, true))
            .unwrap_or(theme.foreground);
        if self.dim {
            let bg_rgb = bg.map(|c| theme.resolve_color(c, false))
                .unwrap_or(theme.background);
            rgb = blend_rgb(&rgb, &bg_rgb, 0.4);
        }
        rules.push(format!("fill: {}", rgb.hex()));

        // Text attributes
        if self.bold { rules.push("font-weight: bold".into()); }
        if self.italic { rules.push("font-style: italic".into()); }
        if self.underline { rules.push("text-decoration: underline".into()); }
        if self.strike { rules.push("text-decoration: line-through".into()); }

        rules.join(";")
    }
}

fn blend_rgb(fg: &ColorTriplet, bg: &ColorTriplet, factor: f64) -> ColorTriplet {
    let blend = |a: u8, b: u8| -> u8 {
        ((a as f64) * (1.0 - factor) + (b as f64) * factor) as u8
    };
    ColorTriplet::new(
        blend(fg.red, bg.red),
        blend(fg.green, bg.green),
        blend(fg.blue, bg.blue),
    )
}
```

### 10.4 HTML Export

```rust
/// Configuration for HTML export
#[derive(Clone)]
pub struct HtmlExportConfig {
    pub theme: TerminalTheme,
    pub inline_styles: bool,    // Inline vs stylesheet (default: false)
    pub template: Option<String>, // Custom HTML template
}

impl Default for HtmlExportConfig {
    fn default() -> Self {
        Self {
            theme: DEFAULT_TERMINAL_THEME.clone(),
            inline_styles: false,
            template: None,
        }
    }
}

pub struct HtmlExporter<'a> {
    config: HtmlExportConfig,
    segments: &'a [Segment],
}

impl<'a> HtmlExporter<'a> {
    pub fn new(segments: &'a [Segment], config: HtmlExportConfig) -> Self {
        Self { config, segments }
    }

    pub fn export(&self) -> String {
        let processed = Segment::simplify(
            Segment::filter_control(self.segments.iter().cloned())
        ).collect::<Vec<_>>();

        if self.config.inline_styles {
            self.export_inline(&processed)
        } else {
            self.export_stylesheet(&processed)
        }
    }

    fn export_inline(&self, segments: &[Segment]) -> String {
        let mut html = String::new();
        for seg in segments {
            let escaped = html_escape(&seg.text);
            if let Some(style) = &seg.style {
                let css = style.to_css(&self.config.theme);
                if let Some(link) = &style.link {
                    html.push_str(&format!(
                        r#"<a href="{}" style="{}">{}</a>"#,
                        html_escape(link), css, escaped
                    ));
                } else if !css.is_empty() {
                    html.push_str(&format!(
                        r#"<span style="{}">{}</span>"#, css, escaped
                    ));
                } else {
                    html.push_str(&escaped);
                }
            } else {
                html.push_str(&escaped);
            }
        }
        self.wrap_html(html, "")
    }

    fn export_stylesheet(&self, segments: &[Segment]) -> String {
        let mut html = String::new();
        let mut styles: HashMap<String, usize> = HashMap::new();

        for seg in segments {
            let escaped = html_escape(&seg.text);
            if let Some(style) = &seg.style {
                let css = style.to_css(&self.config.theme);
                let class_num = *styles.entry(css.clone())
                    .or_insert_with(|| styles.len() + 1);

                if let Some(link) = &style.link {
                    html.push_str(&format!(
                        r#"<a class="r{}" href="{}">{}</a>"#,
                        class_num, html_escape(link), escaped
                    ));
                } else {
                    html.push_str(&format!(
                        r#"<span class="r{}">{}</span>"#,
                        class_num, escaped
                    ));
                }
            } else {
                html.push_str(&escaped);
            }
        }

        let stylesheet = styles.iter()
            .map(|(css, num)| format!(".r{} {{ {} }}", num, css))
            .collect::<Vec<_>>()
            .join("\n");

        self.wrap_html(html, &stylesheet)
    }

    fn wrap_html(&self, code: String, stylesheet: &str) -> String {
        let template = self.config.template.as_deref()
            .unwrap_or(DEFAULT_HTML_TEMPLATE);

        template
            .replace("{code}", &code)
            .replace("{stylesheet}", stylesheet)
            .replace("{foreground}", &self.config.theme.foreground.hex())
            .replace("{background}", &self.config.theme.background.hex())
    }
}

const DEFAULT_HTML_TEMPLATE: &str = r#"<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
{stylesheet}
body {
    color: {foreground};
    background-color: {background};
}
</style>
</head>
<body>
    <pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">
        <code style="font-family:inherit">{code}</code>
    </pre>
</body>
</html>"#;

fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}
```

### 10.5 SVG Export

```rust
/// Configuration for SVG export
#[derive(Clone)]
pub struct SvgExportConfig {
    pub theme: TerminalTheme,
    pub title: String,
    pub font_aspect_ratio: f64,   // Width/height ratio (default: 0.61 for Fira Code)
    pub char_height: f64,         // Character height in pixels (default: 20)
    pub template: Option<String>, // Custom SVG template
}

impl Default for SvgExportConfig {
    fn default() -> Self {
        Self {
            theme: SVG_EXPORT_THEME.clone(),
            title: "Rich".into(),
            font_aspect_ratio: 0.61,
            char_height: 20.0,
            template: None,
        }
    }
}

pub struct SvgExporter<'a> {
    config: SvgExportConfig,
    segments: &'a [Segment],
    width: usize,  // Console width
}

impl<'a> SvgExporter<'a> {
    pub fn new(segments: &'a [Segment], width: usize, config: SvgExportConfig) -> Self {
        Self { config, segments, width }
    }

    pub fn export(&self) -> String {
        let processed = Segment::filter_control(self.segments.iter().cloned())
            .collect::<Vec<_>>();

        let char_height = self.config.char_height;
        let char_width = char_height * self.config.font_aspect_ratio;
        let line_height = char_height * 1.22;

        // Layout constants
        const MARGIN: f64 = 1.0;
        const PADDING_TOP: f64 = 40.0;
        const PADDING_SIDE: f64 = 8.0;

        // Generate unique ID from content hash
        let unique_id = self.compute_unique_id();

        // Process segments into positioned text elements
        let mut text_elements = Vec::new();
        let mut background_rects = Vec::new();
        let mut styles: HashMap<String, usize> = HashMap::new();
        let mut y = 0usize;

        for line in Segment::split_and_crop_lines(&processed, self.width) {
            let mut x = 0usize;
            for seg in line {
                let style = seg.style.as_ref().cloned().unwrap_or_default();
                let css = style.to_svg_css(&self.config.theme);
                let class_num = *styles.entry(css.clone())
                    .or_insert_with(|| styles.len() + 1);

                // Background rectangle
                if style.bgcolor.is_some() || style.reverse {
                    let bg_color = if style.reverse {
                        style.color.as_ref()
                            .map(|c| self.config.theme.resolve_color(c, true))
                            .unwrap_or(self.config.theme.foreground)
                    } else {
                        style.bgcolor.as_ref()
                            .map(|c| self.config.theme.resolve_color(c, false))
                            .unwrap_or(self.config.theme.background)
                    };

                    background_rects.push(format!(
                        r#"<rect fill="{}" x="{}" y="{}" width="{}" height="{}" shape-rendering="crispEdges"/>"#,
                        bg_color.hex(),
                        x as f64 * char_width,
                        y as f64 * line_height + 1.5,
                        char_width * seg.text.len() as f64,
                        line_height + 0.25
                    ));
                }

                // Text element (skip whitespace-only)
                if seg.text.trim().len() > 0 {
                    text_elements.push(format!(
                        r#"<text class="{}-r{}" x="{}" y="{}" textLength="{}">{}</text>"#,
                        unique_id,
                        class_num,
                        x as f64 * char_width,
                        y as f64 * line_height + char_height,
                        char_width * seg.text.len() as f64,
                        svg_escape(&seg.text)
                    ));
                }

                x += cell_len(&seg.text);
            }
            y += 1;
        }

        // Build stylesheet
        let styles_css = styles.iter()
            .map(|(css, num)| format!(".{}-r{} {{ {} }}", unique_id, num, css))
            .collect::<Vec<_>>()
            .join("\n");

        // Calculate dimensions
        let terminal_width = char_width * self.width as f64 + PADDING_SIDE * 2.0;
        let terminal_height = line_height * y as f64 + PADDING_TOP + PADDING_SIDE;
        let total_width = terminal_width + MARGIN * 2.0;
        let total_height = terminal_height + MARGIN * 2.0;

        // Build chrome (window decoration)
        let chrome = self.build_chrome(terminal_width, terminal_height, &unique_id);

        // Assemble SVG
        let template = self.config.template.as_deref()
            .unwrap_or(DEFAULT_SVG_TEMPLATE);

        template
            .replace("{unique_id}", &unique_id)
            .replace("{width}", &total_width.to_string())
            .replace("{height}", &total_height.to_string())
            .replace("{char_height}", &char_height.to_string())
            .replace("{line_height}", &line_height.to_string())
            .replace("{terminal_width}", &(char_width * self.width as f64).to_string())
            .replace("{terminal_height}", &(line_height * y as f64).to_string())
            .replace("{terminal_x}", &(MARGIN + PADDING_SIDE).to_string())
            .replace("{terminal_y}", &(MARGIN + PADDING_TOP).to_string())
            .replace("{styles}", &styles_css)
            .replace("{chrome}", &chrome)
            .replace("{backgrounds}", &background_rects.join("\n"))
            .replace("{matrix}", &text_elements.join("\n"))
    }

    fn compute_unique_id(&self) -> String {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        for seg in self.segments {
            seg.text.hash(&mut hasher);
        }
        self.config.title.hash(&mut hasher);
        format!("terminal-{}", hasher.finish())
    }

    fn build_chrome(&self, width: f64, height: f64, unique_id: &str) -> String {
        let bg = self.config.theme.background.hex();
        let fg = self.config.theme.foreground.hex();

        format!(
            r#"<rect fill="{bg}" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="{width}" height="{height}" rx="8"/>
<text class="{unique_id}-title" fill="{fg}" text-anchor="middle" x="{title_x}" y="26">{title}</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>"#,
            bg = bg,
            width = width,
            height = height,
            unique_id = unique_id,
            fg = fg,
            title_x = width / 2.0,
            title = svg_escape(&self.config.title)
        )
    }
}

fn svg_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace(' ', "&#160;")  // Non-breaking space
}

const DEFAULT_SVG_TEMPLATE: &str = r#"<svg class="rich-terminal" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">
<style>
@font-face {
    font-family: "Fira Code";
    src: local("FiraCode-Regular"),
         url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2");
    font-weight: 400;
}
.{unique_id}-matrix {
    font-family: Fira Code, monospace;
    font-size: {char_height}px;
    line-height: {line_height}px;
}
.{unique_id}-title {
    font-size: 18px;
    font-weight: bold;
    font-family: arial;
}
{styles}
</style>
<defs>
<clipPath id="{unique_id}-clip">
  <rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />
</clipPath>
</defs>
{chrome}
<g transform="translate({terminal_x}, {terminal_y})" clip-path="url(#{unique_id}-clip)">
{backgrounds}
<g class="{unique_id}-matrix">
{matrix}
</g>
</g>
</svg>"#;
```

### 10.6 Console Integration

```rust
impl Console {
    /// Export recorded output as HTML
    pub fn export_html(&self, config: HtmlExportConfig) -> Result<String, ExportError> {
        let buffer = self.record_buffer.read()
            .map_err(|_| ExportError::LockFailed)?;

        if buffer.is_empty() {
            return Err(ExportError::NoRecordedContent);
        }

        let exporter = HtmlExporter::new(&buffer, config);
        Ok(exporter.export())
    }

    /// Save recorded output as HTML file
    pub fn save_html(&self, path: impl AsRef<Path>, config: HtmlExportConfig) -> Result<(), ExportError> {
        let html = self.export_html(config)?;
        std::fs::write(path, html)?;
        Ok(())
    }

    /// Export recorded output as SVG
    pub fn export_svg(&self, config: SvgExportConfig) -> Result<String, ExportError> {
        let buffer = self.record_buffer.read()
            .map_err(|_| ExportError::LockFailed)?;

        if buffer.is_empty() {
            return Err(ExportError::NoRecordedContent);
        }

        let exporter = SvgExporter::new(&buffer, self.width(), config);
        Ok(exporter.export())
    }

    /// Save recorded output as SVG file
    pub fn save_svg(&self, path: impl AsRef<Path>, config: SvgExportConfig) -> Result<(), ExportError> {
        let svg = self.export_svg(config)?;
        std::fs::write(path, svg)?;
        Ok(())
    }

    /// Clear the record buffer
    pub fn clear_record_buffer(&self) {
        if let Ok(mut buffer) = self.record_buffer.write() {
            buffer.clear();
        }
    }
}

#[derive(Debug)]
pub enum ExportError {
    NoRecordedContent,
    LockFailed,
    IoError(std::io::Error),
}

impl From<std::io::Error> for ExportError {
    fn from(e: std::io::Error) -> Self {
        ExportError::IoError(e)
    }
}
```

### 10.7 Module Layout

```
src/export/
├── mod.rs           // Re-exports
├── theme.rs         // TerminalTheme + built-in themes
├── html.rs          // HtmlExporter, HtmlExportConfig
├── svg.rs           // SvgExporter, SvgExportConfig
└── templates/
    ├── html.html    // DEFAULT_HTML_TEMPLATE
    └── svg.svg      // DEFAULT_SVG_TEMPLATE
```

### 10.8 Usage Examples

```rust
use rich_rust::Console;
use rich_rust::export::{HtmlExportConfig, SvgExportConfig, MONOKAI};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create console with recording enabled
    let console = Console::builder()
        .record(true)
        .build();

    // Print styled content
    console.print(Panel::new("Hello, World!"))?;
    console.print(Table::from_data(&data))?;

    // Export as HTML with inline styles
    let html = console.export_html(HtmlExportConfig {
        inline_styles: true,
        ..Default::default()
    })?;

    // Export as SVG with custom theme
    let svg = console.export_svg(SvgExportConfig {
        theme: MONOKAI.clone(),
        title: "My Terminal Output".into(),
        ..Default::default()
    })?;

    // Save to files
    console.save_html("output.html", HtmlExportConfig::default())?;
    console.save_svg("output.svg", SvgExportConfig::default())?;

    Ok(())
}
```

### 10.9 Design Decisions

| Decision | Choice | Rationale |
|----------|--------|-----------|
| Separate configs | HtmlExportConfig / SvgExportConfig | Different use cases and options |
| Template strings | Include templates as const strs | Compile-time embedding, no runtime loading |
| Style deduplication | HashMap-based class generation | Smaller output, efficient |
| SVG unique IDs | Content hash | Allows multiple SVGs on one page |
| Font loading | CDN fallback for Fira Code | Works without local font install |
| Escape functions | Minimal, focused | Security without over-escaping |