gilt 1.7.1

Fast, beautiful terminal formatting for Rust — styles, tables, trees, syntax highlighting, progress bars, markdown.
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
//! Style representation and manipulation for terminal text.
//!
//! This module provides the Style type that represents the visual appearance
//! of terminal text, including colors, text attributes (bold, italic, etc.),
//! and hyperlinks.

use crate::color::{blend_rgb, Color, ColorSystem};
use crate::error::StyleError;
use crate::terminal_theme::TerminalTheme;
use std::fmt;
use std::fmt::Write as _;
use std::ops::Add;

/// Bit positions for text attributes.
const BOLD: u16 = 1 << 0;
const DIM: u16 = 1 << 1;
const ITALIC: u16 = 1 << 2;
const UNDERLINE: u16 = 1 << 3;
const BLINK: u16 = 1 << 4;
const BLINK2: u16 = 1 << 5;
const REVERSE: u16 = 1 << 6;
const CONCEAL: u16 = 1 << 7;
const STRIKE: u16 = 1 << 8;
const UNDERLINE2: u16 = 1 << 9;
const FRAME: u16 = 1 << 10;
const ENCIRCLE: u16 = 1 << 11;
const OVERLINE: u16 = 1 << 12;

/// Underline style variants for extended underline rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnderlineStyle {
    /// Standard single underline (SGR 4)
    Single,
    /// Double underline (SGR 21)
    Double,
    /// Curly/wavy underline (SGR 4:3)
    Curly,
    /// Dotted underline (SGR 4:4)
    Dotted,
    /// Dashed underline (SGR 4:5)
    Dashed,
}

/// A terminal text style with colors, attributes, and links.
#[derive(Clone, Debug)]
pub struct Style {
    /// Foreground color
    color: Option<Color>,
    /// Background color
    bgcolor: Option<Color>,
    /// Bit field of which attributes are set
    set_attributes: u16,
    /// Bit field of attribute values
    attributes: u16,
    /// Optional hyperlink URL
    link: Option<String>,
    /// Optional underline color (SGR 58)
    underline_color: Option<Color>,
    /// Optional underline style variant
    underline_style: Option<UnderlineStyle>,
}

