Skip to main content

rmux_core/
terminal_screen.rs

1//! Public live-terminal screen facade.
2//!
3//! This module exposes the server-facing screen wrapper while keeping the
4//! parser implementation inside the crate-private `terminal` module.
5
6use rmux_proto::TerminalSize;
7
8use crate::screen::Screen;
9use crate::terminal::TerminalParser;
10use crate::terminal_passthrough::TerminalPassthrough;
11use crate::utf8::Utf8Config;
12
13/// Live terminal screen fed by rmux-core's private parser boundary.
14///
15/// `TerminalScreen` is the public core facade that server code uses to feed
16/// raw PTY bytes and inspect structured screen cells. The parser itself stays
17/// hidden behind the crate-private terminal module, so SDK/protocol code can
18/// depend on screen-cell semantics without coupling to parser internals.
19pub struct TerminalScreen {
20    parser: TerminalParser,
21}
22
23impl TerminalScreen {
24    /// Builds a fresh terminal screen with the given geometry and scrollback
25    /// limit.
26    #[must_use]
27    pub fn new(size: TerminalSize, history_limit: usize) -> Self {
28        Self {
29            parser: TerminalParser::new(size, history_limit),
30        }
31    }
32
33    /// Returns a borrow of the structured screen grid.
34    #[must_use]
35    pub fn screen(&self) -> &Screen {
36        self.parser.screen()
37    }
38
39    /// Returns a mutable borrow of the structured screen grid.
40    pub fn screen_mut(&mut self) -> &mut Screen {
41        self.parser.screen_mut()
42    }
43
44    /// Updates the tmux-style UTF-8 width and combining configuration.
45    pub fn set_utf8_config(&mut self, config: Utf8Config) {
46        self.parser.set_utf8_config(config);
47    }
48
49    /// Updates the tmux `input-buffer-size` parser limit.
50    pub fn set_input_buffer_limit(&mut self, limit: usize) {
51        self.parser.set_input_buffer_limit(limit);
52    }
53
54    /// Resizes the screen and resets the scroll region.
55    pub fn resize(&mut self, size: TerminalSize) {
56        self.parser.resize(size);
57    }
58
59    /// Feeds raw PTY output bytes through the private parser into the screen.
60    pub fn feed(&mut self, bytes: &[u8]) {
61        self.parser.feed(bytes);
62    }
63
64    /// Returns and drains terminal replies generated while parsing PTY output.
65    pub fn take_replies(&mut self) -> Vec<u8> {
66        self.parser.take_replies()
67    }
68
69    /// Returns any bytes still buffered inside an incomplete parser state.
70    #[must_use]
71    pub fn pending_bytes(&self) -> Vec<u8> {
72        self.parser.pending_bytes()
73    }
74
75    /// Returns and drains passthrough events generated while parsing PTY output.
76    pub fn take_terminal_passthrough(&mut self) -> Vec<TerminalPassthrough> {
77        self.parser.take_terminal_passthrough()
78    }
79
80    /// Returns and drains passthrough events dropped by parser safety limits.
81    pub fn take_terminal_passthrough_dropped_count(&mut self) -> u64 {
82        self.parser.take_terminal_passthrough_dropped_count()
83    }
84
85    /// Replaces the hidden parser with a fresh ground-state instance while
86    /// preserving the current screen grid.
87    pub fn reset_parser(&mut self) {
88        self.parser.reset_parser();
89    }
90}