mod edit;
mod focus;
mod keymap;
mod nav;
mod select;
mod style;
mod value;
mod view;
mod words;
pub use keymap::KeyMap;
pub use select::Position;
pub use style::{CursorStyle, StyleState, Styles};
use super::cursor::{self, Mode};
use super::{Flags, viewport};
use crate::style::{Border, Style};
use crate::{Cmd, Msg as TeaMsg, batch};
use std::sync::Arc;
pub type Msg = cursor::Msg;
const MIN_HEIGHT: usize = 1;
const DEFAULT_HEIGHT: usize = 6;
const DEFAULT_WIDTH: usize = 40;
const DEFAULT_MAX_HEIGHT: usize = 99;
const DEFAULT_MAX_WIDTH: usize = 500;
const MAX_LINES: usize = 10_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PromptInfo {
pub line_number: usize,
pub focused: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct LineInfo {
pub width: usize,
pub char_width: usize,
pub height: usize,
pub start_column: usize,
pub column_offset: usize,
pub row_offset: usize,
pub char_offset: usize,
}
const USE_VIRTUAL_CURSOR: u16 = 1 << 0;
const FOCUS: u16 = 1 << 1;
const HAS_SELECTION: u16 = 1 << 2;
const SELECTING: u16 = 1 << 3;
const PASTE_PENDING: u16 = 1 << 4;
#[derive(Clone)]
pub struct Model {
pub err: Option<String>,
pub prompt: String,
pub placeholder: String,
pub show_line_numbers: bool,
pub end_of_buffer_character: char,
pub key_map: KeyMap,
pub char_limit: usize,
pub max_height: usize,
pub max_width: usize,
pub dynamic_height: bool,
pub min_height: usize,
pub max_content_height: usize,
styles: Styles,
virtual_cursor: cursor::Model,
prompt_fn: Option<Arc<dyn Fn(PromptInfo) -> String + Send + Sync>>,
prompt_width: usize,
width: usize,
height: usize,
value: Vec<Vec<char>>,
col: usize,
row: usize,
last_char_offset: usize,
viewport: viewport::Model,
anchor: Position,
head: Position,
flags: Flags,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
let mut vp = viewport::Model::new(0, 0);
vp.key_map = viewport::KeyMap::none();
let mut m = Self {
err: None,
prompt: format!("{} ", Border::THICK.left),
placeholder: String::new(),
show_line_numbers: true,
end_of_buffer_character: ' ',
key_map: KeyMap::default(),
char_limit: 0,
max_height: DEFAULT_MAX_HEIGHT,
max_width: DEFAULT_MAX_WIDTH,
dynamic_height: false,
min_height: 0,
max_content_height: 0,
styles: Styles::new(true),
virtual_cursor: cursor::Model::new(),
prompt_fn: None,
prompt_width: 0,
width: 0,
height: 0,
value: vec![Vec::new()],
col: 0,
row: 0,
last_char_offset: 0,
viewport: vp,
anchor: Position::default(),
head: Position::default(),
flags: Flags(USE_VIRTUAL_CURSOR),
};
m.update_cursor_style();
m.set_height(DEFAULT_HEIGHT);
m.set_width(DEFAULT_WIDTH);
m
}
#[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();
}
#[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();
}
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().foreground(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);
}
const fn active_style(&self) -> &StyleState {
if self.flags.get(FOCUS) {
&self.styles.focused
} else {
&self.styles.blurred
}
}
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
if !self.flags.get(FOCUS) {
self.virtual_cursor.blur();
return Cmd::none();
}
let old = (self.cursor_line_number(), self.col);
let first = match self.handle(msg) {
edit::Step::Done => Cmd::none(),
edit::Step::Run(cmd) => cmd,
edit::Step::Stop => return Cmd::none(),
};
self.recalculate_height();
self.viewport.set_content(&self.render_lines());
let cmds = [
first,
self.viewport.update(msg),
self.blink_on_move(msg, old),
];
self.reposition_view();
batch(cmds)
}
fn handle(&mut self, msg: &TeaMsg<Msg>) -> edit::Step {
match msg {
TeaMsg::Paste(text) => self.paste(text),
TeaMsg::KeyPress(key) => return self.press(key),
TeaMsg::Clipboard { content, .. } if self.flags.get(PASTE_PENDING) => {
self.flags.set(PASTE_PENDING, false);
self.paste(content);
}
_ => {}
}
edit::Step::Done
}
fn blink_on_move(&mut self, msg: &TeaMsg<Msg>, old: (usize, usize)) -> Cmd<Msg> {
if !self.flags.get(USE_VIRTUAL_CURSOR) {
return Cmd::none();
}
let blink = self.virtual_cursor.update(msg);
let moved = (self.cursor_line_number(), self.col) != old;
if !moved || self.virtual_cursor.mode() != Mode::Blink {
return blink;
}
self.virtual_cursor.is_blinked = false;
self.virtual_cursor.blink()
}
fn paste(&mut self, text: &str) {
self.delete_selection();
self.insert_from_user(&text.chars().collect::<Vec<_>>());
}
}
fn digits(n: usize) -> usize {
n.to_string().len()
}
pub fn blink() -> Cmd<Msg> {
cursor::blink()
}
#[cfg(test)]
mod tests;