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
//! Group: a renderable that renders multiple children sequentially.
//!
//! This is a Rust port of Python Rich's `rich.console.Group`.
//! In Rich, `Group(*renderables)` yields child renderables in sequence.
//!
//! In `rich-rs` we insert newlines between children when required so that
//! adjacent renderables don't accidentally render on the same terminal line.

use std::sync::Arc;

use crate::segment::{Segment, Segments};
use crate::{Console, ConsoleOptions, Measurement, Renderable, Text, measure_renderables};

/// A renderable that renders multiple children sequentially.
#[derive(Clone, Default)]
pub struct Group {
    renderables: Vec<Arc<dyn Renderable>>,
    fit: bool,
}

impl Group {
    /// Create a new Group from an iterator of renderables.
    pub fn new<I, R>(renderables: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: Renderable + 'static,
    {
        Self::from_arcs(
            renderables
                .into_iter()
                .map(|r| Arc::new(r) as Arc<dyn Renderable>),
        )
    }

    /// Create a Group from an iterator of `Arc<dyn Renderable>`.
    pub fn from_arcs<I>(renderables: I) -> Self
    where
        I: IntoIterator<Item = Arc<dyn Renderable>>,
    {
        Self {
            renderables: renderables.into_iter().collect(),
            fit: true,
        }
    }

    /// Set whether the Group should fit to its contents for measurement.
    ///
    /// When `fit` is false, measurement returns an exact width equal to the available width.
    pub fn with_fit(mut self, fit: bool) -> Self {
        self.fit = fit;
        self
    }

    /// Return the child renderables.
    pub fn renderables(&self) -> &[Arc<dyn Renderable>] {
        &self.renderables
    }
}

fn segments_end_with_newline(segments: &Segments) -> bool {
    // Find the last non-control segment with non-empty text.
    let rev: Vec<_> = segments.iter().collect();
    for seg in rev.into_iter().rev() {
        if seg.control.is_some() {
            continue;
        }
        if seg.text.is_empty() {
            continue;
        }
        let text: &str = seg.text.as_ref();
        return text.ends_with('\n') || text.ends_with("\n\r");
    }
    false
}

impl std::fmt::Debug for Group {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Group")
            .field("len", &self.renderables.len())
            .field("fit", &self.fit)
            .finish()
    }
}

impl Renderable for Group {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> Segments {
        let mut out = Segments::new();
        let mut first = true;

        for child in &self.renderables {
            let segs = child.render(console, options);

            if !first && !segments_end_with_newline(&out) {
                out.push(Segment::line());
            }
            first = false;

            out.extend(segs.into_iter());
        }

        out
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        if self.fit {
            let refs: Vec<&dyn Renderable> = self.renderables.iter().map(|r| r.as_ref()).collect();
            measure_renderables(console, options, &refs)
        } else {
            Measurement::exact(options.max_width)
        }
    }
}

// ============================================================================
// Renderables — like Group but without inserting newlines
// ============================================================================

/// A container that renders multiple children sequentially WITHOUT newlines.
///
/// Unlike `Group`, which inserts newlines between children, `Renderables`
/// yields each child's output directly, matching Python Rich's
/// `rich.containers.Renderables`.
#[derive(Clone, Default)]
pub struct Renderables {
    renderables: Vec<Arc<dyn Renderable>>,
}

impl Renderables {
    /// Create a new Renderables from an iterator of renderables.
    pub fn new<I, R>(renderables: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: Renderable + 'static,
    {
        Self {
            renderables: renderables
                .into_iter()
                .map(|r| Arc::new(r) as Arc<dyn Renderable>)
                .collect(),
        }
    }

    /// Create from an iterator of `Arc<dyn Renderable>`.
    pub fn from_arcs<I>(renderables: I) -> Self
    where
        I: IntoIterator<Item = Arc<dyn Renderable>>,
    {
        Self {
            renderables: renderables.into_iter().collect(),
        }
    }

    /// Create an empty Renderables.
    pub fn empty() -> Self {
        Self {
            renderables: Vec::new(),
        }
    }

    /// Add a renderable.
    pub fn push(&mut self, renderable: impl Renderable + 'static) {
        self.renderables.push(Arc::new(renderable));
    }

    /// Add a pre-wrapped Arc renderable.
    pub fn push_arc(&mut self, renderable: Arc<dyn Renderable>) {
        self.renderables.push(renderable);
    }

    /// Return the child renderables.
    pub fn renderables(&self) -> &[Arc<dyn Renderable>] {
        &self.renderables
    }
}

impl std::fmt::Debug for Renderables {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Renderables")
            .field("len", &self.renderables.len())
            .finish()
    }
}

impl Renderable for Renderables {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> Segments {
        let mut out = Segments::new();
        for child in &self.renderables {
            let segs = child.render(console, options);
            out.extend(segs.into_iter());
        }
        out
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        let refs: Vec<&dyn Renderable> = self.renderables.iter().map(|r| r.as_ref()).collect();
        if refs.is_empty() {
            return Measurement::new(1, 1);
        }
        measure_renderables(console, options, &refs)
    }
}

// ============================================================================
// Lines — a text line container with list-like API
// ============================================================================

/// A container of [`Text`] lines that renders one line per row.
///
/// This type is equivalent to Python Rich's `rich.containers.Lines`.
#[derive(Clone, Default)]
pub struct Lines {
    lines: Vec<Text>,
}

impl Lines {
    /// Create `Lines` from an iterator of [`Text`].
    pub fn new<I>(lines: I) -> Self
    where
        I: IntoIterator<Item = Text>,
    {
        Self {
            lines: lines.into_iter().collect(),
        }
    }

