Crate editline

Crate editline 

Source
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. 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
  • Sync and Async: Both blocking and async APIs available via feature flags
  • Zero global state: All state is explicitly managed
  • Type-safe: Strong typing with Result-based error handling

§Quick Start (Sync)

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;
        }
    }
}

§Quick Start (Async)

use editline::{AsyncLineEditor, terminals::EmbassyUsbTerminal};

let mut editor = AsyncLineEditor::new(1024, 50);
let mut terminal = EmbassyUsbTerminal::new(usb_class);

loop {
    let _ = terminal.write(b"> ").await;
    let _ = terminal.flush().await;

    match editor.read_line(&mut terminal).await {
        Ok(line) => {
            if line == "exit" {
                break;
            }
            defmt::info!("You typed: {}", line);
        }
        Err(e) => {
            defmt::error!("Error: {:?}", e);
            break;
        }
    }
}

§Architecture

The library is organized around several components:

  • Shared Components (work with both sync and async):

  • Sync API (feature = “sync”, default):

  • Async API (feature = “async”):

    • [AsyncTerminal]: Async I/O trait
    • [AsyncLineEditor]: Async line editor

Modules§

terminals
Platform-specific terminal implementations.

Structs§

History
Command history manager with circular buffer storage.
LineBuffer
Text buffer with cursor tracking for line editing operations.
LineEditor
Main line editor interface with full editing and history support.

Enums§

Error
Error type for editline operations
KeyEvent
Key events that can be processed by the line editor.

Traits§

Terminal
Terminal abstraction that enables platform-agnostic line editing.

Type Aliases§

Result
Result type for editline operations