use std::cell::Cell;
use std::ops::Range;
use ratatui::text::Span;
use super::prompt::Prompt;
pub const MAX_ROWS: u16 = 6;
fn char_width(c: char) -> usize {
let mut buf = [0u8; 4];
Span::raw(&*c.encode_utf8(&mut buf)).width()
}
#[derive(Debug, PartialEq, Eq)]
pub struct Wrapped {
pub rows: Vec<Range<usize>>,
pub pos: Vec<(usize, usize)>,
}
impl Wrapped {
fn on_row(&self, row: usize) -> impl Iterator<Item = (usize, usize)> + '_ {
self.pos.iter().enumerate().filter(move |(_, p)| p.0 == row).map(|(k, p)| (k, p.1))
}
}
pub fn wrap(text: &str, width: usize) -> Wrapped {
let width = width.max(1);
let chars: Vec<char> = text.chars().collect();
let n = chars.len();
let mut rows = Vec::new();
let mut pos = Vec::with_capacity(n + 1);
let (mut row, mut col, mut start) = (0, 0, 0);
for k in 0..=n {
let c = chars.get(k).copied();
let w = match c {
None | Some('\n') => 1,
Some(c) => char_width(c),
};
if col > 0 && col + w > width {
rows.push(start..k);
(row, col, start) = (row + 1, 0, k);
}
pos.push((row, col));
if c == Some('\n') {
rows.push(start..k);
(row, col, start) = (row + 1, 0, k + 1);
} else {
col += w;
}
}
rows.push(start..n);
Wrapped { rows, pos }
}
pub fn height(rows: usize) -> u16 {
(rows.min(MAX_ROWS as usize) as u16).max(1)
}
pub fn viewport_top(prev: usize, cursor_row: usize, rows: usize, height: usize) -> usize {
let height = height.max(1);
let top = prev.min(rows.saturating_sub(height));
if cursor_row < top {
cursor_row
} else if cursor_row >= top + height {
cursor_row + 1 - height
} else {
top
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Viewport {
pub top: usize,
pub lines: Vec<String>,
pub caret: (u16, u16),
}
pub fn viewport(text: &str, cursor: usize, width: usize, height: usize, prev_top: usize) -> Viewport {
let w = wrap(text, width);
let chars: Vec<char> = text.chars().collect();
let (row, col) = w.pos[cursor.min(chars.len())];
let top = viewport_top(prev_top, row, w.rows.len(), height);
let lines = w.rows[top..].iter().take(height.max(1)).map(|r| chars[r.clone()].iter().collect()).collect();
Viewport { top, lines, caret: (col as u16, (row - top) as u16) }
}
pub fn vertical(text: &str, cursor: usize, width: usize, up: bool) -> usize {
let w = wrap(text, width);
let last = w.pos.len() - 1;
let (row, col) = w.pos[cursor.min(last)];
let target = match (up, row) {
(true, 0) => return 0,
(true, r) => r - 1,
(false, r) if r + 1 >= w.rows.len() => return last,
(false, r) => r + 1,
};
let mut best = None;
for (k, c) in w.on_row(target) {
if best.is_none() || c <= col {
best = Some(k);
}
}
best.unwrap_or(cursor)
}
pub fn row_edge(text: &str, cursor: usize, width: usize, home: bool) -> usize {
let w = wrap(text, width);
let row = w.pos[cursor.min(w.pos.len() - 1)].0;
let mut on = w.on_row(row).map(|(k, _)| k);
let edge = if home { on.next() } else { on.last() };
edge.unwrap_or(cursor)
}
pub fn hscroll(text: &str, cursor: usize, width: usize) -> (usize, String, u16) {
let width = width.max(1);
let chars: Vec<char> = text.chars().collect();
let cursor = cursor.min(chars.len());
let caret_col: usize = chars[..cursor].iter().map(|&c| char_width(c)).sum();
let need = (caret_col + 1).saturating_sub(width);
let (mut start, mut skipped) = (0, 0);
while skipped < need && start < cursor {
skipped += char_width(chars[start]);
start += 1;
}
let mut shown = String::new();
let mut col = 0;
for &c in &chars[start..] {
let w = char_width(c);
if col + w > width {
break;
}
shown.push(c);
col += w;
}
(start, shown, (caret_col - skipped) as u16)
}
pub struct TextArea {
edit: Prompt,
top: Cell<usize>,
width: Cell<usize>,
}
impl Default for TextArea {
fn default() -> Self {
Self::new()
}
}
impl TextArea {
pub fn new() -> Self {
Self { edit: Prompt::new(Vec::new()), top: Cell::new(0), width: Cell::new(usize::MAX) }
}
pub fn with_text(text: &str) -> Self {
Self { edit: Prompt::with_input(Vec::new(), text.to_string()), ..Self::new() }
}
pub fn input(&self) -> &str {
self.edit.input()
}
pub fn cursor(&self) -> usize {
self.edit.cursor()
}
pub fn height(&self, width: u16) -> u16 {
height(wrap(self.input(), width as usize).rows.len())
}
pub fn view(&self, width: u16, height: u16) -> Viewport {
let v = viewport(self.input(), self.cursor(), width as usize, height as usize, self.top.get());
self.top.set(v.top);
self.width.set((width as usize).max(1));
v
}
pub fn insert_char(&mut self, c: char) {
self.edit.insert_char(c);
}
pub fn newline(&mut self) {
self.edit.insert_char('\n');
}
pub fn backspace(&mut self) {
self.edit.backspace();
}
pub fn delete(&mut self) {
self.edit.delete();
}
pub fn left(&mut self) {
self.edit.left();
}
pub fn right(&mut self) {
self.edit.right();
}
pub fn clear(&mut self) {
self.edit.clear();
self.top.set(0);
}
pub fn delete_word(&mut self) {
self.edit.delete_word();
}
pub fn up(&mut self) {
let at = vertical(self.input(), self.cursor(), self.width.get(), true);
self.edit.set_cursor(at);
}
pub fn down(&mut self) {
let at = vertical(self.input(), self.cursor(), self.width.get(), false);
self.edit.set_cursor(at);
}
pub fn home(&mut self) {
let at = row_edge(self.input(), self.cursor(), self.width.get(), true);
self.edit.set_cursor(at);
}
pub fn end(&mut self) {
let at = row_edge(self.input(), self.cursor(), self.width.get(), false);
self.edit.set_cursor(at);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn typed(s: &str) -> TextArea {
let mut t = TextArea::new();
for c in s.chars() {
if c == '\n' { t.newline() } else { t.insert_char(c) }
}
t
}
fn rows(text: &str, width: usize) -> Vec<String> {
let chars: Vec<char> = text.chars().collect();
wrap(text, width).rows.into_iter().map(|r| chars[r].iter().collect()).collect()
}
#[test]
fn short_text_is_one_row() {
assert_eq!(rows("", 10), [""]);
assert_eq!(rows("hello", 10), ["hello"]);
let w = wrap("hi", 10);
assert_eq!(w.pos, [(0, 0), (0, 1), (0, 2)]);
}
#[test]
fn long_input_wraps_by_char() {
assert_eq!(rows("abcdefghij", 4), ["abcd", "efgh", "ij"]);
let w = wrap("abcdefghij", 4);
assert_eq!(w.pos[3], (0, 3));
assert_eq!(w.pos[4], (1, 0));
assert_eq!(w.pos[10], (2, 2), "end of text");
}
#[test]
fn exact_width_row_puts_the_end_caret_on_the_next_row() {
assert_eq!(rows("abcd", 4), ["abcd", ""]);
assert_eq!(wrap("abcd", 4).pos[4], (1, 0));
assert_eq!(rows("abcd\nx", 4), ["abcd", "", "x"]);
let w = wrap("abcd\nx", 4);
assert_eq!(w.pos[4], (1, 0), "the newline's slot");
assert_eq!(w.pos[5], (2, 0));
}
#[test]
fn wide_glyphs_take_two_columns_and_never_split() {
assert_eq!(rows("漢漢漢漢", 6), ["漢漢漢", "漢"]);
assert_eq!(rows("漢漢漢", 5), ["漢漢", "漢"]);
let w = wrap("a漢b", 3);
assert_eq!(w.pos, [(0, 0), (0, 1), (1, 0), (1, 1)]);
assert_eq!(rows("漢x", 1), ["漢", "x", ""]);
}
#[test]
fn newlines_break_rows() {
assert_eq!(rows("a\nb", 10), ["a", "b"]);
assert_eq!(rows("a\n", 10), ["a", ""]);
assert_eq!(rows("\n\n", 10), ["", "", ""]);
let w = wrap("a\nb", 10);
assert_eq!(w.pos, [(0, 0), (0, 1), (1, 0), (1, 1)]);
}
#[test]
fn height_grows_to_the_cap() {
assert_eq!(height(0), 1);
assert_eq!(height(1), 1);
assert_eq!(height(4), 4);
assert_eq!(height(6), 6);
assert_eq!(height(40), MAX_ROWS);
assert_eq!(typed(&"x".repeat(100)).height(10), MAX_ROWS);
assert_eq!(typed("a\nb\nc").height(10), 3);
}
#[test]
fn viewport_follows_the_cursor() {
assert_eq!(viewport_top(2, 4, 10, 3), 2);
assert_eq!(viewport_top(0, 7, 10, 3), 5);
assert_eq!(viewport_top(5, 1, 10, 3), 1);
assert_eq!(viewport_top(8, 2, 4, 3), 1);
}
#[test]
fn caret_at_every_edge_is_visible() {
let text = "abcdefghijklmnopqrstuvwxyz"; for cursor in 0..=26 {
let v = viewport(text, cursor, 4, 3, 0);
assert!(v.caret.0 < 4 && v.caret.1 < 3, "cursor {cursor}: {v:?}");
assert!(v.lines.len() <= 3);
}
assert_eq!(viewport(text, 0, 4, 3, 0), Viewport { top: 0, lines: vec!["abcd".into(), "efgh".into(), "ijkl".into()], caret: (0, 0) });
let v = viewport(text, 26, 4, 3, 0);
assert_eq!((v.top, v.caret), (4, (2, 2)));
assert_eq!(v.lines, ["qrst", "uvwx", "yz"]);
let v = viewport("abcdefgh", 8, 4, 6, 0);
assert_eq!((v.lines.len(), v.caret), (3, (0, 2)));
}
#[test]
fn resize_rewraps_and_keeps_the_cursor_visible() {
let text = "x".repeat(60);
let wide = viewport(&text, 60, 30, 6, 0);
assert_eq!((wide.top, wide.caret), (0, (0, 2)));
let narrow = viewport(&text, 60, 8, 6, wide.top);
assert_eq!((narrow.top, narrow.caret), (2, (4, 5)));
let back = viewport(&text, 60, 80, 6, narrow.top);
assert_eq!((back.top, back.caret), (0, (60, 0)));
}
#[test]
fn up_and_down_move_by_visual_row() {
let text = "abcdefghij"; assert_eq!(vertical(text, 6, 4, true), 2);
assert_eq!(vertical(text, 2, 4, false), 6);
assert_eq!(vertical(text, 7, 4, false), 10);
assert_eq!(vertical(text, 2, 4, true), 0);
assert_eq!(vertical(text, 9, 4, false), 10);
assert_eq!(vertical("abc\nd", 2, 10, false), 5);
assert_eq!(vertical("abc\nd", 5, 10, true), 1);
}
#[test]
fn home_and_end_stay_on_the_row() {
let text = "abcdefghij";
assert_eq!(row_edge(text, 6, 4, true), 4);
assert_eq!(row_edge(text, 5, 4, false), 7);
assert_eq!(row_edge(text, 8, 4, false), 10);
assert_eq!(row_edge("ab\ncd", 1, 10, false), 2, "before the newline");
}
#[test]
fn textarea_edits_and_scrolls() {
let mut t = typed("hello\nworld");
assert_eq!(t.input(), "hello\nworld");
t.view(20, 6);
t.up();
assert_eq!(t.cursor(), 5);
t.home();
assert_eq!(t.cursor(), 0);
t.down();
t.end();
assert_eq!(t.cursor(), 11);
t.backspace();
assert_eq!(t.input(), "hello\nworl");
t.clear();
assert_eq!((t.input(), t.cursor()), ("", 0));
let mut t = typed(&"y".repeat(50));
assert_eq!(t.view(5, 6).top, 5);
for _ in 0..10 {
t.up();
t.view(5, 6);
}
assert_eq!((t.cursor(), t.view(5, 6).top), (0, 0));
}
#[test]
fn hscroll_keeps_the_caret_in_view() {
assert_eq!(hscroll("hello", 5, 10), (0, "hello".into(), 5));
assert_eq!(hscroll("hello", 5, 5), (1, "ello".into(), 4));
assert_eq!(hscroll("hello", 0, 5), (0, "hello".into(), 0));
assert_eq!(hscroll("abcdefghijklmnop", 12, 5), (8, "ijklm".into(), 4));
assert_eq!(hscroll("漢漢漢", 3, 4), (2, "漢".into(), 2));
assert_eq!(hscroll("a漢漢", 3, 4), (2, "漢".into(), 2));
assert_eq!(hscroll("漢漢漢", 1, 4), (0, "漢漢".into(), 2));
for cursor in 0..=16 {
let (_, _, caret) = hscroll("abcdefghijklmnop", cursor, 5);
assert!(caret < 5, "cursor {cursor}");
}
}
}