    /// Create an empty `Lines`.
    pub fn empty() -> Self {
        Self { lines: Vec::new() }
    }

    /// Append a line.
    pub fn append(&mut self, line: Text) {
        self.lines.push(line);
    }

    /// Extend with multiple lines.
    pub fn extend<I>(&mut self, lines: I)
    where
        I: IntoIterator<Item = Text>,
    {
        self.lines.extend(lines);
    }

    /// Pop the last line.
    pub fn pop(&mut self) -> Option<Text> {
        self.lines.pop()
    }

    /// Number of lines.
    pub fn len(&self) -> usize {
        self.lines.len()
    }

    /// True if there are no lines.
    pub fn is_empty(&self) -> bool {
        self.lines.is_empty()
    }

    /// Borrow contained lines.
    pub fn lines(&self) -> &[Text] {
        &self.lines
    }
}

impl std::fmt::Debug for Lines {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Lines")
            .field("len", &self.lines.len())
            .finish()
    }
}

impl Renderable for Lines {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> Segments {
        let mut out = Segments::new();
        let mut first = true;

        for line in &self.lines {
            let segs = line.render(console, options);

            if !first && !segments_end_with_newline(&out) {
                out.push(Segment::line());
            }
            first = false;

            out.extend(segs.into_iter());
        }

        out
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        if self.lines.is_empty() {
            return Measurement::new(1, 1);
        }
        let refs: Vec<&dyn Renderable> = self
            .lines
            .iter()
            .map(|line| line as &dyn Renderable)
            .collect();
        measure_renderables(console, options, &refs)
    }
}

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

    #[test]
    fn test_group_renders_children_with_newlines() {
        let group = Group::new([Text::plain("A"), Text::plain("B")]);
        let console = Console::new();
        let options = console.options();
        let rendered: String = group
            .render(&console, options)
            .iter()
            .map(|s| s.text.to_string())
            .collect();
        assert!(rendered.contains('\n'));
    }

    #[test]
    fn test_group_measure_fit() {
        let group = Group::new([Text::plain("Hello"), Text::plain("World!")]).with_fit(true);
        let console = Console::new();
        let options = console.options();
        let m = group.measure(&console, options);
        assert!(m.maximum >= 6);
    }

    #[test]
    fn test_renderables_no_newlines() {
        let r = Renderables::new([Text::plain("A"), Text::plain("B")]);
        let console = Console::new();
        let options = console.options();
        let rendered: String = r
            .render(&console, options)
            .iter()
            .map(|s| s.text.to_string())
            .collect();
        // Renderables should NOT insert newlines between children
        assert!(rendered.contains("AB") || rendered.contains("A") && rendered.contains("B"));
        // Check there's no inserted newline between A and B
        let pos_a = rendered.find('A').unwrap();
        let pos_b = rendered.find('B').unwrap();
        let between = &rendered[pos_a + 1..pos_b];
        assert!(
            !between.contains('\n'),
            "Should not insert newline between children"
        );
    }

    #[test]
    fn test_renderables_measure() {
        let r = Renderables::new([Text::plain("Hello"), Text::plain("World!")]);
        let console = Console::new();
        let options = console.options();
        let m = r.measure(&console, options);
        assert!(m.maximum >= 6); // "World!" = 6 chars
    }

    #[test]
    fn test_renderables_empty() {
        let r = Renderables::empty();
        let console = Console::new();
        let options = console.options();
        let m = r.measure(&console, options);
        assert_eq!(m.minimum, 1);
        assert_eq!(m.maximum, 1);
    }

    #[test]
    fn test_group_measure_fill() {
        let group = Group::new([Text::plain("Hello")]).with_fit(false);
        let console = Console::new();
        let mut options = console.options().clone();
        options.max_width = 42;
        let m = group.measure(&console, &options);
        assert_eq!(m.minimum, 42);
        assert_eq!(m.maximum, 42);
    }

    #[test]
    fn test_lines_render_inserts_newlines_between_lines() {
        let lines = Lines::new([Text::plain("A"), Text::plain("B")]);
        let console = Console::new();
        let options = console.options();
        let rendered: String = lines
            .render(&console, options)
            .iter()
            .map(|s| s.text.to_string())
            .collect();
        assert_eq!(rendered, "A\nB");
    }

    #[test]
    fn test_lines_render_does_not_double_insert_newlines() {
        let lines = Lines::new([Text::plain("A\n"), Text::plain("B")]);
        let console = Console::new();
        let options = console.options();
        let rendered: String = lines
            .render(&console, options)
            .iter()
            .map(|s| s.text.to_string())
            .collect();
        assert_eq!(rendered, "A\nB");
    }

    #[test]
    fn test_lines_list_api() {
        let mut lines = Lines::empty();
        assert!(lines.is_empty());

        lines.append(Text::plain("A"));
        lines.extend([Text::plain("B"), Text::plain("C")]);
        assert_eq!(lines.len(), 3);
        assert_eq!(lines.lines()[0].plain_text(), "A");
        assert_eq!(lines.lines()[2].plain_text(), "C");

        let popped = lines.pop().unwrap();
        assert_eq!(popped.plain_text(), "C");
        assert_eq!(lines.len(), 2);
    }

    #[test]
    fn test_lines_measure_uses_widest_line() {
        let lines = Lines::new([Text::plain("cat"), Text::plain("hippo")]);
        let console = Console::new();
        let options = console.options();
        let m = lines.measure(&console, options);
        assert_eq!(m.minimum, 5);
        assert_eq!(m.maximum, 5);
    }
}