Skip to main content

oxicode_textarea/render/
line_utils.rs

1use ratatui::text::Line;
2use ratatui::text::Span;
3
4/// Clone a borrowed ratatui `Line` into an owned `'static` line.
5pub fn line_to_static(line: &Line<'_>) -> Line<'static> {
6    Line {
7        style: line.style,
8        alignment: line.alignment,
9        spans: line
10            .spans
11            .iter()
12            .map(|s| Span {
13                style: s.style,
14                content: std::borrow::Cow::Owned(s.content.to_string()),
15            })
16            .collect(),
17    }
18}
19
20/// Append owned copies of borrowed lines to `out`.
21pub fn push_owned_lines<'a>(src: &[Line<'a>], out: &mut Vec<Line<'static>>) {
22    for l in src {
23        out.push(line_to_static(l));
24    }
25}
26
27/// Consider a line blank if it has no spans or only spans whose contents are
28/// empty or consist solely of spaces (no tabs/newlines).
29pub fn is_blank_line_spaces_only(line: &Line<'_>) -> bool {
30    if line.spans.is_empty() {
31        return true;
32    }
33    line.spans
34        .iter()
35        .all(|s| s.content.is_empty() || s.content.chars().all(|c| c == ' '))
36}
37
38/// Prefix each line with `initial_prefix` for the first line and
39/// `subsequent_prefix` for following lines. Returns a new Vec of owned lines.
40pub fn prefix_lines(
41    lines: Vec<Line<'static>>,
42    initial_prefix: Span<'static>,
43    subsequent_prefix: Span<'static>,
44) -> Vec<Line<'static>> {
45    lines
46        .into_iter()
47        .enumerate()
48        .map(|(i, l)| {
49            let mut spans = Vec::with_capacity(l.spans.len() + 1);
50            spans.push(if i == 0 {
51                initial_prefix.clone()
52            } else {
53                subsequent_prefix.clone()
54            });
55            spans.extend(l.spans);
56            Line::from(spans).style(l.style)
57        })
58        .collect()
59}