pub trait Terminal {
// Required methods
fn read_byte(&mut self) -> Result<u8>;
fn write(&mut self, data: &[u8]) -> Result<()>;
fn flush(&mut self) -> Result<()>;
fn enter_raw_mode(&mut self) -> Result<()>;
fn exit_raw_mode(&mut self) -> Result<()>;
fn cursor_left(&mut self) -> Result<()>;
fn cursor_right(&mut self) -> Result<()>;
fn clear_eol(&mut self) -> Result<()>;
fn parse_key_event(&mut self) -> Result<KeyEvent>;
}Expand description
Terminal abstraction that enables platform-agnostic line editing.
Implement this trait to use editline with any I/O system: standard terminals, UART connections, network sockets, or custom devices.
§Platform Implementations
This library provides built-in implementations:
terminals::StdioTerminalfor Unix (termios + ANSI)terminals::StdioTerminalfor Windows (Console API)
§Example
use editline::{Terminal, KeyEvent, Result};
struct MockTerminal {
input: Vec<u8>,
output: Vec<u8>,
}
impl Terminal for MockTerminal {
fn read_byte(&mut self) -> Result<u8> {
self.input.pop().ok_or(editline::Error::Eof)
}
fn write(&mut self, data: &[u8]) -> Result<()> {
self.output.extend_from_slice(data);
Ok(())
}
// ... implement other methods
}Required Methods§
Sourcefn read_byte(&mut self) -> Result<u8>
fn read_byte(&mut self) -> Result<u8>
Reads a single byte from the input source.
This is called repeatedly to fetch user input. Should block until a byte is available.
Sourcefn write(&mut self, data: &[u8]) -> Result<()>
fn write(&mut self, data: &[u8]) -> Result<()>
Writes raw bytes to the output.
Used to display typed characters and redraw the line during editing.
Sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes any buffered output.
Called after each key event to ensure immediate visual feedback.
Sourcefn enter_raw_mode(&mut self) -> Result<()>
fn enter_raw_mode(&mut self) -> Result<()>
Enters raw mode for character-by-character input.
Should disable line buffering and echo. Called at the start of LineEditor::read_line.
Sourcefn exit_raw_mode(&mut self) -> Result<()>
fn exit_raw_mode(&mut self) -> Result<()>
Exits raw mode and restores normal terminal settings.
Called at the end of LineEditor::read_line to restore the terminal state.
Sourcefn cursor_left(&mut self) -> Result<()>
fn cursor_left(&mut self) -> Result<()>
Moves the cursor left by one position.
Typically outputs an ANSI escape sequence like \x1b[D or calls a platform API.
Sourcefn cursor_right(&mut self) -> Result<()>
fn cursor_right(&mut self) -> Result<()>
Moves the cursor right by one position.
Typically outputs an ANSI escape sequence like \x1b[C or calls a platform API.
Sourcefn clear_eol(&mut self) -> Result<()>
fn clear_eol(&mut self) -> Result<()>
Clears from the cursor position to the end of the line.
Typically outputs an ANSI escape sequence like \x1b[K or calls a platform API.
Sourcefn parse_key_event(&mut self) -> Result<KeyEvent>
fn parse_key_event(&mut self) -> Result<KeyEvent>
Parses the next key event from input.
Should handle multi-byte sequences (like ANSI escape codes) and return a single
KeyEvent. Called once per key press by LineEditor::read_line.