Lines
98.54 %
Functions
59.57 %
Branches
100 %
//! Text-input editor: a buffer with a cursor and the editing operations the
//! keymap names. It does no I/O, so it can be tested without a terminal; the only trace of
//! drawing it keeps is the scroll position of the text it is shown in.
use std::cell::Cell;
use tvui::Viewport;
mod lines;
pub use lines::TEXT_AREA_ROWS;
/// A text buffer with a cursor. Loaded text is kept verbatim, newlines
/// included; a single-line editor cannot type one, a multi-line one
/// ([`Editor::multiline`]) can and moves by lines.
#[derive(Debug, Clone, Default)]
pub struct Editor {
buffer: String,
cursor: usize,
multiline: bool,
scroll: Cell<Viewport>,
}
impl Editor {
#[must_use]
pub fn with_buffer(buffer: impl Into<String>) -> Self {
let buf: String = buffer.into();
let cursor = buf.chars().count();
Self {
buffer: buf,
cursor,
multiline: false,
scroll: Cell::default(),
pub fn buffer(&self) -> &str {
&self.buffer
/// The cell the draw of the field showing this editor keeps its scroll in.
pub fn scroll(&self) -> &Cell<Viewport> {
&self.scroll
/// Where the field showing this editor is scrolled to.
pub fn viewport(&self) -> Viewport {
self.scroll.get()
/// Remember the scroll the drawing of the field ended at; the frame only borrows the editor.
pub fn set_viewport(&self, viewport: Viewport) {
self.scroll.set(viewport);
pub fn cursor(&self) -> usize {
self.cursor
/// Puts the cursor before character `index`, or at the end when the text is shorter.
pub fn set_cursor(&mut self, index: usize) {
self.cursor = index.min(self.buffer.chars().count());
/// Replace the entire buffer with `new`, placing the cursor at the end.
pub fn replace_buffer(&mut self, new: impl Into<String>) {
let buf: String = new.into();
self.cursor = buf.chars().count();
self.buffer = buf;
fn chars(&self) -> Vec<char> {
self.buffer.chars().collect()
fn rebuild(&mut self, chars: &[char]) {
self.buffer = chars.iter().collect();
if self.cursor > chars.len() {
self.cursor = chars.len();
/// Insert a single character at the cursor.
pub fn insert_char(&mut self, c: char) {
let mut chars = self.chars();
chars.insert(self.cursor, c);
self.cursor += 1;
self.rebuild(&chars);
pub fn delete_backward(&mut self) {
let span = self.terminator_span(&chars, self.cursor, true).unwrap_or(1);
let Some(start) = self.cursor.checked_sub(span) else {
return;
};
chars.drain(start..self.cursor);
self.cursor = start;
pub fn delete_forward(&mut self) {
if self.cursor < chars.len() {
let span = self
.terminator_span(&chars, self.cursor, false)
.unwrap_or(1);
chars.drain(self.cursor..self.cursor + span);
pub fn move_left(&mut self) {
self.cursor = self.cursor.saturating_sub(1);
if self.inside_crlf() {
self.cursor -= 1;
pub fn move_right(&mut self) {
if self.cursor < self.buffer.chars().count() {
pub fn move_home(&mut self) {
self.cursor = self.home_target();
pub fn move_end(&mut self) {
self.cursor = self.end_target();
/// Delete from the cursor to end-of-line (emacs C-k).
/// In a multi-line editor it stops at the line end, and at the line end
/// it deletes the line terminator instead.
pub fn kill_to_end(&mut self) {
let end = self.end_target();
if self.multiline && self.cursor == end {
self.delete_forward();
chars.drain(self.cursor..end);
/// Delete the word before the cursor (emacs C-w).
pub fn kill_word_backward(&mut self) {
let mut i = self.cursor;
while i > 0 && chars[i - 1].is_whitespace() {
i -= 1;
while i > 0 && !chars[i - 1].is_whitespace() {
chars.drain(i..self.cursor);
self.cursor = i;
pub fn word_forward(&mut self) {
let chars = self.chars();
while i < chars.len() && !chars[i].is_whitespace() {
i += 1;
while i < chars.len() && chars[i].is_whitespace() {
pub fn word_backward(&mut self) {
pub fn delete_word_forward(&mut self) {
let start = self.cursor;
let mut i = start;
chars.drain(start..i);
#[cfg(test)]
#[path = "editor_tests.rs"]
mod tests;