rich_rust 0.2.1

A Rust port of Python's Rich 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
//! Align - Horizontal alignment wrapper for renderables.
//!
//! This module provides alignment functionality to position content
//! within a given width (left, center, or right aligned).
//!
//! # Example
//!
//! ```rust,ignore
//! use rich_rust::renderables::align::{Align, AlignMethod};
//! use rich_rust::segment::Segment;
//!
//! let content = vec![Segment::new("Hello", None)];
//! let aligned = Align::new(content, 20)
//!     .method(AlignMethod::Center)
//!     .render();
//! ```

use crate::cells::cell_len;
use crate::console::{Console, ConsoleOptions};
use crate::renderables::Renderable;
use crate::segment::Segment;
use crate::style::Style;

/// Horizontal alignment method.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AlignMethod {
    /// Align content to the left (default).
    #[default]
    Left,
    /// Center content horizontally.
    Center,
    /// Align content to the right.
    Right,
}

/// Vertical alignment method for multi-line content.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VerticalAlignMethod {
    /// Align content to the top (default).
    #[default]
    Top,
    /// Center content vertically.
    Middle,
    /// Align content to the bottom.
    Bottom,
}

/// A wrapper that aligns content within a given width.
#[derive(Debug, Clone)]
pub struct Align<'a> {
    /// Content segments to align.
    content: Vec<Segment<'a>>,
    /// Target width.
    width: usize,
    /// Horizontal alignment method.
    method: AlignMethod,
    /// Style for padding spaces.
    pad_style: Style,
}

impl<'a> Align<'a> {
    /// Create a new Align wrapper for single-line content.
    #[must_use]
    pub fn new(content: impl IntoIterator<Item = Segment<'a>>, width: usize) -> Self {
        Self {
            content: content.into_iter().collect(),
            width,
            method: AlignMethod::Left,
            pad_style: Style::new(),
        }
    }

    /// Create an Align wrapper from a string.
    #[must_use]
    pub fn from_str(text: &'a str, width: usize) -> Self {
        Self::new(vec![Segment::new(text, None)], width)
    }

    /// Set the alignment method.
    #[must_use]
    pub fn method(mut self, method: AlignMethod) -> Self {
        self.method = method;
        self
    }

    /// Align content to the left.
    #[must_use]
    pub fn left(self) -> Self {
        self.method(AlignMethod::Left)
    }

    /// Center content horizontally.
    #[must_use]
    pub fn center(self) -> Self {
        self.method(AlignMethod::Center)
    }

    /// Align content to the right.
    #[must_use]
    pub fn right(self) -> Self {
        self.method(AlignMethod::Right)
    }

    /// Set the style for padding spaces.
    #[must_use]
    pub fn pad_style(mut self, style: Style) -> Self {
        self.pad_style = style;
        self
    }

    /// Get the content width in cells.
    #[must_use]
    pub fn content_width(&self) -> usize {
        self.content.iter().map(|s| cell_len(&s.text)).sum()
    }

    /// Render the aligned content.
    #[must_use]
    pub fn render(self) -> Vec<Segment<'a>> {
        let content_width = self.content_width();

        // If content is wider than or equal to target width, return as-is
        if content_width >= self.width {
            return self.content;
        }

        let padding_total = self.width - content_width;
        let mut result = Vec::with_capacity(self.content.len() + 2);

        match self.method {
            AlignMethod::Left => {
                // Content first, then right padding
                result.extend(self.content);
                if padding_total > 0 {
                    result.push(Segment::new(
                        " ".repeat(padding_total),
                        Some(self.pad_style),
                    ));
                }
            }
            AlignMethod::Center => {
                // Left padding, content, right padding
                let left_pad = padding_total / 2;
                let right_pad = padding_total - left_pad;

                if left_pad > 0 {
                    result.push(Segment::new(
                        " ".repeat(left_pad),
                        Some(self.pad_style.clone()),
                    ));
                }
                result.extend(self.content);
                if right_pad > 0 {
                    result.push(Segment::new(" ".repeat(right_pad), Some(self.pad_style)));
                }
            }
            AlignMethod::Right => {
                // Left padding first, then content
                if padding_total > 0 {
                    result.push(Segment::new(
                        " ".repeat(padding_total),
                        Some(self.pad_style),
                    ));
                }
                result.extend(self.content);
            }
        }

        result
    }
}

impl Renderable for Align<'_> {
    fn render<'b>(&'b self, _console: &Console, _options: &ConsoleOptions) -> Vec<Segment<'b>> {
        // Since Align consumes self in render(), we need to clone it if we want to implement Renderable for &Align
        // But Renderable takes &self.
        // Align::render(self).
        // Align is cheap to clone (Vec<Segment> might be expensive if deep).
        // Ideally Align::render should take &self.
        self.clone().render().into_iter().collect()
    }
}

/// A wrapper that aligns multiple lines of content.
#[derive(Debug, Clone)]
pub struct AlignLines<'a> {
    /// Lines of content (each line is a Vec of Segments).
    lines: Vec<Vec<Segment<'a>>>,
    /// Target width.
    width: usize,
    /// Horizontal alignment method.
    method: AlignMethod,
    /// Style for padding spaces.
    pad_style: Style,
}

impl<'a> AlignLines<'a> {
    /// Create a new `AlignLines` wrapper.
    #[must_use]
    pub fn new(lines: Vec<Vec<Segment<'a>>>, width: usize) -> Self {
        Self {
            lines,
            width,
            method: AlignMethod::Left,
            pad_style: Style::new(),
        }
    }

    /// Set the alignment method.
    #[must_use]
    pub fn method(mut self, method: AlignMethod) -> Self {
        self.method = method;
        self
    }

