opentui_rust 0.2.1

High-performance terminal UI rendering engine with alpha blending and diffed buffers
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
//! Text styling with attributes and colors.
//!
//! This module provides types for styling text in the terminal:
//!
//! - [`TextAttributes`]: Bitflags for bold, italic, underline, etc.
//! - [`Style`]: Complete styling including colors, attributes, and hyperlinks
//! - [`StyleBuilder`]: Fluent builder for constructing styles
//!
//! # Examples
//!
//! ```
//! use opentui_rust::{Style, TextAttributes, Rgba};
//!
//! // Quick style creation
//! let title_style = Style::fg(Rgba::WHITE).with_bold();
//!
//! // Builder pattern for complex styles
//! let highlight = Style::builder()
//!     .fg(Rgba::from_hex("#FFD700").unwrap())
//!     .bg(Rgba::from_hex("#1a1a2e").unwrap())
//!     .bold()
//!     .underline()
//!     .build();
//!
//! // Merge styles (overlay takes precedence)
//! let combined = Style::bold().merge(Style::fg(Rgba::RED));
//! ```

use crate::color::Rgba;
use bitflags::bitflags;

bitflags! {
    /// Text rendering attributes (bold, italic, underline, etc.).
    ///
    /// Attributes are represented as bitflags and can be combined using
    /// bitwise OR. Not all terminals support all attributes.
    ///
    /// Link IDs are packed into the upper 24 bits to match the Zig spec.
    #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
    pub struct TextAttributes: u32 {
        /// Bold/increased intensity.
        const BOLD          = 0x01;
        /// Dim/decreased intensity.
        const DIM           = 0x02;
        /// Italic (not widely supported).
        const ITALIC        = 0x04;
        /// Underlined text.
        const UNDERLINE     = 0x08;
        /// Blinking text (rarely supported).
        const BLINK         = 0x10;
        /// Swapped foreground/background.
        const INVERSE       = 0x20;
        /// Hidden/invisible text.
        const HIDDEN        = 0x40;
        /// Strikethrough text.
        const STRIKETHROUGH = 0x80;
    }
}

impl TextAttributes {
    /// Mask for the lower 8 bits containing style flags.
    pub const FLAGS_MASK: u32 = 0x0000_00FF;
    /// Mask for the upper 24 bits containing link ID.
    pub const LINK_ID_MASK: u32 = 0xFFFF_FF00;
    /// Bit shift for link ID storage.
    pub const LINK_ID_SHIFT: u32 = 8;
    /// Maximum link ID that fits in 24 bits.
    pub const MAX_LINK_ID: u32 = 0x00FF_FFFF;

    /// Extract the link ID (if any).
    #[must_use]
    pub const fn link_id(self) -> Option<u32> {
        let id = (self.bits() & Self::LINK_ID_MASK) >> Self::LINK_ID_SHIFT;
        if id == 0 { None } else { Some(id) }
    }

    /// Return attributes with a link ID set (masked to 24 bits).
    #[must_use]
    pub const fn with_link_id(self, link_id: u32) -> Self {
        let id = link_id & Self::MAX_LINK_ID;
        let bits = (self.bits() & Self::FLAGS_MASK) | (id << Self::LINK_ID_SHIFT);
        Self::from_bits_retain(bits)
    }

    /// Clear the link ID, preserving style flags.
    #[must_use]
    pub const fn clear_link_id(self) -> Self {
        Self::from_bits_retain(self.bits() & Self::FLAGS_MASK)
    }

    /// Return only the style flags (link ID cleared).
    #[must_use]
    pub const fn flags_only(self) -> Self {
        Self::from_bits_retain(self.bits() & Self::FLAGS_MASK)
    }

    /// Merge attributes: OR flags, prefer `other` link ID when set.
    #[must_use]
    pub const fn merge(self, other: Self) -> Self {
        let flags = (self.bits() | other.bits()) & Self::FLAGS_MASK;
        let link_bits = if (other.bits() & Self::LINK_ID_MASK) != 0 {
            other.bits() & Self::LINK_ID_MASK
        } else {
            self.bits() & Self::LINK_ID_MASK
        };
        Self::from_bits_retain(flags | link_bits)
    }

    /// Set the link ID in place.
    pub fn set_link_id(&mut self, link_id: u32) {
        *self = self.with_link_id(link_id);
    }
}

