use std::cell::Cell;
use super::field::{caret, edit_line};
use super::{Event, Frame, Key, Rect, Widget};
use crate::style::palette::{BERRY, MUTED, SOFT, TEXT, TRACK};
use crate::style::{italic, mix, paint};
use crate::text::char_width;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Position {
pub row: usize,
pub column: usize,
}
#[derive(Clone, Debug)]
pub struct TextArea {
lines: Vec<Vec<char>>,
row: usize,
col: usize,
goal: Option<usize>,
placeholder: String,
focused: bool,
top: Cell<usize>,
}
impl Default for TextArea {
fn default() -> Self {
TextArea::new()
}
}
impl TextArea {
pub fn new() -> Self {
TextArea {
lines: vec![Vec::new()],
row: 0,
col: 0,
goal: None,
placeholder: String::new(),
focused: false,
top: Cell::new(0),
}
}
#[must_use]
pub fn placeholder(mut self, text: impl Into<String>) -> Self {
self.placeholder = text.into();
self
}
pub fn value(&self) -> String {
let lines: Vec<String> = self.lines.iter().map(|l| l.iter().collect()).collect();
lines.join("\n")
}
pub fn set_value(&mut self, value: &str) {
self.lines = vec![Vec::new()];
(self.row, self.col) = (0, 0);
self.insert(value);
}
pub fn clear(&mut self) {
self.set_value("");
}
pub fn cursor(&self) -> Position {
Position {
row: self.row,
column: self.col,
}
}
pub fn focus(&mut self, focused: bool) {
self.focused = focused;
}
pub fn is_focused(&self) -> bool {
self.focused
}
fn insert(&mut self, text: &str) {
for c in text.chars() {
match c {
'\n' => self.split_line(),
'\t' => (0..4).for_each(|_| self.put(' ')),
c if c.is_control() => {}
c => self.put(c),
}
}
}
fn put(&mut self, c: char) {
self.lines[self.row].insert(self.col, c);
self.col += 1;
}
fn split_line(&mut self) {
let rest = self.lines[self.row].split_off(self.col);
self.row += 1;
self.col = 0;
self.lines.insert(self.row, rest);
}
fn go_to_row(&mut self, row: usize) {
let goal = *self.goal.get_or_insert(self.col);
self.row = row;
self.col = goal.min(self.lines[row].len());
}
pub fn handle(&mut self, event: &Event) -> bool {
if !self.focused {
return false;
}
let key = match event {
Event::Key(key) => *key,
Event::Paste(text) => {
self.insert(text);
self.goal = None;
return true;
}
_ => return false,
};
let (row, col) = (self.row, self.col);
let last = self.lines.len() - 1;
match key {
Key::Up if row > 0 => return self.moved_to_row(row - 1),
Key::Down if row < last => return self.moved_to_row(row + 1),
Key::PageUp => return self.moved_to_row(row.saturating_sub(10)),
Key::PageDown => return self.moved_to_row((row + 10).min(last)),
Key::Up => self.col = 0,
Key::Down => self.col = self.lines[row].len(),
Key::Char(c) => self.put(c),
Key::Enter => self.split_line(),
Key::Backspace if col > 0 => {
self.lines[row].remove(col - 1);
self.col -= 1;
}
Key::Backspace if row > 0 => {
let line = self.lines.remove(row);
self.row -= 1;
self.col = self.lines[self.row].len();
self.lines[self.row].extend(line);
}
Key::Delete if col < self.lines[row].len() => {
self.lines[row].remove(col);
}
Key::Delete if row < last => {
let next = self.lines.remove(row + 1);
self.lines[row].extend(next);
}
Key::Left if col > 0 => self.col -= 1,
Key::Left if row > 0 => {
self.row -= 1;
self.col = self.lines[self.row].len();
}
Key::Right if col < self.lines[row].len() => self.col += 1,
Key::Right if row < last => (self.row, self.col) = (row + 1, 0),
key if edit_line(&mut self.lines[row], &mut self.col, key, |c| !c.is_whitespace()) => {}
Key::Backspace | Key::Delete | Key::Left | Key::Right => {}
_ => return false,
}
self.goal = None;
true
}
fn moved_to_row(&mut self, row: usize) -> bool {
self.go_to_row(row);
true
}
fn layout(&self, width: usize) -> (Vec<String>, (usize, usize)) {
let width = width.max(1);
let mut rows = Vec::new();
let mut cursor = (0, 0);
for (i, line) in self.lines.iter().enumerate() {
let (mut row, mut x) = (String::new(), 0);
for (j, &c) in line.iter().chain([' '].iter()).enumerate() {
let w = char_width(c);
if x + w > width && x > 0 {
rows.push(std::mem::take(&mut row));
x = 0;
}
if (i, j) == (self.row, self.col) {
cursor = (rows.len(), x);
}
if j < line.len() {
row.push(c);
x += w;
}
}
rows.push(row);
}
(rows, cursor)
}
}
impl Widget for TextArea {
fn render(&self, area: Rect, frame: &mut Frame) {
if area.is_empty() {
return;
}
if self.focused {
frame.tint(area, mix(TRACK, BERRY, 0.18));
}
if self.lines.len() == 1 && self.lines[0].is_empty() {
frame.print(area, 0, &paint(MUTED, &italic(&self.placeholder)));
if self.focused {
frame.print(area, 0, &caret(self.placeholder.chars().next().unwrap_or(' ')));
}
return;
}
let (rows, (cursor_row, cursor_x)) = self.layout(area.width);
let h = area.height;
let top = self.top.get().min(cursor_row).max((cursor_row + 1).saturating_sub(h));
self.top.set(top);
let colour = if self.focused { TEXT } else { SOFT };
for (n, line) in rows.iter().skip(top).take(h).enumerate() {
frame.print(area, n, &paint(colour, line));
}
if self.focused {
let under = self.lines[self.row].get(self.col).copied().unwrap_or(' ');
let line = area.row(cursor_row - top);
let at = Rect::new(line.x + cursor_x, line.y, line.width.saturating_sub(cursor_x), 1);
frame.print(at, 0, &caret(under));
}
}
fn size(&self) -> Option<(usize, usize)> {
let widest = self
.lines
.iter()
.map(|l| l.iter().map(|&c| char_width(c)).sum::<usize>());
Some((widest.max().unwrap_or(0).max(20), self.lines.len().max(3)))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn at(text: &TextArea) -> (usize, usize) {
let p = text.cursor();
(p.row, p.column)
}
fn typed(text: &mut TextArea, keys: &[Key]) {
for k in keys {
text.handle(&Event::Key(*k));
}
}
fn drawn(text: &TextArea, w: usize, h: usize) -> String {
crate::set_color_mode(crate::ColorMode::None);
let mut f = Frame::new(w, h);
f.render(f.area(), text);
f.snapshot().plain()
}
fn focused(value: &str) -> TextArea {
let mut text = TextArea::new();
text.focus(true);
text.set_value(value);
text
}
#[test]
fn types_breaks_and_joins_lines() {
let mut text = TextArea::new();
typed(&mut text, &[Key::Char('x')]);
assert_eq!(text.value(), "", "no focus, no typing");
assert!(!text.handle(&Event::Paste("x".into())));
text.focus(true);
typed(
&mut text,
&[Key::Char('a'), Key::Enter, Key::Char('b'), Key::Left, Key::Backspace],
);
assert_eq!((text.value().as_str(), at(&text)), ("ab", (0, 1)));
typed(&mut text, &[Key::Enter, Key::Up, Key::End, Key::Delete]);
assert_eq!((text.value().as_str(), at(&text)), ("ab", (0, 1)));
typed(&mut text, &[Key::Delete, Key::Delete, Key::Home, Key::Backspace]);
assert_eq!(text.value(), "a", "nothing to delete at either end");
typed(&mut text, &[Key::End]);
assert!(text.handle(&Event::Paste("\tb\r\x07\nc".into())));
assert_eq!(text.value(), "a b\nc");
assert!(!text.handle(&Event::Tick) && !text.handle(&Event::Key(Key::Tab)));
text.clear();
assert_eq!((text.value().as_str(), at(&text)), ("", (0, 0)));
}
#[test]
fn moves_across_lines_keeping_its_column() {
let mut text = focused("long line\nab\nanother line");
assert_eq!(at(&text), (2, 12));
typed(&mut text, &[Key::Up]);
assert_eq!(at(&text), (1, 2), "as far as the short line goes");
typed(&mut text, &[Key::Up]);
assert_eq!(at(&text), (0, 9), "and back out to where it was aiming");
typed(&mut text, &[Key::Up]);
assert_eq!(at(&text), (0, 0), "up from the top goes to the start");
typed(&mut text, &[Key::PageDown]);
assert_eq!(at(&text), (2, 0));
typed(&mut text, &[Key::Down]);
assert_eq!(at(&text), (2, 12), "down from the bottom goes to the end");
typed(&mut text, &[Key::PageUp]);
assert_eq!(at(&text), (0, 9));
typed(&mut text, &[Key::Right]);
assert_eq!(at(&text), (1, 0), "right from a line's end goes to the next");
typed(&mut text, &[Key::Left]);
assert_eq!(at(&text), (0, 9), "and left comes back");
typed(&mut text, &[Key::Ctrl('a'), Key::Left]);
assert_eq!(at(&text), (0, 0), "nowhere left of the start");
typed(&mut text, &[Key::Ctrl('e')]);
assert_eq!(at(&text), (0, 9));
}
#[test]
fn clears_parts_of_a_line() {
let mut text = focused("keep\ndeploy my app");
typed(&mut text, &[Key::Ctrl('w')]);
assert_eq!(text.value(), "keep\ndeploy my ");
typed(&mut text, &[Key::Ctrl('w'), Key::Left, Key::Ctrl('k')]);
assert_eq!(text.value(), "keep\ndeploy");
typed(&mut text, &[Key::Left, Key::Ctrl('u')]);
assert_eq!((text.value().as_str(), at(&text)), ("keep\ny", (1, 0)));
}
#[test]
fn wraps_and_scrolls_to_keep_the_cursor_in_view() {
let mut text = focused("abcdefgh\nij");
assert_eq!(drawn(&text, 5, 4), "abcde\nfgh\nij \n");
typed(&mut text, &[Key::Up, Key::End]);
assert_eq!(text.layout(4).1, (2, 0), "past a full row, the cursor starts the next");
assert_eq!(drawn(&text, 5, 2), "abcde\nfgh ");
typed(&mut text, &[Key::Down]);
assert_eq!(drawn(&text, 5, 2), "fgh\nij ");
typed(&mut text, &[Key::Up]);
assert_eq!(drawn(&text, 5, 2), "fgh \nij");
typed(&mut text, &[Key::Ctrl('a')]);
assert_eq!(drawn(&text, 5, 2), "abcde\nfgh");
text.focus(false);
assert_eq!(drawn(&text, 5, 2), "abcde\nfgh");
assert_eq!(drawn(&text, 0, 0), "");
}
#[test]
fn shows_its_placeholder_and_wants_a_little_room() {
let empty = TextArea::new().placeholder("notes");
assert_eq!(drawn(&empty, 10, 1), "notes");
let mut lit = empty.clone();
lit.focus(true);
assert_eq!(drawn(&lit, 10, 1), "notes");
assert_eq!(TextArea::default().size(), Some((20, 3)));
assert_eq!(focused(&"x".repeat(30)).size(), Some((30, 3)));
}
}