rich-rs 1.2.2

Rich text and beautiful formatting for the terminal
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
//! Rule: a horizontal line renderable.
//!
//! A rule is a horizontal line that can optionally have a title.
//! The title can be aligned left, center, or right.
//!
//! # Example
//!
//! ```
//! use rich_rs::rule::{Rule, AlignMethod};
//!
//! // Simple rule without title
//! let rule = Rule::new();
//!
//! // Rule with centered title
//! let rule = Rule::new().with_title("Section Header");
//!
//! // Rule with left-aligned title
//! let rule = Rule::new()
//!     .with_title("Left Title")
//!     .with_align(AlignMethod::Left);
//! ```

use crate::Renderable;
use crate::cells::{cell_len, set_cell_size};
use crate::console::{Console, ConsoleOptions, OverflowMethod};
use crate::measure::Measurement;
use crate::segment::Segments;
use crate::style::Style;
use crate::text::Text;

// ============================================================================
// AlignMethod
// ============================================================================

/// Text alignment method for Rule titles.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AlignMethod {
    /// Left-aligned title.
    Left,
    /// Center-aligned title (default).
    #[default]
    Center,
    /// Right-aligned title.
    Right,
}

impl AlignMethod {
    /// Parse an alignment method from a string.
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "left" => Some(AlignMethod::Left),
            "center" => Some(AlignMethod::Center),
            "right" => Some(AlignMethod::Right),
            _ => None,
        }
    }
}

// ============================================================================
// Rule
// ============================================================================

/// A horizontal rule (line) that can optionally have a title.
///
/// # Example
///
/// ```
/// use rich_rs::rule::{Rule, AlignMethod};
/// use rich_rs::Style;
///
/// // Simple horizontal line
/// let rule = Rule::new();
///
/// // Rule with a centered title
/// let titled_rule = Rule::new().with_title("My Section");
///
/// // Customized rule
/// let custom_rule = Rule::new()
///     .with_title("Header")
///     .with_characters("=")
///     .with_align(AlignMethod::Left)
///     .with_style(Style::new().with_bold(true));
/// ```
#[derive(Debug, Clone)]
pub struct Rule {
    /// Optional title text.
    title: Option<Text>,
    /// Characters used to draw the line (default: "─").
    characters: String,
    /// Style for the rule line.
    style: Style,
    /// String to append at the end (default: "\n").
    end: String,
    /// Title alignment (default: Center).
    align: AlignMethod,
}

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

impl Rule {
    /// Create a new rule with default settings.
    ///
    /// Default settings:
    /// - No title
    /// - "─" (box drawing horizontal) as the line character
    /// - Default style (from theme "rule.line")
    /// - Newline at the end
    /// - Center alignment
    pub fn new() -> Self {
        Rule {
            title: None,
            characters: "".to_string(),
            style: Style::new(),
            end: "\n".to_string(),
            align: AlignMethod::Center,
        }
    }

    /// Set the title from a string.
    ///
    /// The title supports BBCode-style markup (e.g., `[bold]Title[/bold]`).
    /// If markup parsing fails, the title is used as plain text.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        let title_str = title.into();
        // Try to parse as markup; fall back to plain text on error.
        self.title =
            Some(Text::from_markup(&title_str, false).unwrap_or_else(|_| Text::plain(&title_str)));
        self
    }

    /// Set the title from a Text object.
    ///
    /// This allows for styled titles.
    pub fn with_title_text(mut self, title: Text) -> Self {
        self.title = Some(title);
        self
    }

    /// Set the characters used to draw the line.
    ///
    /// # Panics
    ///
    /// Panics if the characters have a cell width less than 1.
    pub fn with_characters(mut self, characters: impl Into<String>) -> Self {
        let chars = characters.into();
        assert!(
            cell_len(&chars) >= 1,
            "'characters' argument must have a cell width of at least 1"
        );
        self.characters = chars;
        self
    }

    /// Set the style for the rule line.
    pub fn with_style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set the end string (default is "\n").
    pub fn with_end(mut self, end: impl Into<String>) -> Self {
        self.end = end.into();
        self
    }

    /// Set the title alignment.
    pub fn with_align(mut self, align: AlignMethod) -> Self {
        self.align = align;
        self
    }

    /// Generate a line of characters without a title.
    fn rule_line(&self, characters: &str, chars_len: usize, width: usize) -> Text {
        // Create enough characters to fill the width, then truncate
        let repeat_count = (width / chars_len) + 1;
        let line_chars = characters.repeat(repeat_count);
        let rule_text = Text::styled(line_chars, self.style);

        // Truncate to exact width (using character count, not cell width)
        let chars: Vec<char> = rule_text.plain_text().chars().collect();
        let mut current_width = 0;
        let mut char_count = 0;
        for c in &chars {
            let cw = crate::cells::char_width(*c);
            if current_width + cw > width {
                break;
            }
            current_width += cw;
            char_count += 1;
        }

        // Rebuild the text with the truncated content
        let truncated: String = chars[..char_count].iter().collect();
        let mut result = Text::styled(truncated, self.style);

        // Pad to exact width if needed
        if current_width < width {
            let padding = " ".repeat(width - current_width);
            result.append(&padding, Some(self.style));
        }

        result
    }
}

