Lines
99.17 %
Functions
100 %
Branches
//! A multi-line text box: fixed-size window onto lines of text, scrolled on both axes.
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use ratatui::style::Style;
use ratatui::widgets::StatefulWidget;
use crate::input_line::{index_at_column, join_to_previous, shown, width_of};
use crate::{Role, RowWindow, Theme};
/// The first line and first display column shown in the box.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Viewport {
pub top: usize,
pub left: usize,
}
/// The lines of a text and the cursor in them as (line, character index). Rendering scrolls
/// the [`Viewport`] state the least that keeps the character under the cursor in view, then
/// draws through it; the terminal cursor itself is placed by the caller at [`TextView::cursor`].
pub struct TextView<'a> {
theme: &'a Theme,
lines: &'a [&'a str],
cursor: (usize, usize),
focused: bool,
/// The display column of the character index `cursor` of `line`.
pub(crate) fn column(line: &str, cursor: usize) -> usize {
line.chars()
.take(cursor)
.map(|ch| usize::from(width_of(ch)))
.sum()
/// `start` moved the least that brings `span` cells from `at` into `size` cells from it; a span
/// wider than `size` is shown from its start.
pub(crate) fn follow(start: usize, at: usize, span: usize, size: u16) -> usize {
let size = usize::from(size).max(1);
let end = at.saturating_add(span.clamp(1, size));
if at < start {
at
} else if end > start.saturating_add(size) {
end - size
} else {
start
fn scroll_offset(at: usize) -> u16 {
u16::try_from(at).unwrap_or(u16::MAX)
/// `previous` scrolled the least that keeps the `cursor` of `lines` inside `area`.
fn follow_cursor(
area: Rect,
lines: &[&str],
previous: Viewport,
) -> Viewport {
let (row, col) = cursor;
let line = lines.get(row).copied().unwrap_or("");
let under = line
.chars()
.nth(col)
.map_or(1, |ch| usize::from(width_of(ch)));
let last_top = RowWindow::last_top(lines.len(), usize::from(area.height));
Viewport {
top: follow(previous.top.min(last_top), row, 1, area.height),
left: follow(previous.left, column(line, col), under, area.width),
impl<'a> TextView<'a> {
#[must_use]
pub fn new(
) -> Self {
TextView {
theme,
lines,
cursor,
focused,
/// The screen cell of the `cursor` of `lines` drawn in `area` through `viewport`.
pub fn cursor(
viewport: Viewport,
) -> Position {
let at = lines.get(row).map_or(0, |line| column(line, col));
let x = at.saturating_sub(viewport.left);
let y = row.saturating_sub(viewport.top);
Position::new(
area.x.saturating_add(scroll_offset(x)),
area.y.saturating_add(scroll_offset(y)),
)
/// The (line, character index) a click at `point` lands on in `lines` drawn in `area`
/// through `viewport`: the inverse of [`TextView::cursor`]. A click below the text lands on
/// the last line and one past a line's end at its end.
pub fn position_at(
point: Position,
) -> (usize, usize) {
let row = viewport
.top
.saturating_add(usize::from(point.y.saturating_sub(area.y)))
.min(lines.len().saturating_sub(1));
let dx = usize::from(point.x.saturating_sub(area.x));
(row, index_at_column(line, viewport.left.saturating_add(dx)))
/// One line drawn into the row `y` of `area`, its first `left` columns scrolled off; a wide
/// character cut by either edge is left out.
pub(crate) fn draw_row(
buf: &mut Buffer,
y: u16,
line: &str,
left: usize,
style: Style,
) {
let mut col = 0;
let mut last = None;
for ch in line.chars() {
let width = usize::from(width_of(ch));
if width == 0 {
if col > left {
join_to_previous(buf, last, ch);
continue;
let start = col;
col += width;
if start < left {
let x = scroll_offset(start - left);
if x.saturating_add(scroll_offset(width)) > area.width {
break;
let at = Position::new(area.x + x, y);
buf[at].set_char(shown(ch)).set_style(style);
last = Some(at);
impl StatefulWidget for TextView<'_> {
type State = Viewport;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Viewport) {
*state = follow_cursor(area, self.lines, self.cursor, *state);
let area = area.intersection(buf.area);
let role = if self.focused {
Role::InputFocused
Role::Input
};
let style = self.theme.style(role);
buf.set_style(area, style);
let shown = self.lines.iter().skip(state.top);
for (y, line) in (area.y..area.bottom()).zip(shown) {
draw_row(buf, area, y, line, state.left, style);
#[cfg(test)]
mod tests;