/// Complete text style including colors, attributes, and optional hyperlink.
///
/// Styles are immutable and cheap to copy. Use the builder methods to create
/// modified versions, or [`Style::merge`] to combine multiple styles.
///
/// # Default Values
///
/// `None` for colors means "use terminal default" rather than a specific color.
/// This allows styled text to respect the user's terminal theme.
///
/// # Hyperlinks
///
/// Link IDs are packed into [`TextAttributes`] (bits 8-31) and reference
/// URLs stored in a [`LinkPool`](crate::LinkPool).
/// Terminals supporting OSC 8 will render these as clickable links.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Style {
    /// Foreground color (None = terminal default).
    pub fg: Option<Rgba>,
    /// Background color (None = terminal default).
    pub bg: Option<Rgba>,
    /// Text rendering attributes.
    pub attributes: TextAttributes,
}

impl Style {
    /// Empty style with no colors or attributes.
    pub const NONE: Self = Self {
        fg: None,
        bg: None,
        attributes: TextAttributes::empty(),
    };

    /// Create a new style builder.
    #[must_use]
    pub fn builder() -> StyleBuilder {
        StyleBuilder::default()
    }

    /// Create a style with only foreground color.
    #[must_use]
    pub const fn fg(color: Rgba) -> Self {
        Self {
            fg: Some(color),
            bg: None,
            attributes: TextAttributes::empty(),
        }
    }

    /// Create a style with only background color.
    #[must_use]
    pub const fn bg(color: Rgba) -> Self {
        Self {
            fg: None,
            bg: Some(color),
            attributes: TextAttributes::empty(),
        }
    }

    /// Create a bold style.
    #[must_use]
    pub const fn bold() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::BOLD,
        }
    }

    /// Create an italic style.
    #[must_use]
    pub const fn italic() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::ITALIC,
        }
    }

    /// Create an underline style.
    #[must_use]
    pub const fn underline() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::UNDERLINE,
        }
    }

    /// Create a dim style.
    #[must_use]
    pub const fn dim() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::DIM,
        }
    }

    /// Create an inverse (swapped fg/bg) style.
    #[must_use]
    pub const fn inverse() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::INVERSE,
        }
    }

    /// Create a strikethrough style.
    #[must_use]
    pub const fn strikethrough() -> Self {
        Self {
            fg: None,
            bg: None,
            attributes: TextAttributes::STRIKETHROUGH,
        }
    }

    /// Return a new style with the specified foreground color.
    #[must_use]
    pub const fn with_fg(self, color: Rgba) -> Self {
        Self {
            fg: Some(color),
            ..self
        }
    }

    /// Return a new style with the specified background color.
    #[must_use]
    pub const fn with_bg(self, color: Rgba) -> Self {
        Self {
            bg: Some(color),
            ..self
        }
    }

    /// Return a new style with the specified attributes added.
    #[must_use]
    pub const fn with_attributes(self, attrs: TextAttributes) -> Self {
        Self {
            attributes: self.attributes.merge(attrs),
            ..self
        }
    }

    /// Return a new style with the bold attribute added.
    #[must_use]
    pub const fn with_bold(self) -> Self {
        self.with_attributes(TextAttributes::BOLD)
    }

    /// Return a new style with the italic attribute added.
    #[must_use]
    pub const fn with_italic(self) -> Self {
        self.with_attributes(TextAttributes::ITALIC)
    }

    /// Return a new style with the underline attribute added.
    #[must_use]
    pub const fn with_underline(self) -> Self {
        self.with_attributes(TextAttributes::UNDERLINE)
    }

    /// Return a new style with a hyperlink ID.
    #[must_use]
    pub const fn with_link(self, link_id: u32) -> Self {
        Self {
            attributes: self.attributes.with_link_id(link_id),
            ..self
        }
    }

    /// Check if this style has any non-default properties.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.fg.is_none() && self.bg.is_none() && self.attributes.is_empty()
    }

    /// Merge two styles, with `other` taking precedence for set values.
    #[must_use]
    pub fn merge(self, other: Self) -> Self {
        Self {
            fg: other.fg.or(self.fg),
            bg: other.bg.or(self.bg),
            attributes: self.attributes.merge(other.attributes),
        }
    }
}

/// Builder for creating styles fluently.
#[derive(Clone, Debug, Default)]
pub struct StyleBuilder {
    style: Style,
}

impl StyleBuilder {
    /// Set foreground color.
    #[must_use]
    pub fn fg(mut self, color: Rgba) -> Self {
        self.style.fg = Some(color);
        self
    }

    /// Set background color.
    #[must_use]
    pub fn bg(mut self, color: Rgba) -> Self {
        self.style.bg = Some(color);
        self
    }

