Skip to main content

Crate termina

Crate termina 

Source
Expand description

Terminal I/O, escape-sequence types, styling, and input parsing.

Termina keeps the terminal protocol visible. Applications write typed CSI, OSC, and DCS values from escape instead of assembling byte strings, and read typed Event values instead of decoding terminal input by hand. PlatformTerminal opens the current process terminal, switches raw/cooked mode, writes bytes, and creates an EventReader for synchronous input.

Code that already has terminal bytes can use Parser directly. That is useful for PTY tests, terminal multiplexers, or callers that own the input source and only need Termina’s parser.

§Examples

use std::io::{self, Write};

use termina::{
    event::{KeyCode, KeyEventKind},
    Event, PlatformTerminal, Terminal,
};

fn main() -> io::Result<()> {
    let mut terminal = PlatformTerminal::new()?;
    terminal.enter_raw_mode()?;
    writeln!(terminal, "Press q to exit.")?;

    let reader = terminal.event_reader();
    loop {
        let event = reader.read(|_| true)?;
        if matches!(
            event,
            Event::Key(key)
                if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q')
        ) {
            break;
        }
    }

    terminal.enter_cooked_mode()
}

Parsing PTY bytes directly does not require opening a terminal handle:

use termina::{Event, Parser};

let mut parser = Parser::default();
parser.parse(b"\x1b[5~", false);
assert!(matches!(parser.pop(), Some(Event::Key(_))));

Re-exports§

pub use event::Event;

Modules§

escape
Typed ANSI escape-sequence helpers.
event
Terminal input events.
style
Types for styling terminal cells.

Structs§

EventReader
A reader of events from the terminal’s input handle.
OneBased
A one-based terminal coordinate or dimension.
Parser
An incremental parser for terminal input.
WindowSize
The dimensions of a terminal window.

Traits§

Terminal
Platform-agnostic terminal I/O surface.

Type Aliases§

PlatformHandle
The output handle type passed to panic hooks on the current platform.
PlatformTerminal
The terminal implementation for the current platform.