Lines
98.08 %
Functions
96.15 %
Branches
100 %
//! A one-row text field as Turbo Vision draws it: the text starts one cell in, and a `◄` or `►`
//! in the outer cells says that text is scrolled off that side.
use ratatui::buffer::Buffer;
use ratatui::layout::{Margin, Position, Rect};
use ratatui::style::Style;
use ratatui::text::Span;
use ratatui::widgets::StatefulWidget;
use crate::text_view::{column, draw_row, follow};
use crate::{Role, Theme, Viewport};
/// The text of a field and the cursor as a character index. Rendering scrolls the
/// [`Viewport`] state the least that keeps the character under the cursor between the arrow
/// cells, as [`TextView`](crate::TextView) does; the terminal cursor itself is placed by the
/// caller at [`InputLine::cursor`].
pub struct InputLine<'a> {
theme: &'a Theme,
text: &'a str,
cursor: usize,
focused: bool,
}
/// The cells an input line keeps at its two ends for the scroll arrows.
pub const ARROW_CELLS: u16 = 2;
const LEFT_ARROW: char = '\u{25c4}';
const RIGHT_ARROW: char = '\u{25ba}';
/// The part of `area` the text is drawn in: a field too narrow for text between two arrow cells
/// has no arrows.
fn text_area(area: Rect) -> Rect {
let margin = u16::from(area.width >= 3);
area.inner(Margin::new(margin, 0))
/// The cells `ch` takes; a control character is drawn as one blank, see [`shown`].
pub(crate) fn width_of(ch: char) -> u16 {
if ch.is_control() {
return 1;
let mut buf = [0; 4];
u16::try_from(Span::raw(&*ch.encode_utf8(&mut buf)).width()).unwrap_or(u16::MAX)
/// The character put in a cell for `ch`: control characters must not reach the terminal.
pub(crate) fn shown(ch: char) -> char {
if ch.is_control() { ' ' } else { ch }
/// The index of the character of `text` that covers display column `target`, counted from the
/// start of the text; the character count when `target` is past the end.
pub(crate) fn index_at_column(text: &str, target: usize) -> usize {
let mut col = 0;
for (index, ch) in text.chars().enumerate() {
let width = usize::from(width_of(ch));
if width > 0 && target < col + width {
return index;
col += width;
text.chars().count()
/// A zero-width character such as a combining mark joins the symbol of the cell before it.
pub(crate) fn join_to_previous(buf: &mut Buffer, last: Option<Position>, ch: char) {
if let Some(at) = last {
let joined = format!("{}{ch}", buf[at].symbol());
buf[at].set_symbol(&joined);
impl<'a> InputLine<'a> {
#[must_use]
pub fn new(theme: &'a Theme, text: &'a str, cursor: usize, focused: bool) -> Self {
InputLine {
theme,
text,
cursor,
focused,
/// The screen cell of the cursor of `text` drawn in `area` through `viewport`.
pub fn cursor(area: Rect, text: &str, cursor: usize, viewport: Viewport) -> Position {
let x = column(text, cursor).saturating_sub(viewport.left);
Position::new(
text_area(area)
.x
.saturating_add(u16::try_from(x).unwrap_or(u16::MAX)),
area.y,
)
/// The character index a click at `point` lands on in `text` drawn in `area` through
/// `viewport`: the inverse of [`InputLine::cursor`]. A click past the end lands at the end.
pub fn position_at(area: Rect, text: &str, viewport: Viewport, point: Position) -> usize {
let dx = usize::from(point.x.saturating_sub(text_area(area).x));
index_at_column(text, viewport.left.saturating_add(dx))
impl InputLine<'_> {
fn draw_arrows(&self, area: Rect, buf: &mut Buffer, left: usize, style: Style) {
let arrow = self
.theme
.style(Role::InputArrow)
.fg
.map_or(style, |fg| style.fg(fg));
let shown = [
(left > 0, area.x, LEFT_ARROW),
(
column(self.text, usize::MAX)
> left.saturating_add(usize::from(text_area(area).width)),
area.right().saturating_sub(1),
RIGHT_ARROW,
),
];
for (_, x, glyph) in shown.into_iter().filter(|(on, ..)| *on) {
if let Some(cell) = buf.cell_mut((x, area.y)) {
cell.set_char(glyph).set_style(arrow);
impl StatefulWidget for InputLine<'_> {
type State = Viewport;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Viewport) {
let inner = text_area(area);
let reach = area.right().saturating_sub(inner.x);
let under = self
.text
.chars()
.nth(self.cursor)
.map_or(1, |ch| usize::from(width_of(ch)));
let size = if under > 1 { inner.width } else { reach };
let filled = column(self.text, usize::MAX)
.saturating_add(1)
.saturating_sub(usize::from(reach));
state.left = follow(
state.left.min(filled),
column(self.text, self.cursor),
under,
size,
);
let role = if self.focused {
Role::InputFocused
} else {
Role::Input
};
let style = self.theme.style(role);
let visible = area.intersection(buf.area);
buf.set_style(visible, style);
if visible.is_empty() {
return;
let text = inner.intersection(buf.area);
draw_row(buf, text, text.y, self.text, state.left, style);
if area.width >= 3 {
self.draw_arrows(area, buf, state.left, style);
#[cfg(test)]
mod tests;