impl Style {
    /// Creates a new style with specified attributes.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        color: Option<&str>,
        bgcolor: Option<&str>,
        bold: Option<bool>,
        dim: Option<bool>,
        italic: Option<bool>,
        underline: Option<bool>,
        blink: Option<bool>,
        blink2: Option<bool>,
        reverse: Option<bool>,
        conceal: Option<bool>,
        strike: Option<bool>,
        underline2: Option<bool>,
        frame: Option<bool>,
        encircle: Option<bool>,
        overline: Option<bool>,
        link: Option<&str>,
    ) -> Result<Self, StyleError> {
        let mut style = Style {
            color: None,
            bgcolor: None,
            set_attributes: 0,
            attributes: 0,
            link: None,
            underline_color: None,
            underline_style: None,
        };

        if let Some(c) = color {
            style.color = Some(
                Color::parse(c)
                    .map_err(|e| StyleError::InvalidSyntax(format!("invalid color: {}", e)))?,
            );
        }

        if let Some(bg) = bgcolor {
            style.bgcolor = Some(
                Color::parse(bg)
                    .map_err(|e| StyleError::InvalidSyntax(format!("invalid bgcolor: {}", e)))?,
            );
        }

        style.set_attribute(BOLD, bold);
        style.set_attribute(DIM, dim);
        style.set_attribute(ITALIC, italic);
        style.set_attribute(UNDERLINE, underline);
        style.set_attribute(BLINK, blink);
        style.set_attribute(BLINK2, blink2);
        style.set_attribute(REVERSE, reverse);
        style.set_attribute(CONCEAL, conceal);
        style.set_attribute(STRIKE, strike);
        style.set_attribute(UNDERLINE2, underline2);
        style.set_attribute(FRAME, frame);
        style.set_attribute(ENCIRCLE, encircle);
        style.set_attribute(OVERLINE, overline);

        if let Some(l) = link {
            style.link = Some(l.to_string());
        }

        Ok(style)
    }

    /// Creates an empty null style with no attributes set.
    pub fn null() -> Self {
        Style {
            color: None,
            bgcolor: None,
            set_attributes: 0,
            attributes: 0,
            link: None,
            underline_color: None,
            underline_style: None,
        }
    }

    /// Creates a style from optional colors.
    pub fn from_color(color: Option<Color>, bgcolor: Option<Color>) -> Self {
        Style {
            color,
            bgcolor,
            set_attributes: 0,
            attributes: 0,
            link: None,
            underline_color: None,
            underline_style: None,
        }
    }

    /// Parses a style definition string. **Lossy**: returns
    /// [`Style::null`] on any parse error.
    ///
    /// This is the recommended entry point for the common case of literal
    /// style strings (`"bold red"`, `"on blue"`, …) where a parse failure
    /// indicates a programming bug, not a recoverable runtime condition.
    /// For static literals there is nothing meaningful to do with an error
    /// at the callsite — the lossy form removes the boilerplate
    /// `unwrap_or_else(|_| Style::null())` that wrapped almost every
    /// previous use.
    ///
    /// # When to use [`parse_strict`](Self::parse_strict) instead
    ///
    /// - You're parsing user-supplied style strings (config files, CLI
    ///   flags) and want to surface a syntax error.
    /// - You want to write a unit test that verifies a particular input
    ///   *fails* to parse.
    ///
    /// # Examples
    ///
    /// ```
    /// use gilt::style::Style;
    ///
    /// let bold_red = Style::parse("bold red");          // no `?`, no `.unwrap()`
    /// let same     = Style::parse("BOLD RED");          // case-insensitive keywords
    /// let null     = Style::parse("not a real style");  // returns Style::null(), no panic
    /// assert!(null.is_null());
    /// ```
    pub fn parse(definition: &str) -> Self {
        Self::parse_strict(definition).unwrap_or_else(|_| Style::null())
    }

    /// Parses a style definition string. **Strict**: returns the parse
    /// error if the input is malformed.
    ///
    /// Prefer [`parse`](Self::parse) for the common case where a parse
    /// failure indicates a programming bug, not a recoverable runtime
    /// condition.
    ///
    /// # Grammar
    /// - Words are split by whitespace
    /// - `"on"` keyword: next word is background color
    /// - `"not"` keyword: next word is attribute name, set to false
    /// - `"link"` keyword: next word is URL
    /// - Known attribute names with aliases
    /// - Anything else: try as foreground color
    pub fn parse_strict(definition: &str) -> Result<Self, StyleError> {
        // Normalise the cache key to lowercase so "BOLD" and "bold" share
        // a single entry.
        let key = definition.to_lowercase();

        // Single lock scope: check cache, parse if missing, insert, drop.
        let mut cache = get_style_cache();
        if let Some(ref mut c) = *cache {
            if let Some(style) = c.get(key.as_str()) {
                return Ok(style.clone());
            }
        }

        // Parse (cache lock still held — recompute-and-insert is correct
        // under contention; no invariant requires exactly-once parsing).
        let style = Self::parse_internal(definition)?;

        if let Some(ref mut c) = *cache {
            c.put(key, style.clone());
        }

        Ok(style)
    }

    /// Internal parsing logic without caching.
    fn parse_internal(definition: &str) -> Result<Self, StyleError> {
        let definition = definition.trim();
        if definition.is_empty() || definition.eq_ignore_ascii_case("none") {
            return Ok(Style::null());
        }

        let mut style = Style::null();
        let words: Vec<&str> = definition.split_whitespace().collect();
        let mut i = 0;

        while i < words.len() {
            let word = words[i].to_lowercase();

            match word.as_str() {
                "on" => {
                    i += 1;
                    if i >= words.len() {
                        return Err(StyleError::InvalidSyntax(
                            "expected color after 'on'".to_string(),
                        ));
                    }
                    let bgcolor_str = words[i];
                    style.bgcolor = Some(Color::parse(bgcolor_str).map_err(|e| {
                        StyleError::InvalidSyntax(format!("invalid background color: {}", e))
                    })?);
                }
                "not" => {
                    i += 1;
                    if i >= words.len() {
                        return Err(StyleError::InvalidSyntax(
                            "expected attribute after 'not'".to_string(),
                        ));
                    }
                    let attr = words[i].to_lowercase();
                    if let Some(bit) = parse_attribute_name(&attr) {
                        style.set_attribute(bit, Some(false));
                    } else {
                        return Err(StyleError::UnknownAttribute(attr));
                    }
                }
                "link" => {
                    i += 1;
                    if i >= words.len() {
                        return Err(StyleError::InvalidSyntax(
                            "expected URL after 'link'".to_string(),
                        ));
                    }
                    style.link = Some(words[i].to_string());
                }
                _ => {
                    // Handle link=URL syntax (use original word to preserve URL case)
                    if word.starts_with("link=") {
                        let url = &words[i]["link=".len()..];
                        if url.is_empty() {
                            return Err(StyleError::InvalidSyntax(
                                "expected URL after 'link='".to_string(),
                            ));
                        }
                        style.link = Some(url.to_string());
                    } else if let Some(ul_style) = parse_underline_style_name(&word) {
                        // Underline style variant (from Display round-trip)
                        style.underline_style = Some(ul_style);
                    } else if word.starts_with("underline_color(") && word.ends_with(')') {
                        // underline_color(<name>) token emitted by Display
                        let inner = &word["underline_color(".len()..word.len() - 1];
                        let color = Color::parse(inner).map_err(|e| {
                            StyleError::InvalidSyntax(format!("invalid underline_color: {}", e))
                        })?;
                        style.underline_color = Some(color);
                    } else if let Some(bit) = parse_attribute_name(&word) {
                        // Try as attribute name
                        style.set_attribute(bit, Some(true));
                    } else {
                        // Try as foreground color
                        match Color::parse(&word) {
                            Ok(color) => style.color = Some(color),
                            Err(e) => {
                                return Err(StyleError::InvalidSyntax(format!(
                                    "unknown attribute or color '{}': {}",
                                    word, e
                                )))
                            }
                        }
                    }
                }
            }

            i += 1;
        }

        Ok(style)
    }

    /// Sets an attribute bit.
    fn set_attribute(&mut self, bit: u16, value: Option<bool>) {
        if let Some(val) = value {
            self.set_attributes |= bit;
            if val {
                self.attributes |= bit;
            } else {
                self.attributes &= !bit;
            }
        }
    }

    /// Gets an attribute value.
    fn get_attribute(&self, bit: u16) -> Option<bool> {
        if self.set_attributes & bit != 0 {
            Some(self.attributes & bit != 0)
        } else {
            None
        }
    }

    /// Returns the bold attribute.
    pub fn bold(&self) -> Option<bool> {
        self.get_attribute(BOLD)
    }

    /// Returns the dim attribute.
    pub fn dim(&self) -> Option<bool> {
        self.get_attribute(DIM)
    }

    /// Returns the italic attribute.
    pub fn italic(&self) -> Option<bool> {
        self.get_attribute(ITALIC)
    }

    /// Returns the underline attribute.
    pub fn underline(&self) -> Option<bool> {
        self.get_attribute(UNDERLINE)
    }

    /// Returns the blink attribute.
    pub fn blink(&self) -> Option<bool> {
        self.get_attribute(BLINK)
    }

    /// Returns the blink2 attribute.
    pub fn blink2(&self) -> Option<bool> {
        self.get_attribute(BLINK2)
    }

    /// Returns the reverse attribute.
    pub fn reverse(&self) -> Option<bool> {
        self.get_attribute(REVERSE)
    }

    /// Returns the conceal attribute.
    pub fn conceal(&self) -> Option<bool> {
        self.get_attribute(CONCEAL)
    }

    /// Returns the strike attribute.
    pub fn strike(&self) -> Option<bool> {
        self.get_attribute(STRIKE)
    }

    /// Returns the underline2 attribute.
    pub fn underline2(&self) -> Option<bool> {
        self.get_attribute(UNDERLINE2)
    }

    /// Returns the frame attribute.
    pub fn frame(&self) -> Option<bool> {
        self.get_attribute(FRAME)
    }

    /// Returns the encircle attribute.
    pub fn encircle(&self) -> Option<bool> {
        self.get_attribute(ENCIRCLE)
    }

    /// Returns the overline attribute.
    pub fn overline(&self) -> Option<bool> {
        self.get_attribute(OVERLINE)
    }

    /// Returns the foreground color.
    pub fn color(&self) -> Option<&Color> {
        self.color.as_ref()
    }

    /// Returns the background color.
    pub fn bgcolor(&self) -> Option<&Color> {
        self.bgcolor.as_ref()
    }

    /// Returns the link URL.
    pub fn link(&self) -> Option<&str> {
        self.link.as_deref()
    }

    /// Returns the underline color.
    pub fn underline_color(&self) -> Option<&Color> {
        self.underline_color.as_ref()
    }

    /// Returns the underline style.
    pub fn underline_style(&self) -> Option<UnderlineStyle> {
        self.underline_style
    }

    /// Sets the bold attribute.
    pub fn set_bold(&mut self, value: Option<bool>) {
        self.set_attribute(BOLD, value);
    }

    /// Sets the dim attribute.
    pub fn set_dim(&mut self, value: Option<bool>) {
        self.set_attribute(DIM, value);
    }

    /// Sets the italic attribute.
    pub fn set_italic(&mut self, value: Option<bool>) {
        self.set_attribute(ITALIC, value);
    }

    /// Sets the underline attribute.
    pub fn set_underline(&mut self, value: Option<bool>) {
        self.set_attribute(UNDERLINE, value);
    }

    /// Sets the blink attribute.
    pub fn set_blink(&mut self, value: Option<bool>) {
        self.set_attribute(BLINK, value);
    }

    /// Sets the reverse attribute.
    pub fn set_reverse(&mut self, value: Option<bool>) {
        self.set_attribute(REVERSE, value);
    }

    /// Sets the conceal attribute.
    pub fn set_conceal(&mut self, value: Option<bool>) {
        self.set_attribute(CONCEAL, value);
    }

    /// Sets the strike attribute.
    pub fn set_strike(&mut self, value: Option<bool>) {
        self.set_attribute(STRIKE, value);
    }

    /// Sets the underline color.
    pub fn set_underline_color(&mut self, color: Option<Color>) {
        self.underline_color = color;
    }

    /// Sets the underline style.
    pub fn set_underline_style(&mut self, style: Option<UnderlineStyle>) {
        self.underline_style = style;
    }

    /// Combines multiple styles into one (left-to-right merge).
    ///
    /// Prefer [`Style::combine_refs`] in hot paths — `combine` clones each
    /// input on the way through `Add<Style>`. The reference-based variant
    /// avoids the per-step clone.
    pub fn combine(styles: &[Style]) -> Style {
        styles
            .iter()
            .fold(Style::null(), |acc, style| acc + style.clone())
    }

    /// Alias for [`Style::combine`] matching Python rich's `Style.chain`.
    ///
    /// Applies each style left-to-right; later styles override earlier ones
    /// for conflicting attributes.
    pub fn chain(styles: &[Style]) -> Style {
        Self::combine(styles)
    }

    /// Returns the canonical string form of this style.
    ///
    /// Equivalent to formatting and re-parsing: `Style::parse(&format!("{}", self))`.
    /// Useful for normalising user-supplied style strings (config, CLI flags)
    /// to a canonical representation.
    ///
    /// Mirrors Python rich's `Style.normalize(definition)`.
    pub fn normalize(&self) -> String {
        self.to_string()
    }

    /// Like [`Style::combine`] but iterates over references — avoids the
    /// per-step `Style.clone()` that the by-value `Add<Style>` impl forces.
    ///
    /// Used by the per-segment span-rendering loop in `Text` where the same
    /// active span set is consulted thousands of times per render.
    pub fn combine_refs<'a, I>(styles: I) -> Style
    where
        I: IntoIterator<Item = &'a Style>,
    {
        let mut acc = Style::null();
        for style in styles {
            // Manually inline Add::add with rhs taken by reference — clones
            // only the chosen Option fields, not the entire rhs Style.
            acc = Style {
                color: style.color.or(acc.color),
                bgcolor: style.bgcolor.or(acc.bgcolor),
                set_attributes: acc.set_attributes | style.set_attributes,
                attributes: (acc.attributes & !style.set_attributes)
                    | (style.attributes & style.set_attributes),
                link: style.link.clone().or(acc.link),
                underline_color: style.underline_color.or(acc.underline_color),
                underline_style: style.underline_style.or(acc.underline_style),
            };
        }
        acc
    }

    /// Renders text with this style as ANSI escape sequences.
    /// Render `text` with this style's SGR codes (color, bold, etc.) but
    /// **without** wrapping in an OSC 8 hyperlink even if `self.link` is set.
    ///
    /// Used by `Console::render_buffer` when it has decided to coalesce a
    /// run of consecutive same-link segments under a single OSC 8 wrapper.
    ///
    /// Returns plain `text` when `color_system` is `None` or `text` is empty.
    pub fn render_no_link(&self, text: &str, color_system: Option<ColorSystem>) -> String {
        // Skip the OSC 8 wrapping branch — no clone of `self` needed.
        self.render_inner(text, color_system, false)
    }

    pub fn render(&self, text: &str, color_system: Option<ColorSystem>) -> String {
        self.render_inner(text, color_system, true)
    }

    /// Internal render path. `emit_link` controls whether `self.link` is
    /// wrapped in OSC 8 (the public `render` passes `true`; `render_no_link`
    /// passes `false` to avoid cloning Style just to strip the field).
    fn render_inner(
        &self,
        text: &str,
        color_system: Option<ColorSystem>,
        emit_link: bool,
    ) -> String {
        if text.is_empty() || color_system.is_none() {
            return text.to_string();
        }

        // Build semicolon-separated SGR codes directly into a buffer.
        // Pre-size for the typical "bold + 256-color fg + 256-color bg"
        // sequence (≈ "1;38;5;NNN;48;5;NNN" → ~20 chars).
        let mut sgr = String::with_capacity(32);

        // Add attribute codes
        let attrs: [(u16, &str); 13] = [
            (BOLD, "1"),
            (DIM, "2"),
            (ITALIC, "3"),
            (UNDERLINE, "4"),
            (BLINK, "5"),
            (BLINK2, "6"),
            (REVERSE, "7"),
            (CONCEAL, "8"),
            (STRIKE, "9"),
            (UNDERLINE2, "21"),
            (FRAME, "51"),
            (ENCIRCLE, "52"),
            (OVERLINE, "53"),
        ];

        for (bit, code) in &attrs {
            if self.attributes & bit != 0 && self.set_attributes & bit != 0 {
                if !sgr.is_empty() {
                    sgr.push(';');
                }
                sgr.push_str(code);
            }
        }

        // Underline style codes (extended underline)
        if let Some(ul_style) = &self.underline_style {
            if !sgr.is_empty() {
                sgr.push(';');
            }
            sgr.push_str(match ul_style {
                UnderlineStyle::Single => "4:1",
                UnderlineStyle::Double => "4:2",
                UnderlineStyle::Curly => "4:3",
                UnderlineStyle::Dotted => "4:4",
                UnderlineStyle::Dashed => "4:5",
            });
        }

        // Add color codes
        if let Some(color) = &self.color {
            color.write_ansi_codes(true, &mut sgr);
        }

        if let Some(bgcolor) = &self.bgcolor {
            bgcolor.write_ansi_codes(false, &mut sgr);
        }

        // Underline color (SGR 58;5;N or 58;2;R;G;B)
        if let Some(ul_color) = &self.underline_color {
            ul_color.write_underline_color_codes(&mut sgr);
        }

        // Pre-size: text + SGR opener (~5 + sgr) + reset (~4) + slack.
        let mut result = String::with_capacity(text.len() + sgr.len() + 12);

        if sgr.is_empty() {
            result.push_str(text);
        } else {
            write!(result, "\x1b[{}m{}\x1b[0m", sgr, text).unwrap();
        }

        // Wrap in hyperlink if present and the caller wants links emitted.
        //
        // Emit `id=N;url` so terminals that group multi-line links (iTerm2,
        // Kitty, WezTerm) recognise runs as a single clickable target.
        // The id is derived deterministically from the URL string via a
        // FNV-1a 64-bit hash, so repeated `render()` calls on the same
        // Style (or different Style instances with the same link) produce
        // the same id — no mutable state or extra fields required.
        if emit_link {
            if let Some(url) = &self.link {
                let id = link_id_for(url);
                let mut linked = String::with_capacity(result.len() + url.len() + 32);
                write!(
                    linked,
                    "\x1b]8;id={};{}\x1b\\{}\x1b]8;;\x1b\\",
                    id, url, result
                )
                .unwrap();
                return linked;
            }
        }
        result
    }

    /// Pick the first non-null style from the candidate list, or [`Style::null`]
    /// if every candidate is null/None.
    ///
    /// Mirrors rich's `Style.pick_first(*candidates)` — useful in render
    /// pipelines that select among theme / row / column / cell style overrides.
    pub fn pick_first(candidates: &[Option<&Style>]) -> Style {
        for s in candidates.iter().flatten() {
            if !s.is_null() {
                return (*s).clone();
            }
        }
        Style::null()
    }

    /// Returns true if this is a null style (nothing set).
    pub fn is_null(&self) -> bool {
        self.color.is_none()
            && self.bgcolor.is_none()
            && self.set_attributes == 0
            && self.link.is_none()
            && self.underline_color.is_none()
            && self.underline_style.is_none()
    }

    // -- Typed builder methods (Task 3) ------------------------------------

    /// Set the foreground color from a typed [`Color`] value (builder-style).
    ///
    /// # Examples
    ///
    /// ```
    /// use gilt::style::Style;
    /// use gilt::color::Color;
    ///
    /// let s = Style::null().fg(Color::from_rgb(255, 0, 0));
    /// assert!(s.color().is_some());
    /// ```
    #[must_use]
    pub fn fg(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Set the background color from a typed [`Color`] value (builder-style).
    ///
    /// # Examples
    ///
    /// ```
    /// use gilt::style::Style;
    /// use gilt::color::Color;
    ///
    /// let s = Style::null().bg(Color::from_rgb(0, 0, 255));
    /// assert!(s.bgcolor().is_some());
    /// ```
    #[must_use]
    pub fn bg(mut self, color: Color) -> Self {
        self.bgcolor = Some(color);
        self
    }

    /// Set the underline color from a typed [`Color`] value (builder-style).
    ///
    /// # Examples
    ///
    /// ```
    /// use gilt::style::Style;
    /// use gilt::color::Color;
    ///
    /// let s = Style::null().with_underline_color(Color::from_rgb(0, 255, 0));
    /// assert!(s.underline_color().is_some());
    /// ```
    #[must_use]
    pub fn with_underline_color(mut self, color: Color) -> Self {
        self.underline_color = Some(color);
        self
    }

    /// Returns a copy of this style without colors.
    pub fn without_color(&self) -> Style {
        Style {
            color: None,
            bgcolor: None,
            set_attributes: self.set_attributes,
            attributes: self.attributes,
            link: self.link.clone(),
            underline_color: self.underline_color,
            underline_style: self.underline_style,
        }
    }

    /// Returns a style with only the background color.
    pub fn background_style(&self) -> Style {
        Style {
            color: None,
            bgcolor: self.bgcolor,
            set_attributes: 0,
            attributes: 0,
            link: None,
            underline_color: None,
            underline_style: None,
        }
    }

    /// Returns a copy without metadata and links.
    pub fn clear_meta_and_links(&self) -> Style {
        Style {
            color: self.color,
            bgcolor: self.bgcolor,
            set_attributes: self.set_attributes,
            attributes: self.attributes,
            link: None,
            underline_color: self.underline_color,
            underline_style: self.underline_style,
        }
    }

    /// Returns a copy of this style with the given hyperlink URL.
    pub fn with_link(url: &str) -> Style {
        Style {
            color: None,
            bgcolor: None,
            set_attributes: 0,
            attributes: 0,
            link: Some(url.to_string()),
            underline_color: None,
            underline_style: None,
        }
    }

    /// Returns a copy with an updated link.
    pub fn update_link(&self, link: Option<&str>) -> Style {
        Style {
            color: self.color,
            bgcolor: self.bgcolor,
            set_attributes: self.set_attributes,
            attributes: self.attributes,
            link: link.map(|s| s.to_string()),
            underline_color: self.underline_color,
            underline_style: self.underline_style,
        }
    }

    /// Returns a CSS style string for HTML rendering.
    pub fn get_html_style(&self, theme: Option<&TerminalTheme>) -> String {
        let mut css = String::new();

        let mut fg_color = self.color.as_ref();
        let mut bg_color = self.bgcolor.as_ref();

        // Handle reverse
        if self.reverse() == Some(true) {
            std::mem::swap(&mut fg_color, &mut bg_color);
        }

        // Get color triplets
        let mut fg_triplet = fg_color.map(|c| c.get_truecolor(theme, true));
        let bg_triplet = bg_color.map(|c| c.get_truecolor(theme, false));

        // Handle dim
        if self.dim() == Some(true) {
            if let (Some(fg), Some(bg)) = (fg_triplet, bg_triplet) {
                fg_triplet = Some(blend_rgb(fg, bg, 0.5));
            }
        }

        // Color
        if let Some(triplet) = fg_triplet {
            let hex = triplet.hex();
            write!(css, "color: {}; text-decoration-color: {}", hex, hex).unwrap();
        }

        // Background color
        if let Some(triplet) = bg_triplet {
            if !css.is_empty() {
                css.push_str("; ");
            }
            write!(css, "background-color: {}", triplet.hex()).unwrap();
        }

        // Bold
        if self.bold() == Some(true) {
            if !css.is_empty() {
                css.push_str("; ");
            }
            css.push_str("font-weight: bold");
        }

        // Italic
        if self.italic() == Some(true) {
            if !css.is_empty() {
                css.push_str("; ");
            }
            css.push_str("font-style: italic");
        }

        // Text decorations
        let has_underline = self.underline() == Some(true);
        let has_strike = self.strike() == Some(true);
        let has_overline = self.overline() == Some(true);
        if has_underline || has_strike || has_overline {
            if !css.is_empty() {
                css.push_str("; ");
            }
            css.push_str("text-decoration: ");
            let mut first = true;
            if has_underline {
                css.push_str("underline");
                first = false;
            }
            if has_strike {
                if !first {
                    css.push(' ');
                }
                css.push_str("line-through");
                first = false;
            }
            if has_overline {
                if !first {
                    css.push(' ');
                }
                css.push_str("overline");
            }
        }

        css
    }
}

