richrs 0.2.1

A Rust port of the Rich Python 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
// Static regex uses unwrap on known-valid pattern
#![allow(clippy::unwrap_used)]
#![allow(clippy::panic)]

//! Markup parsing for console output.
//!
//! This module provides Rich-compatible markup syntax parsing.
//! Markup allows inline styling using tags like `[bold red]text[/]`.

use crate::errors::{Error, Result};
use crate::style::Style;
use crate::text::Text;
use regex::Regex;
use std::fmt;
use std::sync::LazyLock;

/// A regex pattern for matching markup tags.
static TAG_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
    // This pattern is known to be valid at compile time
    Regex::new(r"\[(/?)([^\]]*)\]").unwrap()
});

/// Represents a parsed markup tag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag {
    /// Opening style tag with style definition.
    Open(Style),
    /// Closing tag (resets to previous style).
    Close,
    /// Named closing tag (closes specific style).
    CloseNamed(String),
}

impl Tag {
    /// Parses a tag from its content (without brackets).
    ///
    /// # Errors
    ///
    /// Returns an error if the tag content cannot be parsed.
    pub fn parse(content: &str, is_closing: bool) -> Result<Self> {
        let content = content.trim();

        if is_closing {
            if content.is_empty() {
                return Ok(Self::Close);
            }
            return Ok(Self::CloseNamed(content.to_owned()));
        }

        // Parse as style
        let style = Style::parse(content)?;
        Ok(Self::Open(style))
    }
}

/// A segment of parsed markup (either text or a tag).
#[derive(Debug, Clone)]
pub enum MarkupSegment {
    /// Plain text content.
    Text(String),
    /// A markup tag.
    Tag(Tag),
}

/// Parsed markup that can be converted to styled text.
#[derive(Debug, Clone, Default)]
pub struct Markup {
    /// The segments that make up this markup.
    segments: Vec<MarkupSegment>,
}

impl Markup {
    /// Creates empty markup.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            segments: Vec::new(),
        }
    }

    /// Parses markup from a string.
    ///
    /// # Syntax
    ///
    /// - `[style]text[/]` - Apply style to text
    /// - `[bold]`, `[italic]`, etc. - Style attributes
    /// - `[red]`, `[#ff0000]`, etc. - Colors
    /// - `[bold red on white]` - Combined styles
    /// - `[/]` - Close the most recent tag
    /// - `[/bold]` - Close a specific style (for nested styles)
    /// - `\\[` - Escaped bracket (literal `[`)
    ///
    /// # Errors
    ///
    /// Returns an error if the markup syntax is invalid.
    pub fn parse(s: &str) -> Result<Self> {
        let mut markup = Self::new();
        let mut last_end = 0;

        for cap in TAG_PATTERN.captures_iter(s) {
            let full_match = cap.get(0).ok_or_else(|| Error::MarkupParse {
                message: "regex capture failed".to_owned(),
            })?;

            // Check if this tag is escaped
            let match_start = full_match.start();
            if match_start > 0 {
                let prev_char_start = match_start.saturating_sub(1);
                if s.get(prev_char_start..match_start) == Some("\\") {
                    continue;
                }
            }

            // Add text before this tag
            if match_start > last_end {
                let text = s
                    .get(last_end..match_start)
                    .ok_or_else(|| Error::MarkupParse {
                        message: "invalid string slice".to_owned(),
                    })?;
                let unescaped = unescape_brackets(text);
                if !unescaped.is_empty() {
                    markup.segments.push(MarkupSegment::Text(unescaped));
                }
            }

            // Parse the tag
            let is_closing = cap.get(1).map(|m| m.as_str() == "/").unwrap_or(false);
            let tag_content = cap.get(2).map(|m| m.as_str()).unwrap_or("");

            // Skip if the tag is just whitespace inside brackets
            if tag_content.trim().is_empty() && !is_closing {
                // Treat as literal text
                let tag_text = full_match.as_str();
                markup
                    .segments
                    .push(MarkupSegment::Text(tag_text.to_owned()));
            } else {
                let tag = Tag::parse(tag_content, is_closing)?;
                markup.segments.push(MarkupSegment::Tag(tag));
            }

            last_end = full_match.end();
        }

        // Add remaining text
        if last_end < s.len() {
            let text = s.get(last_end..).ok_or_else(|| Error::MarkupParse {
                message: "invalid string slice".to_owned(),
            })?;
            let unescaped = unescape_brackets(text);
            if !unescaped.is_empty() {
                markup.segments.push(MarkupSegment::Text(unescaped));
            }
        }

        Ok(markup)
    }

    /// Converts the parsed markup to a Text object.
    #[must_use]
    pub fn to_text(&self) -> Text {
        let mut text = Text::new();
        let mut style_stack: Vec<Style> = Vec::new();

        for segment in &self.segments {
            match segment {
                MarkupSegment::Text(s) => {
                    let current_style = style_stack.last().cloned().unwrap_or_default();
                    if current_style.is_empty() {
                        text.append_plain(s);
                    } else {
                        text.append_styled(s, current_style);
                    }
                }
                MarkupSegment::Tag(tag) => match tag {
                    Tag::Open(style) => {
                        // Combine with current style and push
                        let current = style_stack.last().cloned().unwrap_or_default();
                        let combined = current.combine(style);
                        style_stack.push(combined);
                    }
                    Tag::Close => {
                        style_stack.pop();
                    }
                    Tag::CloseNamed(_name) => {
                        // For simplicity, just pop the last style
                        // A more sophisticated implementation would track named styles
                        style_stack.pop();
                    }
                },
            }
        }

        text
    }

    /// Returns the plain text content without markup.
    #[must_use]
    pub fn plain_text(&self) -> String {
        let mut result = String::new();
        for segment in &self.segments {
            if let MarkupSegment::Text(s) = segment {
                result.push_str(s);
            }
        }
        result
    }

    /// Returns true if the markup is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.segments.is_empty()
            || self
                .segments
                .iter()
                .all(|s| matches!(s, MarkupSegment::Text(t) if t.is_empty()))
    }
}

