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 /// Returns whether plain printable output can bypass structured rendering
45 /// without losing parser or screen semantics.
46 #[must_use]
47 pub fn plain_output_forwarding_safe(&self) -> bool {
48 self.parser.plain_output_forwarding_safe()
49 }
50
51 /// Updates the tmux-style UTF-8 width and combining configuration.
52 pub fn set_utf8_config(&mut self, config: Utf8Config) {
53 self.parser.set_utf8_config(config);
54 }
55
56 /// Enables or disables DEC alternate-screen entry for subsequent output.
57 pub fn set_alternate_screen_enabled(&mut self, enabled: bool) {
58 self.parser.set_alternate_screen_enabled(enabled);
59 }
60
61 /// Enables or disables title changes requested by pane output.
62 pub fn set_title_rename_enabled(&mut self, enabled: bool) {
63 self.parser.set_title_rename_enabled(enabled);
64 }
65
66 /// Updates the tmux `input-buffer-size` parser limit.
67 pub fn set_input_buffer_limit(&mut self, limit: usize) {
68 self.parser.set_input_buffer_limit(limit);
69 }
70
71 /// Resizes the screen and resets the scroll region.
72 pub fn resize(&mut self, size: TerminalSize) {
73 self.parser.resize(size);
74 }
75
76 /// Feeds raw PTY output bytes through the private parser into the screen.
77 pub fn feed(&mut self, bytes: &[u8]) {
78 self.parser.feed(bytes);
79 }
80
81 /// Returns and drains terminal replies generated while parsing PTY output.
82 pub fn take_replies(&mut self) -> Vec<u8> {
83 self.parser.take_replies()
84 }
85
86 /// Returns any bytes still buffered inside an incomplete parser state.
87 #[must_use]
88 pub fn pending_bytes(&self) -> Vec<u8> {
89 self.parser.pending_bytes()
90 }
91
92 /// Returns whether the parser ground timeout is currently armed.
93 #[must_use]
94 pub fn ground_timer_active(&self) -> bool {
95 self.parser.ground_timer_active()
96 }
97
98 /// Notifies the parser that its ground timeout has expired.
99 pub fn ground_timer_expired(&mut self) {
100 self.parser.ground_timer_expired();
101 }
102
103 /// Returns and drains passthrough events generated while parsing PTY output.
104 pub fn take_terminal_passthrough(&mut self) -> Vec<TerminalPassthrough> {
105 self.parser.take_terminal_passthrough()
106 }
107
108 /// Returns and drains passthrough events dropped by parser safety limits.
109 pub fn take_terminal_passthrough_dropped_count(&mut self) -> u64 {
110 self.parser.take_terminal_passthrough_dropped_count()
111 }
112
113 /// Replaces the hidden parser with a fresh ground-state instance while
114 /// preserving the current screen grid.
115 pub fn reset_parser(&mut self) {
116 self.parser.reset_parser();
117 }
118}