impl fmt::Display for Style {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut parts = Vec::new();

        // Attributes
        let attrs = [
            (BOLD, "bold", "not bold"),
            (DIM, "dim", "not dim"),
            (ITALIC, "italic", "not italic"),
            (UNDERLINE, "underline", "not underline"),
            (BLINK, "blink", "not blink"),
            (BLINK2, "blink2", "not blink2"),
            (REVERSE, "reverse", "not reverse"),
            (CONCEAL, "conceal", "not conceal"),
            (STRIKE, "strike", "not strike"),
            (UNDERLINE2, "underline2", "not underline2"),
            (FRAME, "frame", "not frame"),
            (ENCIRCLE, "encircle", "not encircle"),
            (OVERLINE, "overline", "not overline"),
        ];

        for (bit, on_name, off_name) in &attrs {
            if self.set_attributes & bit != 0 {
                if self.attributes & bit != 0 {
                    parts.push(on_name.to_string());
                } else {
                    parts.push(off_name.to_string());
                }
            }
        }

        // Foreground color
        if let Some(color) = &self.color {
            parts.push(color.name().into_owned());
        }

        // Background color
        if let Some(bgcolor) = &self.bgcolor {
            parts.push("on".to_string());
            parts.push(bgcolor.name().into_owned());
        }

