1
//! The read-only text window body: lines scrolled by row under the frame scrollbar.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5

            
6
use crate::desktop::MIN_SIZE;
7
use crate::text_view::{column, draw_row};
8
use crate::{Role, RowWindow, Theme, inner, render_scrollbar};
9

            
10
/// Border cells around the content on each axis.
11
const FRAME: usize = 2;
12

            
13
/// The window size that shows all of `lines` whole inside the frame, at least the minimum
14
/// window size.
15
#[must_use]
16
103
pub fn viewer_size<S: AsRef<str>>(lines: &[S]) -> (u16, u16) {
17
103
    let widest = lines
18
103
        .iter()
19
72184
        .map(|line| column(line.as_ref(), usize::MAX))
20
        .max()
21
103
        .unwrap_or(0);
22
206
    let cells = |content: usize, least: u16| {
23
206
        u16::try_from(content.saturating_add(FRAME))
24
206
            .unwrap_or(u16::MAX)
25
206
            .max(least)
26
206
    };
27
103
    (cells(widest, MIN_SIZE.0), cells(lines.len(), MIN_SIZE.1))
28
103
}
29

            
30
/// Draws `lines` and the scrollbar inside the frame of a window drawn in `area`, from the row
31
/// `scroll` holds, which the draw keeps inside the last page.
32
25
pub fn render_viewer<S: AsRef<str>>(
33
25
    theme: &Theme,
34
25
    lines: &[S],
35
25
    area: Rect,
36
25
    scroll: &RowWindow,
37
25
    buf: &mut Buffer,
38
25
) {
39
25
    let body = inner(area).intersection(buf.area);
40
25
    let view = usize::from(body.height);
41
25
    let top = scroll.fit(lines.len(), view);
42
25
    let style = theme.style(Role::Panel);
43
25
    let shown = lines.iter().skip(top);
44
25
    for (y, line) in (body.y..body.bottom()).zip(shown) {
45
283
        draw_row(buf, body, y, line.as_ref(), 0, style);
46
    }
47
25
    render_scrollbar(theme, area, buf, (lines.len(), view, top));
48
25
}
49

            
50
#[cfg(test)]
51
mod tests;