LineEditor

Struct LineEditor 

Source
pub struct LineEditor { /* private fields */ }
Expand description

Main line editor interface with full editing and history support.

Provides a high-level API for reading edited lines from any Terminal implementation. Handles all keyboard input, cursor movement, text editing, and history navigation.

§Examples

use editline::{LineEditor, terminals::StdioTerminal};

let mut editor = LineEditor::new(1024, 50);
let mut terminal = StdioTerminal::new();

match editor.read_line(&mut terminal) {
    Ok(line) => println!("Got: {}", line),
    Err(e) => eprintln!("Error: {}", e),
}

§Key Bindings

  • Arrow keys: Move cursor left/right, navigate history up/down
  • Home/End: Jump to start/end of line
  • Backspace/Delete: Delete characters
  • Ctrl+Left/Right: Move by word
  • Alt+Backspace: Delete word left
  • Ctrl+Delete: Delete word right
  • Enter: Submit line

Implementations§

Source§

impl LineEditor

Source

pub fn new(buffer_capacity: usize, history_capacity: usize) -> Self

Creates a new line editor with the specified capacities.

§Arguments
  • buffer_capacity - Initial capacity for the line buffer in bytes
  • history_capacity - Maximum number of history entries to store
§Examples
use editline::LineEditor;

// 1024 byte buffer, 50 history entries
let editor = LineEditor::new(1024, 50);
Examples found in repository?
examples/simple_repl.rs (line 13)
6fn main() {
7    println!("Simple REPL - Type something and press Enter");
8    println!("Type 'exit' or press Ctrl-D to quit");
9    println!("Features: line editing, history (up/down), word navigation (Ctrl+arrows)");
10    println!("Press Ctrl-C to cancel current line");
11    println!();
12
13    let mut editor = LineEditor::new(1024, 50);
14    let mut terminal = StdioTerminal::new();
15
16    loop {
17        print!("> ");
18        std::io::Write::flush(&mut std::io::stdout()).unwrap();
19
20        match editor.read_line(&mut terminal) {
21            Ok(line) => {
22                if line == "exit" {
23                    println!("Goodbye!");
24                    break;
25                } else if !line.is_empty() {
26                    println!("typed: {}", line);
27                }
28            }
29            Err(e) => {
30                // Handle Ctrl-C and Ctrl-D
31                match e {
32                    editline::Error::Eof => {
33                        // Ctrl-D pressed - exit gracefully
34                        println!("\nGoodbye!");
35                        break;
36                    }
37                    editline::Error::Interrupted => {
38                        // Ctrl-C pressed - show message and continue
39                        println!("\nInterrupted. Type 'exit' or press Ctrl-D to quit.");
40                        continue;
41                    }
42                    _ => {
43                        eprintln!("\nError reading input: {}", e);
44                        break;
45                    }
46                }
47            }
48        }
49    }
50}
Source

pub fn read_line<T: Terminal>(&mut self, terminal: &mut T) -> Result<String>

Reads a line from the terminal with full editing support.

Enters raw mode, processes key events until Enter is pressed, then returns the edited line with leading and trailing whitespace removed. The trimmed line is automatically added to history if non-empty.

§Arguments
  • terminal - Any type implementing the Terminal trait
§Returns

Ok(String) with the trimmed entered line, or Err if an I/O error occurs.

§Examples
use editline::{LineEditor, terminals::StdioTerminal};

let mut editor = LineEditor::new(1024, 50);
let mut terminal = StdioTerminal::new();

print!("> ");
std::io::Write::flush(&mut std::io::stdout()).unwrap();

let line = editor.read_line(&mut terminal)?;
println!("You entered: {}", line);
Examples found in repository?
examples/simple_repl.rs (line 20)
6fn main() {
7    println!("Simple REPL - Type something and press Enter");
8    println!("Type 'exit' or press Ctrl-D to quit");
9    println!("Features: line editing, history (up/down), word navigation (Ctrl+arrows)");
10    println!("Press Ctrl-C to cancel current line");
11    println!();
12
13    let mut editor = LineEditor::new(1024, 50);
14    let mut terminal = StdioTerminal::new();
15
16    loop {
17        print!("> ");
18        std::io::Write::flush(&mut std::io::stdout()).unwrap();
19
20        match editor.read_line(&mut terminal) {
21            Ok(line) => {
22                if line == "exit" {
23                    println!("Goodbye!");
24                    break;
25                } else if !line.is_empty() {
26                    println!("typed: {}", line);
27                }
28            }
29            Err(e) => {
30                // Handle Ctrl-C and Ctrl-D
31                match e {
32                    editline::Error::Eof => {
33                        // Ctrl-D pressed - exit gracefully
34                        println!("\nGoodbye!");
35                        break;
36                    }
37                    editline::Error::Interrupted => {
38                        // Ctrl-C pressed - show message and continue
39                        println!("\nInterrupted. Type 'exit' or press Ctrl-D to quit.");
40                        continue;
41                    }
42                    _ => {
43                        eprintln!("\nError reading input: {}", e);
44                        break;
45                    }
46                }
47            }
48        }
49    }
50}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.