    /// Align content to the left.
    #[must_use]
    pub fn left(self) -> Self {
        self.method(AlignMethod::Left)
    }

    /// Center content horizontally.
    #[must_use]
    pub fn center(self) -> Self {
        self.method(AlignMethod::Center)
    }

    /// Align content to the right.
    #[must_use]
    pub fn right(self) -> Self {
        self.method(AlignMethod::Right)
    }

    /// Set the style for padding spaces.
    #[must_use]
    pub fn pad_style(mut self, style: Style) -> Self {
        self.pad_style = style;
        self
    }

    /// Get the width of a line in cells.
    #[must_use]
    pub fn line_width(line: &[Segment]) -> usize {
        line.iter().map(|s| cell_len(&s.text)).sum()
    }

    /// Render all lines with alignment applied.
    #[must_use]
    pub fn render(self) -> Vec<Vec<Segment<'a>>> {
        self.lines
            .into_iter()
            .map(|line| {
                Align::new(line, self.width)
                    .method(self.method)
                    .pad_style(self.pad_style.clone())
                    .render()
            })
            .collect()
    }
}

impl Renderable for AlignLines<'_> {
    fn render<'b>(&'b self, _console: &Console, _options: &ConsoleOptions) -> Vec<Segment<'b>> {
        let lines = self.clone().render();
        let mut result = Vec::new();
        for (i, line) in lines.into_iter().enumerate() {
            if i > 0 {
                result.push(Segment::line());
            }
            result.extend(line);
        }
        result.into_iter().collect()
    }
}

/// Convenience function to align a single line of text.
#[must_use]
pub fn align_text(text: &str, width: usize, method: AlignMethod) -> String {
    let content_width = cell_len(text);

    if content_width >= width {
        return text.to_string();
    }

    let padding_total = width - content_width;

    match method {
        AlignMethod::Left => {
            format!("{text}{}", " ".repeat(padding_total))
        }
        AlignMethod::Center => {
            let left_pad = padding_total / 2;
            let right_pad = padding_total - left_pad;
            format!("{}{text}{}", " ".repeat(left_pad), " ".repeat(right_pad))
        }
        AlignMethod::Right => {
            format!("{}{text}", " ".repeat(padding_total))
        }
    }
}

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

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

    #[test]
    fn test_align_left() {
        let content = vec![Segment::new("Hi", None)];
        let aligned = Align::new(content, 10).left().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "Hi        ");
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_align_center() {
        let content = vec![Segment::new("Hi", None)];
        let aligned = Align::new(content, 10).center().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "    Hi    ");
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_align_right() {
        let content = vec![Segment::new("Hi", None)];
        let aligned = Align::new(content, 10).right().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "        Hi");
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_align_content_too_wide() {
        let content = vec![Segment::new("Hello World", None)];
        let aligned = Align::new(content, 5).center().render();

        // Should return content as-is when wider than target
        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "Hello World");
    }

    #[test]
    fn test_align_exact_width() {
        let content = vec![Segment::new("Hello", None)];
        let aligned = Align::new(content, 5).center().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "Hello");
    }

    #[test]
    fn test_align_multiple_segments() {
        let content = vec![
            Segment::new("Hello", None),
            Segment::new(" ", None),
            Segment::new("World", None),
        ];
        let aligned = Align::new(content, 20).center().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(cell_len(&text), 20);
        assert!(text.contains("Hello World"));
    }

    #[test]
    fn test_align_center_odd_padding() {
        // 3 char content in 10 width = 7 padding
        // Left: 3, Right: 4
        let content = vec![Segment::new("abc", None)];
        let aligned = Align::new(content, 10).center().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "   abc    ");
        assert_eq!(cell_len(&text), 10);
    }

    #[test]
    fn test_align_from_str() {
        let aligned = Align::from_str("Test", 10).right().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(text, "      Test");
    }

    #[test]
    fn test_align_lines() {
        let lines = vec![
            vec![Segment::new("Short", None)],
            vec![Segment::new("Longer line", None)],
            vec![Segment::new("Hi", None)],
        ];
        let aligned = AlignLines::new(lines, 15).center().render();

        assert_eq!(aligned.len(), 3);

        // Each line should be 15 chars wide
        for line in &aligned {
            let text: String = line.iter().map(|s| s.text.as_ref()).collect();
            assert_eq!(cell_len(&text), 15);
        }
    }

    #[test]
    fn test_align_text_function() {
        assert_eq!(align_text("Hi", 10, AlignMethod::Left), "Hi        ");
        assert_eq!(align_text("Hi", 10, AlignMethod::Center), "    Hi    ");
        assert_eq!(align_text("Hi", 10, AlignMethod::Right), "        Hi");
    }

    #[test]
    fn test_align_with_cjk() {
        // CJK characters are 2 cells wide
        let content = vec![Segment::new("日本", None)]; // 4 cells wide
        let aligned = Align::new(content, 10).center().render();

        let text: String = aligned.iter().map(|s| s.text.as_ref()).collect();
        assert_eq!(cell_len(&text), 10);
        // 10 - 4 = 6 padding, left: 3, right: 3
        assert!(text.starts_with("   "));
        assert!(text.ends_with("   "));
    }

    #[test]
    fn test_content_width() {
        let content = vec![
            Segment::new("Hello", None),
            Segment::new(" ", None),
            Segment::new("World", None),
        ];
        let align = Align::new(content, 20);
        assert_eq!(align.content_width(), 11);
    }

    #[test]
    fn test_vertical_align_default() {
        assert_eq!(VerticalAlignMethod::default(), VerticalAlignMethod::Top);
    }
}