pub(super) mod edit;
mod keymap;
mod style;
mod suggest;
mod value;
mod view;
pub use keymap::KeyMap;
pub use style::{CursorStyle, StyleState, Styles};
use super::Flags;
use super::cursor::{self, Mode};
use crate::style::Style;
use crate::{Cmd, Msg as TeaMsg, batch};
use std::sync::Arc;
pub type Msg = cursor::Msg;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum EchoMode {
#[default]
Normal,
Password,
None,
}
pub type ValidateFn = Arc<dyn Fn(&str) -> Result<(), String> + Send + Sync>;
const FOCUS: u16 = 1 << 0;
const USE_VIRTUAL_CURSOR: u16 = 1 << 1;
const PASTE_PENDING: u16 = 1 << 2;
#[derive(Clone)]
pub struct Model {
pub err: Option<String>,
pub prompt: String,
pub placeholder: String,
pub echo_mode: EchoMode,
pub echo_character: char,
pub char_limit: usize,
pub key_map: KeyMap,
pub validate: Option<ValidateFn>,
pub show_suggestions: bool,
styles: Styles,
width: usize,
value: Vec<char>,
pos: usize,
offset: usize,
offset_right: usize,
virtual_cursor: cursor::Model,
suggestions: Vec<Vec<char>>,
matched: Vec<Vec<char>>,
current_suggestion: usize,
flags: Flags,
}
impl std::fmt::Debug for Model {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Model")
.field("err", &self.err)
.field("prompt", &self.prompt)
.field("placeholder", &self.placeholder)
.field("echo_mode", &self.echo_mode)
.field("echo_character", &self.echo_character)
.field("char_limit", &self.char_limit)
.field("key_map", &self.key_map)
.field("validate", &super::opaque(self.validate.as_ref()))
.field("show_suggestions", &self.show_suggestions)
.field("styles", &self.styles)
.field("width", &self.width)
.field("value", &self.value)
.field("pos", &self.pos)
.field("offset", &self.offset)
.field("offset_right", &self.offset_right)
.field("virtual_cursor", &self.virtual_cursor)
.field("suggestions", &self.suggestions)
.field("matched", &self.matched)
.field("current_suggestion", &self.current_suggestion)
.field("flags", &self.flags)
.finish()
}
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
let mut m = Self {
err: None,
prompt: "> ".into(),
placeholder: String::new(),
echo_mode: EchoMode::Normal,
echo_character: '*',
char_limit: 0,
key_map: KeyMap::default(),
validate: None,
show_suggestions: false,
styles: Styles::new(true),
width: 0,
value: Vec::new(),
pos: 0,
offset: 0,
offset_right: 0,
virtual_cursor: cursor::Model::new(),
suggestions: Vec::new(),
matched: Vec::new(),
current_suggestion: 0,
flags: Flags(USE_VIRTUAL_CURSOR),
};
m.update_cursor_style();
m
}
#[must_use]
pub const fn virtual_cursor(&self) -> bool {
self.flags.get(USE_VIRTUAL_CURSOR)
}
pub fn set_virtual_cursor(&mut self, on: bool) {
self.flags.set(USE_VIRTUAL_CURSOR, on);
self.update_cursor_style();
}
#[must_use]
pub const fn styles(&self) -> Styles {
self.styles
}
pub fn set_styles(&mut self, styles: Styles) {
self.styles = styles;
self.update_cursor_style();
}
fn update_cursor_style(&mut self) {
let c = self.styles.cursor;
let mode = if !self.flags.get(USE_VIRTUAL_CURSOR) {
Mode::Hide
} else if c.blink {
Mode::Blink
} else {
Mode::Static
};
if self.flags.get(USE_VIRTUAL_CURSOR) {
self.virtual_cursor.style = Style::new().fg(c.color);
if c.blink && !c.blink_speed.is_zero() {
self.virtual_cursor.blink_speed = c.blink_speed;
}
}
let _ = self.virtual_cursor.set_mode(mode);
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
pub const fn set_width(&mut self, columns: usize) {
self.width = columns;
}
#[must_use]
pub const fn focused(&self) -> bool {
self.flags.get(FOCUS)
}
pub fn focus(&mut self) -> Cmd<Msg> {
self.flags.set(FOCUS, true);
self.virtual_cursor.focus()
}
pub const fn blur(&mut self) {
self.flags.set(FOCUS, false);
self.virtual_cursor.blur();
}
pub fn reset(&mut self) {
self.value.clear();
self.set_cursor(0);
}
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
if !self.flags.get(FOCUS) {
return Cmd::none();
}
let old = self.pos;
let mut cmds = Vec::new();
match msg {
TeaMsg::KeyPress(key) => cmds.push(self.press(key)),
TeaMsg::Paste(text) => self.insert_from_user(text),
TeaMsg::Clipboard { content, .. } if self.flags.get(PASTE_PENDING) => {
self.flags.set(PASTE_PENDING, false);
self.insert_from_user(content);
}
_ => {}
}
if self.flags.get(USE_VIRTUAL_CURSOR) {
cmds.push(self.virtual_cursor.update(msg));
if old != self.pos && self.virtual_cursor.mode() == Mode::Blink {
self.virtual_cursor.is_blinked = false;
cmds.push(self.virtual_cursor.blink());
}
}
self.handle_overflow();
batch(cmds)
}
fn check(&self, chars: &[char]) -> Option<String> {
let text: String = chars.iter().collect();
self.validate.as_ref().and_then(|f| f(&text).err())
}
}
#[must_use]
pub fn blink() -> Cmd<Msg> {
cursor::blink()
}
#[cfg(test)]
mod tests;