use super::{CursorShape, Event, Frame, Key, Mods, NamedKey, Rect, Widget};
use crate::style::palette::{BERRY, MUTED, ROSE, SOFT, TEXT, TRACK};
use crate::style::{ColorMode, Rgb, color_mode, italic, mix, paint, paint_on};
use crate::text::char_width;
#[derive(Clone, Debug)]
pub struct TextInput {
value: Vec<char>,
at: usize,
prompt: String,
placeholder: String,
focused: bool,
is_word: fn(char) -> bool,
history: Option<History>,
draft: Option<Vec<char>>,
terminal_cursor: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Edit {
Changed,
Moved,
Submitted,
Cancelled,
Ignored,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct History {
entries: Vec<String>,
at: Option<usize>,
limit: Option<usize>,
}
impl From<Vec<String>> for History {
fn from(entries: Vec<String>) -> Self {
History {
entries,
at: None,
limit: None,
}
}
}
impl History {
pub fn new() -> Self {
History::default()
}
#[must_use]
pub fn limit(mut self, lines: usize) -> Self {
self.limit = Some(lines);
self.trim();
self
}
pub fn push(&mut self, line: &str) {
self.at = None;
if line.trim().is_empty() {
return;
}
self.entries.retain(|e| e != line);
self.entries.push(line.to_string());
self.trim();
}
fn trim(&mut self) {
if let Some(limit) = self.limit {
let over = self.entries.len().saturating_sub(limit);
self.entries.drain(..over);
}
}
pub fn entries(&self) -> &[String] {
&self.entries
}
pub fn back(&mut self) -> Option<&str> {
let last = self.entries.len().checked_sub(1)?;
let at = self.at.map_or(last, |i| i.saturating_sub(1));
self.at = Some(at);
Some(&self.entries[at])
}
pub fn forward(&mut self) -> Option<&str> {
let at = self.at? + 1;
self.at = (at < self.entries.len()).then_some(at);
self.at.map(|i| self.entries[i].as_str())
}
pub fn is_browsing(&self) -> bool {
self.at.is_some()
}
pub fn reset(&mut self) {
self.at = None;
}
}
fn not_space(c: char) -> bool {
!c.is_whitespace()
}
impl Default for TextInput {
fn default() -> Self {
TextInput::new()
}
}
impl TextInput {
pub fn new() -> Self {
TextInput {
value: Vec::new(),
at: 0,
prompt: "›".into(),
placeholder: String::new(),
focused: false,
is_word: not_space,
history: None,
draft: None,
terminal_cursor: false,
}
}
#[must_use]
pub fn word(mut self, is_word: fn(char) -> bool) -> Self {
self.is_word = is_word;
self
}
#[must_use]
pub fn with_history(mut self, history: History) -> Self {
self.history = Some(history);
self
}
pub fn history(&self) -> Option<&History> {
self.history.as_ref()
}
#[must_use]
pub fn terminal_cursor(mut self, on: bool) -> Self {
self.terminal_cursor = on;
self
}
#[must_use]
pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
self.prompt = prompt.into();
self
}
#[must_use]
pub fn placeholder(mut self, text: impl Into<String>) -> Self {
self.placeholder = text.into();
self
}
pub fn value(&self) -> String {
self.value.iter().collect()
}
pub fn set_value(&mut self, value: &str) {
self.value = value.chars().collect();
self.at = self.value.len();
}
pub fn cursor(&self) -> usize {
self.at
}
pub fn set_cursor(&mut self, at: usize) {
self.at = at.min(self.value.len());
}
pub fn insert_str(&mut self, text: &str) {
let text = text.chars().filter_map(|c| match c {
'\n' | '\t' => Some(' '),
c if c.is_control() => None,
c => Some(c),
});
let before = self.value.len();
self.value.splice(self.at..self.at, text);
self.at += self.value.len() - before;
}
pub fn clear(&mut self) {
self.set_value("");
}
pub fn focus(&mut self, focused: bool) {
self.focused = focused;
}
pub fn is_focused(&self) -> bool {
self.focused
}
pub fn handle(&mut self, event: &Event) -> bool {
if matches!(event, Event::Key(Key::Enter | Key::Esc)) {
return false;
}
matches!(self.edit(event), Edit::Changed | Edit::Moved)
}
pub fn edit(&mut self, event: &Event) -> Edit {
if !self.focused {
return Edit::Ignored;
}
let key = match event {
Event::Key(key) => *key,
Event::Paste(text) => {
self.insert_str(text);
return Edit::Changed;
}
_ => return Edit::Ignored,
};
let (before, at) = (self.value.clone(), self.at);
match key {
Key::Enter => {
if let Some(h) = self.history.as_mut() {
h.push(&self.value.iter().collect::<String>());
}
self.draft = None;
return Edit::Submitted;
}
Key::Esc => {
if let Some(h) = self.history.as_mut() {
h.reset();
}
self.draft = None;
return Edit::Cancelled;
}
Key::Up | Key::Down if self.history.is_some() => {
let history = self.history.as_mut().expect("just checked");
if !history.is_browsing() {
self.draft = Some(self.value.clone());
}
let line = if key == Key::Up {
history.back().map(str::to_string)
} else {
history.forward().map(str::to_string)
};
match line {
Some(line) => self.value = line.chars().collect(),
None if key == Key::Down => self.value = self.draft.take().unwrap_or_default(),
None => {}
}
self.at = self.value.len();
}
Key::Char(c) => {
self.value.insert(at, c);
self.at += 1;
}
Key::Backspace if at > 0 => {
self.value.remove(at - 1);
self.at -= 1;
}
Key::Delete if at < self.value.len() => {
self.value.remove(at);
}
Key::Left => self.at = at.saturating_sub(1),
Key::Right => self.at = (at + 1).min(self.value.len()),
key if edit_line(&mut self.value, &mut self.at, key, self.is_word) => {}
Key::Backspace | Key::Delete => {}
_ => return Edit::Ignored,
}
if self.value != before {
Edit::Changed
} else {
Edit::Moved
}
}
pub fn cursor_cell(&self, area: Rect) -> (usize, usize) {
let lead = crate::text::width(&self.prompt) + 1;
let room = area.width.saturating_sub(lead);
let (_, x) = self.scrolled(room);
((area.x + lead + x).min((area.x + area.width).saturating_sub(1)), area.y)
}
fn scrolled(&self, room: usize) -> (usize, usize) {
let under = self.value.get(self.at).map_or(1, |&c| char_width(c).max(1));
let mut start = self.at;
let mut x = 0;
while start > 0 && x + char_width(self.value[start - 1]) + under <= room {
start -= 1;
x += char_width(self.value[start]);
}
(start, x)
}
}
impl Widget for TextInput {
fn render(&self, area: Rect, frame: &mut Frame) {
if area.is_empty() {
return;
}
let line = area.row(0);
let lead = format!("{} ", self.prompt);
let lead_w = crate::text::width(&lead);
if self.focused {
frame.tint(line, mix(TRACK, BERRY, 0.18));
}
frame.print(line, 0, &paint(if self.focused { ROSE } else { MUTED }, &lead));
let room = line.width.saturating_sub(lead_w);
let field = Rect::new(line.x + lead_w, line.y, room, 1);
if self.value.is_empty() {
let hint = paint(MUTED, &italic(&self.placeholder));
frame.print(field, 0, &hint);
if self.focused && self.terminal_cursor {
frame.set_cursor(field.x, field.y, CursorShape::Bar);
} else if self.focused {
let under = self.placeholder.chars().next().unwrap_or(' ');
frame.print(field, 0, &caret(under));
}
return;
}
let (start, x) = self.scrolled(room);
let mut used = 0;
let shown: String = self.value[start..]
.iter()
.take_while(|&&c| {
used += char_width(c);
used <= room
})
.collect();
frame.print(field, 0, &paint(if self.focused { TEXT } else { SOFT }, &shown));
if self.focused && self.terminal_cursor {
frame.set_cursor(field.x + x, field.y, CursorShape::Bar);
} else if self.focused {
let under = self.value.get(self.at).copied().unwrap_or(' ');
frame.print(
Rect::new(field.x + x, field.y, room.saturating_sub(x), 1),
0,
&caret(under),
);
}
}
}
pub(super) fn edit_line(line: &mut Vec<char>, at: &mut usize, key: Key, is_word: fn(char) -> bool) -> bool {
let space = |c: char| c.is_whitespace();
let word_start = |line: &[char], mut i: usize| {
while i > 0 && space(line[i - 1]) {
i -= 1;
}
let word = i > 0 && is_word(line[i - 1]);
while i > 0 && !space(line[i - 1]) && is_word(line[i - 1]) == word {
i -= 1;
}
i
};
let word_end = |line: &[char], mut i: usize| {
while i < line.len() && space(line[i]) {
i += 1;
}
let word = i < line.len() && is_word(line[i]);
while i < line.len() && !space(line[i]) && is_word(line[i]) == word {
i += 1;
}
i
};
let by_word = |m: Mods| m.ctrl() || m.alt();
match key {
Key::Home | Key::Ctrl('a') => *at = 0,
Key::End | Key::Ctrl('e') => *at = line.len(),
Key::Ctrl('u') => {
line.drain(..*at);
*at = 0;
}
Key::Ctrl('k') => line.truncate(*at),
Key::Ctrl('w') | Key::Mod(Mods::ALT, NamedKey::Backspace) => {
let start = word_start(line, *at);
line.drain(start..*at);
*at = start;
}
Key::Alt('b') => *at = word_start(line, *at),
Key::Mod(m, NamedKey::Left) if by_word(m) => *at = word_start(line, *at),
Key::Alt('f') => *at = word_end(line, *at),
Key::Mod(m, NamedKey::Right) if by_word(m) => *at = word_end(line, *at),
_ => return false,
}
true
}
pub(super) fn caret(c: char) -> String {
if color_mode() == ColorMode::None {
return format!("\x1b[7m{c}\x1b[27m");
}
paint_on(Rgb(40, 30, 36), ROSE, &c.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
fn typed(input: &mut TextInput, keys: &[Key]) {
for k in keys {
input.handle(&Event::Key(*k));
}
}
fn drawn(input: &TextInput, w: usize) -> String {
crate::set_color_mode(ColorMode::None);
let mut f = Frame::new(w, 1);
f.render(f.area(), input);
f.snapshot().plain()
}
#[test]
fn edits_and_moves() {
let mut input = TextInput::new();
typed(&mut input, &[Key::Char('x')]);
assert_eq!(input.value(), "", "no focus, no typing");
input.focus(true);
input.set_value("deploy my app");
typed(&mut input, &[Key::Ctrl('w')]);
assert_eq!(input.value(), "deploy my ");
typed(
&mut input,
&[Key::Ctrl('w'), Key::Ctrl('a'), Key::Right, Key::Ctrl('k')],
);
assert_eq!(input.value(), "d");
typed(&mut input, &[Key::Backspace, Key::Backspace, Key::Delete, Key::Left]);
assert_eq!((input.value().as_str(), input.at), ("", 0));
input.set_value("abc");
typed(&mut input, &[Key::Left, Key::Ctrl('u'), Key::Ctrl('e'), Key::Char('!')]);
assert_eq!(input.value(), "c!");
input.set_value("one two three");
let ctrl = |k| Key::Mod(Mods::CTRL, k);
typed(&mut input, &[Key::Alt('b'), ctrl(NamedKey::Left), Key::Char('_')]);
assert_eq!(input.value(), "one _two three");
typed(&mut input, &[ctrl(NamedKey::Right), Key::Alt('f'), Key::Char('!')]);
assert_eq!(input.value(), "one _two three!");
typed(&mut input, &[Key::Mod(Mods::ALT, NamedKey::Backspace)]);
assert_eq!(input.value(), "one _two ");
assert!(!input.handle(&Event::Tick) && !input.handle(&Event::Key(Key::Tab)));
input.clear();
assert!(input.value().is_empty() && input.is_focused());
}
#[test]
fn a_paste_goes_in_at_the_cursor_on_one_line() {
let mut input = TextInput::new();
let paste = Event::Paste("b\nc\td\x07".into());
assert!(!input.handle(&paste), "no focus, no pasting");
input.focus(true);
input.set_value("a!");
typed(&mut input, &[Key::Left]);
assert!(input.handle(&paste));
assert_eq!(input.value(), "ab c d!");
typed(&mut input, &[Key::Char('?')]);
assert_eq!(input.value(), "ab c d?!");
}
#[test]
fn long_text_scrolls_to_keep_the_cursor_in_view() {
let mut input = TextInput::default();
input.focus(true);
input.set_value("abcdefghij");
assert_eq!(drawn(&input, 8), "› fghij ", "the cursor sits past the end");
typed(&mut input, &[Key::Home]);
assert_eq!(drawn(&input, 8), "› abcdef");
input.focus(false);
assert_eq!(drawn(&input, 20), "› abcdefghij");
assert_eq!(drawn(&TextInput::new(), 0), "");
input.focus(true);
input.set_value(&("x".repeat(20_000) + "日本語"));
assert_eq!(drawn(&input, 10), "› x日本語 ");
typed(&mut input, &[Key::Left]);
assert_eq!(drawn(&input, 10), "› xx日本語");
}
#[test]
fn the_cursor_shows_without_colour_too() {
crate::set_color_mode(ColorMode::None);
let mut input = TextInput::new().placeholder("name");
input.focus(true);
let mut f = Frame::new(10, 1);
f.render(f.area(), &input);
assert!(f.snapshot().to_string().contains("\x1b[0;7mn"), "{:?}", f.snapshot());
}
#[test]
fn deletes_forwards_and_lights_the_caret_in_colour() {
let mut input = TextInput::new();
input.focus(true);
input.set_value("abc");
typed(&mut input, &[Key::Home, Key::Delete]);
assert_eq!(input.value(), "bc");
let lit = crate::style::with_color_mode(ColorMode::TrueColor, || caret('x'));
assert!(lit.contains("48;2;") && lit.contains('x'), "{lit:?}");
}
#[test]
fn an_empty_field_without_focus_shows_its_placeholder() {
let input = TextInput::new().placeholder("name");
assert!(drawn(&input, 20).contains("name"));
}
#[test]
fn edits_say_what_they_did() {
let mut input = TextInput::new();
let key = |k| Event::Key(k);
assert_eq!(input.edit(&key(Key::Char('a'))), Edit::Ignored, "no focus");
input.focus(true);
assert_eq!(input.edit(&key(Key::Char('a'))), Edit::Changed);
assert_eq!(input.edit(&key(Key::Home)), Edit::Moved);
assert_eq!(
input.edit(&key(Key::Backspace)),
Edit::Moved,
"nothing before the cursor"
);
assert_eq!(input.edit(&Event::Paste("bc".into())), Edit::Changed);
assert_eq!(input.edit(&key(Key::Esc)), Edit::Cancelled);
assert_eq!(input.edit(&key(Key::Enter)), Edit::Submitted);
assert_eq!(input.edit(&key(Key::Up)), Edit::Ignored, "no history to go through");
assert_eq!(input.edit(&Event::Tick), Edit::Ignored);
assert_eq!((input.value().as_str(), input.cursor()), ("bca", 2));
input.set_cursor(99);
input.insert_str("d\ne");
assert_eq!(input.value(), "bcad e");
}
#[test]
fn history_goes_back_and_comes_forward_to_what_was_typed() {
let mut input = TextInput::new().with_history(History::from(vec!["one".into(), "two".into()]).limit(3));
input.focus(true);
let press = |input: &mut TextInput, k| input.edit(&Event::Key(k));
input.set_value("dra");
press(&mut input, Key::Up);
assert_eq!(input.value(), "two");
press(&mut input, Key::Up);
press(&mut input, Key::Up);
assert_eq!(input.value(), "one", "stops at the oldest");
press(&mut input, Key::Down);
assert_eq!(input.value(), "two");
press(&mut input, Key::Down);
assert_eq!(
(input.value().as_str(), input.cursor()),
("dra", 3),
"back to the draft"
);
assert_eq!(press(&mut input, Key::Down), Edit::Moved);
input.set_value("three");
assert_eq!(press(&mut input, Key::Enter), Edit::Submitted);
input.set_value("one");
press(&mut input, Key::Enter);
assert_eq!(input.history().unwrap().entries(), ["two", "three", "one"]);
input.set_value("four");
press(&mut input, Key::Enter);
assert_eq!(
input.history().unwrap().entries(),
["three", "one", "four"],
"kept to its limit"
);
let mut blank = History::new();
blank.push(" ");
assert!(blank.entries().is_empty() && blank.back().is_none() && blank.forward().is_none());
}
#[test]
fn words_are_what_the_box_says() {
let mut input = TextInput::new().word(|c| c.is_alphanumeric() || c == '_');
input.focus(true);
input.set_value("call(my_arg");
typed(&mut input, &[Key::Ctrl('w')]);
assert_eq!(input.value(), "call(");
typed(&mut input, &[Key::Ctrl('w')]);
assert_eq!(input.value(), "call");
typed(&mut input, &[Key::Alt('b')]);
assert_eq!(input.cursor(), 0);
typed(&mut input, &[Key::Alt('f')]);
assert_eq!(input.cursor(), 4);
}
#[test]
fn the_terminal_cursor_can_show_where_typing_goes() {
let mut input = TextInput::new().terminal_cursor(true);
input.focus(true);
let mut f = Frame::new(10, 1);
f.render(f.area(), &input);
assert_eq!(f.cursor().map(|c| (c.x, c.y)), Some((2, 0)));
input.set_value("abc");
let mut f = Frame::new(10, 1);
f.render(f.area(), &input);
assert_eq!(f.cursor().map(|c| (c.x, c.shape)), Some((5, CursorShape::Bar)));
assert_eq!(input.cursor_cell(f.area()), (5, 0));
}
}