        // Underline style — match to a static &'static str (avoids two
        // allocations from `format!("{:?}").to_lowercase()`).
        if let Some(ul_style) = &self.underline_style {
            parts.push(
                match ul_style {
                    UnderlineStyle::Single => "single",
                    UnderlineStyle::Double => "double",
                    UnderlineStyle::Curly => "curly",
                    UnderlineStyle::Dotted => "dotted",
                    UnderlineStyle::Dashed => "dashed",
                }
                .to_string(),
            );
        }

        // Underline color
        if let Some(ul_color) = &self.underline_color {
            parts.push(format!("underline_color({})", ul_color.name()));
        }

        // Link
        if let Some(link) = &self.link {
            parts.push("link".to_string());
            parts.push(link.clone());
        }

        if parts.is_empty() {
            write!(f, "none")
        } else {
            write!(f, "{}", parts.join(" "))
        }
    }
}

impl PartialEq for Style {
    fn eq(&self, other: &Self) -> bool {
        self.color == other.color
            && self.bgcolor == other.bgcolor
            && self.set_attributes == other.set_attributes
            && self.attributes == other.attributes
            && self.link == other.link
            && self.underline_color == other.underline_color
            && self.underline_style == other.underline_style
    }
}

impl std::hash::Hash for Style {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.color.hash(state);
        self.bgcolor.hash(state);
        self.set_attributes.hash(state);
        self.attributes.hash(state);
        self.link.hash(state);
        self.underline_color.hash(state);
        self.underline_style.hash(state);
    }
}