impl Renderable for Rule {
    fn render(&self, _console: &Console, options: &ConsoleOptions) -> Segments {
        let width = options.max_width;

        // Handle ASCII-only mode
        let characters = if options.ascii_only() && !self.characters.is_ascii() {
            "-".to_string()
        } else {
            self.characters.clone()
        };

        let chars_len = cell_len(&characters);

        // If no title, just render the line
        if self.title.is_none() {
            let mut rule_text = self.rule_line(&characters, chars_len, width);
            // Apply the end string
            if !self.end.is_empty() {
                rule_text.append(&self.end, None);
            }
            return rule_text.render(_console, options);
        }

        // We have a title - need to build the rule with title
        let title = self.title.as_ref().unwrap();

        // Prepare title text: replace newlines with spaces and expand tabs
        // We preserve the base style from the original title
        let plain = title.plain_text().replace('\n', " ");
        let mut title_text = if let Some(style) = title.base_style() {
            Text::styled(&plain, style)
        } else {
            Text::plain(&plain)
        };

        // Copy over spans from original title
        // Since we just replaced newlines with spaces, character offsets are preserved
        for span in title.spans() {
            title_text.stylize(span.start, span.end, span.style);
        }

        // Expand tabs
        title_text = title_text.expand_tabs(8);

        // Calculate required space for title
        // Center alignment needs 2 chars on each side, left/right needs 2 chars on one side
        let required_space = if self.align == AlignMethod::Center {
            4
        } else {
            2
        };

        let truncate_width = width.saturating_sub(required_space);
        if truncate_width == 0 {
            // No room for title, just render the line
            let mut rule_text = self.rule_line(&characters, chars_len, width);
            if !self.end.is_empty() {
                rule_text.append(&self.end, None);
            }
            return rule_text.render(_console, options);
        }

        // Truncate title if needed
        let title_text = title_text.truncate(truncate_width, OverflowMethod::Ellipsis, false);

        // Build the rule text based on alignment
        let mut rule_text = Text::new();

        match self.align {
            AlignMethod::Center => {
                // ───── Title ─────
                let title_cell_len = cell_len(title_text.plain_text());
                let side_width = (width.saturating_sub(title_cell_len)) / 2;

                // Left side: characters filling side_width - 1 (leave space for " ")
                let left_chars = characters.repeat((side_width / chars_len) + 1);
                let left_truncated = set_cell_size(&left_chars, side_width.saturating_sub(1));
                rule_text.append(&left_truncated, Some(self.style));
                rule_text.append(" ", Some(self.style));

                // Title
                rule_text.append_text(&title_text);

                // Right side: fill remaining space
                let right_length =
                    width.saturating_sub(cell_len(&left_truncated) + 1 + title_cell_len);
                rule_text.append(" ", Some(self.style));
                let right_chars = characters.repeat((right_length / chars_len) + 1);
                let right_truncated =
                    set_cell_size(&right_chars, right_length.saturating_sub(1).max(0));
                rule_text.append(&right_truncated, Some(self.style));
            }
            AlignMethod::Left => {
                // Title ─────────────
                rule_text.append_text(&title_text);
                rule_text.append(" ", Some(self.style)); // Separator uses rule style

                let remaining = width.saturating_sub(rule_text.cell_len());
                let fill_chars = characters.repeat((remaining / chars_len) + 1);
                let fill_truncated = set_cell_size(&fill_chars, remaining);
                rule_text.append(&fill_truncated, Some(self.style));
            }
            AlignMethod::Right => {
                // ───────────── Title
                let title_cell_len = cell_len(title_text.plain_text());
                let fill_length = width.saturating_sub(title_cell_len + 1);
                let fill_chars = characters.repeat((fill_length / chars_len) + 1);
                let fill_truncated = set_cell_size(&fill_chars, fill_length);
                rule_text.append(&fill_truncated, Some(self.style));
                rule_text.append(" ", Some(self.style)); // Separator uses rule style
                rule_text.append_text(&title_text);
            }
        }

        // Ensure exact width
        let final_plain = set_cell_size(rule_text.plain_text(), width);
        let mut final_text = Text::plain(&final_plain);

        // Re-apply styles from rule_text
        for span in rule_text.spans() {
            // Clamp span to new text length
            let new_len = final_text.len();
            if span.start < new_len {
                final_text.stylize(span.start, span.end.min(new_len), span.style);
            }
        }

        // Apply base style if present
        if let Some(base) = rule_text.base_style() {
            final_text.set_base_style(Some(base));
        }

        // Append end string
        if !self.end.is_empty() {
            final_text.append(&self.end, None);
        }

        final_text.render(_console, options)
    }

