1 unstable release

0.1.0 Dec 27, 2024

#1041 in Algorithms

Download history 107/week @ 2024-12-21 30/week @ 2024-12-28

137 downloads per month

MIT license

36KB
730 lines

Virtual terminal state machine implementation.

This library provides the lowest-level handling of a virtual terminal stream, recognizing escape sequences and other control characters and delivering them to a caller-provided handler.

For example, given the sequence "\x1b[10;10H" this library can report that this is a control sequence with function character H and the parameters [10, 10], but it's up to the provided handler to interpret that as a command to move the cursor to row 10, column 10.

As with so many libraries like this, the state machine is based on the work of Paul Flo Williams in A parser for DEC’s ANSI-compatible video terminals, though any flaws are mine. This implementation does not aim to be fully compatible with VT100 or its successors. In particular, it implements a Unicode-native machine that does not support legacy character sets.


lib.rs:

Virtual terminal state machine implementation.

This library provides the lowest-level handling of a virtual terminal stream, recognizing escape sequences and other control characters and delivering them to a caller-provided handler.

For example, given the sequence "\x1b[10;10H" this library can report that this is a control sequence with function character H and the parameters [10, 10], but it's up to the provided handler to interpret that as a command to move the cursor to row 10, column 10.

As with so many libraries like this, the state machine is based on the work of Paul Flo Williams in A parser for DEC’s ANSI-compatible video terminals, though any flaws are mine. This implementation does not aim to be fully compatible with VT100 or its successors. In particular, it implements a Unicode-native machine that does not support legacy character sets.

The main entry point in this crate is VtMachine, which implements the state machine and delivers events to an implementation of trait VtHandler.

let mut machine = VtMachine::new(vt_handler_fn(|event| {
    println!("{event:?}");
}));
machine.write("\x1b[2J\x1b[1;1HHello!\r\n");
DispatchCsi { cmd: 'J', params: VtParams([2]), intermediates: VtIntermediates([]) }
DispatchCsi { cmd: 'H', params: VtParams([1, 1]), intermediates: VtIntermediates([]) }
Print('H')
Print('e')
Print('l')
Print('l')
Print('o')
Print('!')
ExecuteCtrl('\r')
ExecuteCtrl('\n')

No runtime deps