impl Eq for Style {}

impl Add<Style> for Style {
    type Output = Style;

    fn add(self, rhs: Style) -> Style {
        Style {
            color: rhs.color.or(self.color),
            bgcolor: rhs.bgcolor.or(self.bgcolor),
            set_attributes: self.set_attributes | rhs.set_attributes,
            attributes: (self.attributes & !rhs.set_attributes)
                | (rhs.attributes & rhs.set_attributes),
            link: rhs.link.or(self.link),
            underline_color: rhs.underline_color.or(self.underline_color),
            underline_style: rhs.underline_style.or(self.underline_style),
        }
    }
}

impl Add<Option<Style>> for Style {
    type Output = Style;

    fn add(self, rhs: Option<Style>) -> Style {
        match rhs {
            Some(style) => self + style,
            None => self,
        }
    }
}

/// Parses an underline-style token (emitted by [`Display`]) to its variant.
fn parse_underline_style_name(name: &str) -> Option<UnderlineStyle> {
    match name {
        "single" => Some(UnderlineStyle::Single),
        "double" => Some(UnderlineStyle::Double),
        "curly" => Some(UnderlineStyle::Curly),
        "dotted" => Some(UnderlineStyle::Dotted),
        "dashed" => Some(UnderlineStyle::Dashed),
        _ => None,
    }
}