    fn measure(&self, _console: &Console, _options: &ConsoleOptions) -> Measurement {
        // Rule always uses exactly 1 cell minimum and maximum width
        // (it expands to fill available space)
        Measurement::new(1, 1)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ==================== AlignMethod tests ====================

    #[test]
    fn test_align_method_parse() {
        assert_eq!(AlignMethod::parse("left"), Some(AlignMethod::Left));
        assert_eq!(AlignMethod::parse("LEFT"), Some(AlignMethod::Left));
        assert_eq!(AlignMethod::parse("center"), Some(AlignMethod::Center));
        assert_eq!(AlignMethod::parse("CENTER"), Some(AlignMethod::Center));
        assert_eq!(AlignMethod::parse("right"), Some(AlignMethod::Right));
        assert_eq!(AlignMethod::parse("RIGHT"), Some(AlignMethod::Right));
        assert_eq!(AlignMethod::parse("invalid"), None);
    }

    #[test]
    fn test_align_method_default() {
        assert_eq!(AlignMethod::default(), AlignMethod::Center);
    }

    // ==================== Rule construction tests ====================

    #[test]
    fn test_rule_new() {
        let rule = Rule::new();
        assert!(rule.title.is_none());
        assert_eq!(rule.characters, "");
        assert_eq!(rule.end, "\n");
        assert_eq!(rule.align, AlignMethod::Center);
    }

    #[test]
    fn test_rule_with_title() {
        let rule = Rule::new().with_title("Test");
        assert!(rule.title.is_some());
        assert_eq!(rule.title.as_ref().unwrap().plain_text(), "Test");
    }

    #[test]
    fn test_rule_with_title_text() {
        let text = Text::styled("Styled Title", Style::new().with_bold(true));
        let rule = Rule::new().with_title_text(text);
        assert!(rule.title.is_some());
        assert_eq!(rule.title.as_ref().unwrap().plain_text(), "Styled Title");
    }

    #[test]
    fn test_rule_with_characters() {
        let rule = Rule::new().with_characters("=");
        assert_eq!(rule.characters, "=");
    }

    #[test]
    #[should_panic(expected = "'characters' argument must have a cell width of at least 1")]
    fn test_rule_with_empty_characters() {
        Rule::new().with_characters("");
    }

    #[test]
    fn test_rule_with_style() {
        let style = Style::new().with_bold(true);
        let rule = Rule::new().with_style(style);
        assert_eq!(rule.style.bold, Some(true));
    }

    #[test]
    fn test_rule_with_end() {
        let rule = Rule::new().with_end("");
        assert_eq!(rule.end, "");
    }

    #[test]
    fn test_rule_with_align() {
        let rule = Rule::new().with_align(AlignMethod::Left);
        assert_eq!(rule.align, AlignMethod::Left);
    }

    // ==================== Rule render tests ====================

    #[test]
    fn test_rule_render_no_title() {
        let rule = Rule::new().with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should be exactly 20 cells of "─"
        assert_eq!(cell_len(&text), 20);
        assert!(text.contains(""));
    }

    #[test]
    fn test_rule_render_with_title_center() {
        let rule = Rule::new().with_title("Test").with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should contain the title
        assert!(text.contains("Test"));
        // Should be exactly 20 cells
        assert_eq!(cell_len(&text), 20);
    }

    #[test]
    fn test_rule_render_with_title_left() {
        let rule = Rule::new()
            .with_title("Left")
            .with_align(AlignMethod::Left)
            .with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Title should be at the left
        assert!(text.starts_with("Left"));
        assert_eq!(cell_len(&text), 20);
    }

    #[test]
    fn test_rule_render_with_title_right() {
        let rule = Rule::new()
            .with_title("Right")
            .with_align(AlignMethod::Right)
            .with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Title should be at the right
        assert!(text.trim_end().ends_with("Right"));
        assert_eq!(cell_len(&text), 20);
    }

    #[test]
    fn test_rule_ascii_only() {
        let rule = Rule::new().with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 10,
            encoding: "ascii".to_string(),
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should use "-" instead of "─" in ASCII mode
        assert!(text.chars().all(|c| c == '-' || c == ' '));
        assert!(!text.contains(""));
    }

    #[test]
    fn test_rule_long_title_truncation() {
        let rule = Rule::new()
            .with_title("This is a very long title that needs truncation")
            .with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should truncate with ellipsis
        assert!(text.contains("") || cell_len(&text) == 20);
        assert_eq!(cell_len(&text), 20);
    }

    #[test]
    fn test_rule_multi_char_pattern() {
        let rule = Rule::new().with_characters("+-").with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 10,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should contain the pattern
        assert!(text.contains('+') || text.contains('-'));
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_rule_measure() {
        let rule = Rule::new().with_title("Test");
        let console = Console::new();
        let options = ConsoleOptions::default();

        let measurement = rule.measure(&console, &options);
        assert_eq!(measurement.minimum, 1);
        assert_eq!(measurement.maximum, 1);
    }

    #[test]
    fn test_rule_with_end_newline() {
        let rule = Rule::new();
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 10,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should end with newline
        assert!(text.ends_with('\n'));
    }

    // ==================== Markup tests ====================

    #[test]
    fn test_rule_with_markup_title() {
        let rule = Rule::new()
            .with_title("[bold]Bold Title[/bold]")
            .with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 30,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // The markup should be parsed, so "Bold Title" appears (without the tags)
        assert!(text.contains("Bold Title"));
        assert!(!text.contains("[bold]"));

        // Should have correct width
        assert_eq!(cell_len(&text), 30);
    }

    #[test]
    fn test_rule_with_plain_title_fallback() {
        // If markup is invalid, it should fall back to plain text
        let rule = Rule::new().with_title("Plain Title").with_end("");
        assert_eq!(rule.title.as_ref().unwrap().plain_text(), "Plain Title");
    }

    // ==================== Edge case tests ====================

    #[test]
    fn test_rule_very_narrow_width() {
        let rule = Rule::new().with_title("Test").with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 4,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        // With only 4 cells, no room for title (requires 4 for padding)
        // Should render just the line
        assert_eq!(cell_len(&text), 4);
    }

    #[test]
    fn test_rule_unicode_characters() {
        let rule = Rule::new().with_characters("").with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 10,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(text.contains(""));
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_rule_cjk_title() {
        let rule = Rule::new().with_title("你好").with_end("");
        let console = Console::new();
        let options = ConsoleOptions {
            max_width: 20,
            ..Default::default()
        };

        let segments = rule.render(&console, &options);
        let text: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(text.contains("你好"));
        assert_eq!(cell_len(&text), 20);
    }
}