Lines
100 %
Functions
59.09 %
Branches
//! The read-only text window body: lines scrolled by row under the frame scrollbar.
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use crate::desktop::MIN_SIZE;
use crate::text_view::{column, draw_row};
use crate::{Role, RowWindow, Theme, inner, render_scrollbar};
/// Border cells around the content on each axis.
const FRAME: usize = 2;
/// The window size that shows all of `lines` whole inside the frame, at least the minimum
/// window size.
#[must_use]
pub fn viewer_size<S: AsRef<str>>(lines: &[S]) -> (u16, u16) {
let widest = lines
.iter()
.map(|line| column(line.as_ref(), usize::MAX))
.max()
.unwrap_or(0);
let cells = |content: usize, least: u16| {
u16::try_from(content.saturating_add(FRAME))
.unwrap_or(u16::MAX)
.max(least)
};
(cells(widest, MIN_SIZE.0), cells(lines.len(), MIN_SIZE.1))
}
/// Draws `lines` and the scrollbar inside the frame of a window drawn in `area`, from the row
/// `scroll` holds, which the draw keeps inside the last page.
pub fn render_viewer<S: AsRef<str>>(
theme: &Theme,
lines: &[S],
area: Rect,
scroll: &RowWindow,
buf: &mut Buffer,
) {
let body = inner(area).intersection(buf.area);
let view = usize::from(body.height);
let top = scroll.fit(lines.len(), view);
let style = theme.style(Role::Panel);
let shown = lines.iter().skip(top);
for (y, line) in (body.y..body.bottom()).zip(shown) {
draw_row(buf, body, y, line.as_ref(), 0, style);
render_scrollbar(theme, area, buf, (lines.len(), view, top));
#[cfg(test)]
mod tests;