/// Parses an attribute name to its bit mask.
fn parse_attribute_name(name: &str) -> Option<u16> {
    match name {
        "bold" | "b" => Some(BOLD),
        "dim" | "d" => Some(DIM),
        "italic" | "i" => Some(ITALIC),
        "underline" | "u" => Some(UNDERLINE),
        "blink" => Some(BLINK),
        "blink2" => Some(BLINK2),
        "reverse" | "r" => Some(REVERSE),
        "conceal" | "c" => Some(CONCEAL),
        "strike" | "s" => Some(STRIKE),
        "underline2" | "uu" => Some(UNDERLINE2),
        "frame" => Some(FRAME),
        "encircle" => Some(ENCIRCLE),
        "overline" | "o" => Some(OVERLINE),
        _ => None,
    }
}

/// A stack of styles for managing nested style contexts.
#[derive(Debug, Clone)]
pub struct StyleStack {
    stack: Vec<Style>,
}

impl StyleStack {
    /// Creates a new style stack with a default style.
    pub fn new(default: Style) -> Self {
        StyleStack {
            stack: vec![default],
        }
    }

    /// Returns the current (top) style.
    pub fn current(&self) -> &Style {
        self.stack.last().expect("StyleStack should never be empty")
    }

    /// Pushes a new style, combining it with the current style.
    pub fn push(&mut self, style: Style) {
        let new_style = self.current().clone() + style;
        self.stack.push(new_style);
    }