    /// Add bold attribute.
    #[must_use]
    pub fn bold(mut self) -> Self {
        self.style.attributes |= TextAttributes::BOLD;
        self
    }

    /// Add dim attribute.
    #[must_use]
    pub fn dim(mut self) -> Self {
        self.style.attributes |= TextAttributes::DIM;
        self
    }

    /// Add italic attribute.
    #[must_use]
    pub fn italic(mut self) -> Self {
        self.style.attributes |= TextAttributes::ITALIC;
        self
    }

    /// Add underline attribute.
    #[must_use]
    pub fn underline(mut self) -> Self {
        self.style.attributes |= TextAttributes::UNDERLINE;
        self
    }

    /// Add blink attribute.
    #[must_use]
    pub fn blink(mut self) -> Self {
        self.style.attributes |= TextAttributes::BLINK;
        self
    }

    /// Add inverse attribute.
    #[must_use]
    pub fn inverse(mut self) -> Self {
        self.style.attributes |= TextAttributes::INVERSE;
        self
    }

    /// Add hidden attribute.
    #[must_use]
    pub fn hidden(mut self) -> Self {
        self.style.attributes |= TextAttributes::HIDDEN;
        self
    }

    /// Add strikethrough attribute.
    #[must_use]
    pub fn strikethrough(mut self) -> Self {
        self.style.attributes |= TextAttributes::STRIKETHROUGH;
        self
    }

    /// Set hyperlink ID.
    #[must_use]
    pub fn link(mut self, link_id: u32) -> Self {
        self.style.attributes = self.style.attributes.with_link_id(link_id);
        self
    }

    /// Build the final style.
    #[must_use]
    pub fn build(self) -> Style {
        self.style
    }
}

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

    #[test]
    fn test_style_builder() {
        let style = Style::builder()
            .fg(Rgba::RED)
            .bg(Rgba::BLACK)
            .bold()
            .underline()
            .build();

        assert_eq!(style.fg, Some(Rgba::RED));
        assert_eq!(style.bg, Some(Rgba::BLACK));
        assert!(style.attributes.contains(TextAttributes::BOLD));
        assert!(style.attributes.contains(TextAttributes::UNDERLINE));
    }

    #[test]
    fn test_style_merge() {
        let base = Style::fg(Rgba::RED).with_bold();
        let overlay = Style::bg(Rgba::BLUE).with_italic();

        let merged = base.merge(overlay);

        assert_eq!(merged.fg, Some(Rgba::RED));
        assert_eq!(merged.bg, Some(Rgba::BLUE));
        assert!(merged.attributes.contains(TextAttributes::BOLD));
        assert!(merged.attributes.contains(TextAttributes::ITALIC));
    }

    #[test]
    fn test_const_styles() {
        assert!(Style::bold().attributes.contains(TextAttributes::BOLD));
        assert!(Style::italic().attributes.contains(TextAttributes::ITALIC));
        assert!(
            Style::underline()
                .attributes
                .contains(TextAttributes::UNDERLINE)
        );
    }

    #[test]
    fn test_text_attributes_link_id_packing() {
        let attrs = TextAttributes::BOLD.with_link_id(0x12_3456);
        assert!(attrs.contains(TextAttributes::BOLD));
        assert_eq!(attrs.link_id(), Some(0x12_3456));
        assert_eq!(attrs.flags_only(), TextAttributes::BOLD);
    }

    #[test]
    fn test_text_attributes_merge_link_id_preference() {
        let base = TextAttributes::BOLD.with_link_id(1);
        let overlay_no_link = TextAttributes::ITALIC;
        let merged = base.merge(overlay_no_link);
        assert_eq!(merged.link_id(), Some(1));
        assert!(merged.contains(TextAttributes::BOLD));
        assert!(merged.contains(TextAttributes::ITALIC));

        let overlay_with_link = TextAttributes::UNDERLINE.with_link_id(2);
        let merged_with_link = base.merge(overlay_with_link);
        assert_eq!(merged_with_link.link_id(), Some(2));
        assert!(merged_with_link.contains(TextAttributes::BOLD));
        assert!(merged_with_link.contains(TextAttributes::UNDERLINE));
    }

    #[test]
    fn test_text_attributes_link_id_masking() {
        let attrs = TextAttributes::empty().with_link_id(0x1FF_FFFF);
        assert_eq!(attrs.link_id(), Some(TextAttributes::MAX_LINK_ID));
    }
}