Expand description
Platform-agnostic line editor with history and full editing capabilities.
This library provides a flexible line editing system with complete separation between
I/O operations and editing logic through the Terminal
trait. This design enables
usage across various platforms and I/O systems without modification to the core logic.
§Features
- Full line editing: Insert, delete, cursor movement
- Word-aware navigation: Ctrl+Left/Right, Alt+Backspace, Ctrl+Delete
- Command history: 50-entry circular buffer with up/down navigation
- Smart history: Automatically skips duplicates and empty lines
- Cross-platform: Unix (termios/ANSI) and Windows (Console API) implementations included
- Zero global state: All state is explicitly managed
- Type-safe: Strong typing with Result-based error handling
§Quick Start
use editline::{LineEditor, terminals::StdioTerminal};
let mut editor = LineEditor::new(1024, 50); // buffer size, history size
let mut terminal = StdioTerminal::new();
loop {
print!("> ");
std::io::Write::flush(&mut std::io::stdout()).unwrap();
match editor.read_line(&mut terminal) {
Ok(line) => {
if line == "exit" {
break;
}
println!("You typed: {}", line);
}
Err(e) => {
eprintln!("Error: {}", e);
break;
}
}
}
§Architecture
The library is organized around three main components:
LineEditor
: Main API that orchestrates editing operationsLineBuffer
: Manages text buffer and cursor positionHistory
: Circular buffer for command history
All I/O is abstracted through the Terminal
trait, which platform-specific
implementations must provide.
§Custom Terminal Implementation
To use editline with custom I/O (UART, network, etc.), implement the Terminal
trait:
use editline::{Terminal, KeyEvent, Result};
struct MyTerminal {
// Your platform-specific fields
}
impl Terminal for MyTerminal {
fn read_byte(&mut self) -> Result<u8> {
// Read from your input source
}
fn write(&mut self, data: &[u8]) -> Result<()> {
// Write to your output
}
fn flush(&mut self) -> Result<()> {
// Flush output
}
fn enter_raw_mode(&mut self) -> Result<()> {
// Configure for character-by-character input
}
fn exit_raw_mode(&mut self) -> Result<()> {
// Restore normal mode
}
fn cursor_left(&mut self) -> Result<()> {
// Move cursor left one position
}
fn cursor_right(&mut self) -> Result<()> {
// Move cursor right one position
}
fn clear_eol(&mut self) -> Result<()> {
// Clear from cursor to end of line
}
fn parse_key_event(&mut self) -> Result<KeyEvent> {
// Parse input bytes into key events
}
}
Modules§
- terminals
- Platform-specific terminal implementations.
Structs§
- History
- Command history manager with circular buffer storage.
- Line
Buffer - Text buffer with cursor tracking for line editing operations.
- Line
Editor - Main line editor interface with full editing and history support.
Enums§
Traits§
- Terminal
- Terminal abstraction that enables platform-agnostic line editing.
Type Aliases§
- Result
- Result type for editline operations