    /// Pops the top style and returns the new current style.
    pub fn pop(&mut self) -> Result<&Style, StyleError> {
        if self.stack.len() <= 1 {
            return Err(StyleError::StackError(
                "cannot pop from stack with only default style".to_string(),
            ));
        }
        self.stack.pop();
        Ok(self.current())
    }
}

// ============================================================================
// LRU Cache for Style Parsing
// ============================================================================

use lru::LruCache;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;

/// Process-wide monotonic counter for OSC 8 `id=` parameters.
///
/// Retained for tests that assert monotonic behaviour; the render path now
/// uses [`link_id_for`] instead.
static LINK_ID_COUNTER: AtomicU64 = AtomicU64::new(1);

/// Return a fresh OSC 8 link id, monotonically increasing for the process.
pub(crate) fn next_link_id() -> u64 {
    LINK_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
}

/// Derive a stable OSC 8 `id=` value from a URL string using FNV-1a 64-bit.
///
/// Using a hash of the URL means identical links on different Style instances
/// produce the same id, and repeated `render()` calls on the same Style are
/// idempotent — no mutable state or extra fields needed.
fn link_id_for(url: &str) -> u64 {
    const FNV_OFFSET: u64 = 14_695_981_039_346_656_037;
    const FNV_PRIME: u64 = 1_099_511_628_211;
    let mut hash = FNV_OFFSET;
    for byte in url.bytes() {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

/// Global LRU cache for parsed styles with capacity for 256 entries.
static STYLE_CACHE: Mutex<Option<LruCache<String, Style>>> = Mutex::new(None);

/// Gets or initializes the style cache.
///
/// Recovers from a poisoned mutex (after a panic in a previous holder) by
/// extracting the inner value — the cache is purely a parse accelerator, so
/// the data behind a poison flag is still safe to use.
fn get_style_cache() -> std::sync::MutexGuard<'static, Option<LruCache<String, Style>>> {
    let mut cache = STYLE_CACHE
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    if cache.is_none() {
        *cache = Some(LruCache::new(NonZeroUsize::new(256).unwrap()));
    }
    cache
}

/// Clears the global style cache.
pub fn clear_style_cache() {
    if let Ok(mut cache) = STYLE_CACHE.lock() {
        *cache = None;
    }
}

/// Returns the current number of entries in the style cache.
pub fn style_cache_size() -> usize {
    if let Ok(cache) = STYLE_CACHE.lock() {
        cache.as_ref().map(|c| c.len()).unwrap_or(0)
    } else {
        0
    }
}

// ---------------------------------------------------------------------------
// serde Serialize / Deserialize for Style (gated on `json` feature)
// ---------------------------------------------------------------------------
//
// `Style` serializes as its canonical `Display` string (e.g. `"bold red"`,
// `"on blue"`, `"none"`) and deserializes via `Style::parse_strict`.
// A custom impl is used because the bit-packed internal representation has
// no meaningful automatic serde mapping.

#[cfg(feature = "json")]
impl serde::Serialize for Style {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.to_string())
    }
}

#[cfg(feature = "json")]
impl<'de> serde::Deserialize<'de> for Style {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        use serde::de::{self, Visitor};
        struct StyleVisitor;
        impl<'de> Visitor<'de> for StyleVisitor {
            type Value = Style;
            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "a style string like \"bold red\" or \"italic on blue\"")
            }
            fn visit_str<E: de::Error>(self, v: &str) -> Result<Style, E> {
                Style::parse_strict(v).map_err(|e| de::Error::custom(e.to_string()))
            }
        }
        d.deserialize_str(StyleVisitor)
    }
}

#[cfg(test)]
#[path = "style_tests.rs"]
mod tests;