impl fmt::Display for Markup {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.plain_text())
    }
}

impl From<&str> for Markup {
    fn from(s: &str) -> Self {
        Self::parse(s).unwrap_or_default()
    }
}

impl From<String> for Markup {
    fn from(s: String) -> Self {
        Self::parse(&s).unwrap_or_default()
    }
}

/// Unescapes bracket characters in text.
fn unescape_brackets(s: &str) -> String {
    s.replace("\\[", "[").replace("\\]", "]")
}

/// Escapes markup characters in plain text.
#[must_use]
pub fn escape(s: &str) -> String {
    s.replace('[', "\\[").replace(']', "\\]")
}

/// Renders markup to ANSI-escaped text.
///
/// # Errors
///
/// Returns an error if the markup is invalid.
pub fn render(s: &str) -> Result<String> {
    let markup = Markup::parse(s)?;
    let text = markup.to_text();
    let segments = text.to_segments();
    Ok(segments.to_ansi())
}

/// Renders markup and strips to plain text.
///
/// # Errors
///
/// Returns an error if the markup is invalid.
pub fn render_plain(s: &str) -> Result<String> {
    let markup = Markup::parse(s)?;
    Ok(markup.plain_text())
}

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

    #[test]
    fn test_parse_plain_text() {
        let markup = Markup::parse("hello world").ok().unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello world");
    }

    #[test]
    fn test_parse_simple_tag() {
        let markup = Markup::parse("[bold]hello[/]").ok().unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello");
        assert_eq!(markup.segments.len(), 3);
    }

    #[test]
    fn test_parse_nested_tags() {
        let markup = Markup::parse("[bold][red]hello[/][/]")
            .ok()
            .unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello");
    }

    #[test]
    fn test_parse_color() {
        let markup = Markup::parse("[red]hello[/]").ok().unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello");
    }

    #[test]
    fn test_parse_combined_style() {
        let markup = Markup::parse("[bold red on white]hello[/]")
            .ok()
            .unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello");
    }

    #[test]
    fn test_parse_escaped_bracket() {
        let markup = Markup::parse("hello \\[world\\]").ok().unwrap_or_default();
        assert_eq!(markup.plain_text(), "hello [world]");
    }

    #[test]
    fn test_to_text_plain() {
        let markup = Markup::parse("hello").ok().unwrap_or_default();
        let text = markup.to_text();
        assert_eq!(text.plain(), "hello");
        assert!(text.to_segments().iter().all(|s| s.style.is_none()));
    }

    #[test]
    fn test_to_text_styled() {
        let markup = Markup::parse("[bold]hello[/]").ok().unwrap_or_default();
        let text = markup.to_text();
        assert_eq!(text.plain(), "hello");

        let segments = text.to_segments();
        let styled_segment = segments.iter().find(|s| s.text == "hello");
        assert!(styled_segment.is_some());
        if let Some(seg) = styled_segment {
            assert!(seg.style.is_some());
            assert_eq!(
                seg.style.as_ref().and_then(|s| s.attributes.bold),
                Some(true)
            );
        }
    }

    #[test]
    fn test_to_text_nested() {
        let markup = Markup::parse("[bold][italic]hello[/][/]")
            .ok()
            .unwrap_or_default();
        let text = markup.to_text();
        assert_eq!(text.plain(), "hello");

        let segments = text.to_segments();
        let styled_segment = segments.iter().find(|s| s.text == "hello");
        if let Some(seg) = styled_segment {
            let style = seg.style.as_ref();
            assert_eq!(style.and_then(|s| s.attributes.bold), Some(true));
            assert_eq!(style.and_then(|s| s.attributes.italic), Some(true));
        }
    }

    #[test]
    fn test_render() {
        let result = render("[bold]hello[/]").ok().unwrap_or_default();
        assert!(result.contains("hello"));
        assert!(result.contains("\x1b[")); // Contains ANSI codes
    }

    #[test]
    fn test_render_plain() {
        let result = render_plain("[bold red]hello[/] [italic]world[/]")
            .ok()
            .unwrap_or_default();
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_escape() {
        assert_eq!(escape("hello [world]"), "hello \\[world\\]");
        assert_eq!(escape("no brackets"), "no brackets");
    }

    #[test]
    fn test_tag_parse_open() {
        let tag = Tag::parse("bold", false).ok();
        assert!(matches!(tag, Some(Tag::Open(_))));
    }

    #[test]
    fn test_tag_parse_close() {
        let tag = Tag::parse("", true).ok();
        assert!(matches!(tag, Some(Tag::Close)));
    }

    #[test]
    fn test_tag_parse_close_named() {
        let tag = Tag::parse("bold", true).ok();
        assert!(matches!(tag, Some(Tag::CloseNamed(name)) if name == "bold"));
    }

    #[test]
    fn test_markup_from_str() {
        let markup: Markup = "[bold]hello[/]".into();
        assert_eq!(markup.plain_text(), "hello");
    }

    #[test]
    fn test_markup_from_string() {
        let markup: Markup = "[italic]world[/]".to_owned().into();
        assert_eq!(markup.plain_text(), "world");
    }

    #[test]
    fn test_markup_display() {
        let markup = Markup::parse("[bold]hello[/]").ok().unwrap_or_default();
        assert_eq!(format!("{markup}"), "hello");
    }

    #[test]
    fn test_markup_is_empty() {
        let markup = Markup::new();
        assert!(markup.is_empty());

        let markup = Markup::parse("").ok().unwrap_or_default();
        assert!(markup.is_empty());

        let markup = Markup::parse("hello").ok().unwrap_or_default();
        assert!(!markup.is_empty());
    }

    #[test]
    fn test_complex_markup() {
        let input = "[bold]Hello[/], [red]world[/]! This is [italic blue on white]styled[/] text.";
        let markup = Markup::parse(input).ok().unwrap_or_default();
        assert_eq!(markup.plain_text(), "Hello, world! This is styled text.");
    }

    #[test]
    fn test_hex_color_in_markup() {
        let markup = Markup::parse("[#ff0000]red text[/]")
            .ok()
            .unwrap_or_default();
        assert_eq!(markup.plain_text(), "red text");
    }

    #[test]
    fn test_rgb_color_in_markup() {
        let markup = Markup::parse("[rgb(255,0,0)]red text[/]")
            .ok()
            .unwrap_or_default();
        assert_eq!(markup.plain_text(), "red text");
    }
}