rich_ext/layout/
overflow.rs1use rich::{Justify, Overflow, Segment, Style, Text, Theme};
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub enum OverflowPolicy {
5 Wrap,
6 #[default]
7 Fold,
8 Crop,
9 Ellipsis,
10 Visible,
11}
12pub fn fit_segments(
13 segments: &[Segment],
14 width: usize,
15 policy: OverflowPolicy,
16) -> Vec<Vec<Segment>> {
17 if width == 0 {
18 return Vec::new();
19 }
20 let mut text = Text::new("");
21 for segment in segments.iter().filter(|s| !s.control) {
22 text.append(&segment.text, segment.style.clone().map(Into::into));
23 }
24 let (overflow, no_wrap) = match policy {
25 OverflowPolicy::Wrap => (Overflow::Crop, false),
26 OverflowPolicy::Fold => (Overflow::Fold, false),
27 OverflowPolicy::Crop => (Overflow::Crop, true),
28 OverflowPolicy::Ellipsis => (Overflow::Ellipsis, true),
29 OverflowPolicy::Visible => (Overflow::Ignore, true),
30 };
31 text.render_lines_wrapped(
32 &Theme::default_theme(),
33 &Style::new(),
34 Some(width),
35 Justify::Default,
36 overflow,
37 no_wrap,
38 )
39}