Skip to main content

term_wm_pty_engine/
pane.rs

1use std::io;
2use std::sync::{Arc, Mutex};
3use std::thread::JoinHandle;
4
5use portable_pty::{Child, ExitStatus, PtySize};
6
7use crate::{PtyResult, PtyStatus};
8
9pub trait Pane {
10    fn resize(&mut self, size: PtySize) -> PtyResult<()>;
11    fn has_exited(&mut self) -> bool;
12    fn alternate_screen(&mut self) -> bool;
13    fn scrollback(&mut self) -> usize;
14    fn set_scrollback(&mut self, rows: usize);
15    fn write_bytes(&mut self, input: &[u8]) -> io::Result<()>;
16    fn max_scrollback(&mut self) -> usize;
17    fn scrollback_len(&self) -> usize;
18    fn take_exit_status(&mut self) -> Option<ExitStatus>;
19    fn exit_status(&self) -> Option<ExitStatus>;
20    fn bytes_received(&self) -> usize;
21    fn last_bytes_text(&self) -> String;
22    fn kill_child(&mut self) -> PtyResult<()>;
23    /// Set a status callback invoked on PTY data or exit.
24    fn set_status_callback(&mut self, _cb: Option<Box<dyn Fn(PtyStatus) + Send + Sync>>) {}
25    fn take_pending_title(&mut self) -> Option<String> {
26        None
27    }
28    /// Extract the child process and reader thread handle so they can be
29    /// moved into the `Reaper` for async teardown.
30    /// Returns `None` by default (for mock panes). The real `Pty` impl
31    /// returns `(child, reader_handle)`.
32    fn take_parts(&mut self) -> Option<(Box<dyn Child + Send + Sync>, JoinHandle<()>)> {
33        None
34    }
35    /// Access the shared parser for zero-copy rendering.
36    fn shared_parser(&mut self) -> Arc<Mutex<vt100::Parser>> {
37        Arc::new(Mutex::new(vt100::Parser::new(24, 80, 0)))
38    }
39    /// Reset the dirty flag. Returns true if dirty was set.
40    fn take_dirty(&self) -> bool {
41        false
42    }
43    /// Clear the dirty flag and notify the reader thread via Condvar.
44    /// This is the primary mechanism for I/O burst budget backpressure.
45    fn clear_dirty_and_notify(&self) {}
46    /// Sync dirty state and handle DSR/foreground polling.
47    /// Call before locking the parser for cell access.
48    fn sync_screen(&mut self) {}
49
50    /// Unified routing decision: returns true if keyboard/mouse inputs
51    /// should be forwarded to the PTY child rather than intercepted
52    /// by the window manager's native scrollbar.
53    /// Thread-safe: no mutable borrow needed (atomic reads).
54    fn requires_app_routing(&self) -> bool {
55        false
56    }
57
58    /// Returns true when DECCKM (DECSET 1 / Application Cursor Keys) is active.
59    /// When true, unmodified arrow keys must use SS3 (`\eOA`) instead of CSI (`\e[A`).
60    fn is_application_cursor_keys_active(&self) -> bool {
61        false
62    }
63}
64
65impl Pane for crate::Pty {
66    fn resize(&mut self, size: PtySize) -> PtyResult<()> {
67        self.resize(size)
68    }
69
70    fn has_exited(&mut self) -> bool {
71        self.has_exited()
72    }
73
74    fn alternate_screen(&mut self) -> bool {
75        self.alternate_screen()
76    }
77
78    fn scrollback(&mut self) -> usize {
79        self.scrollback()
80    }
81
82    fn set_scrollback(&mut self, rows: usize) {
83        self.set_scrollback(rows);
84    }
85
86    fn write_bytes(&mut self, input: &[u8]) -> io::Result<()> {
87        self.write_bytes(input)
88    }
89
90    fn sync_screen(&mut self) {
91        crate::Pty::screen(self);
92    }
93
94    fn max_scrollback(&mut self) -> usize {
95        self.max_scrollback()
96    }
97
98    fn scrollback_len(&self) -> usize {
99        self.scrollback_len()
100    }
101
102    fn take_exit_status(&mut self) -> Option<ExitStatus> {
103        self.take_exit_status()
104    }
105
106    fn exit_status(&self) -> Option<ExitStatus> {
107        self.exit_status()
108    }
109
110    fn bytes_received(&self) -> usize {
111        self.bytes_received()
112    }
113
114    fn last_bytes_text(&self) -> String {
115        self.last_bytes_text()
116    }
117
118    fn kill_child(&mut self) -> PtyResult<()> {
119        self.kill_child()
120    }
121
122    fn take_pending_title(&mut self) -> Option<String> {
123        crate::Pty::take_pending_title(self)
124    }
125
126    fn take_parts(&mut self) -> Option<(Box<dyn Child + Send + Sync>, JoinHandle<()>)> {
127        let parts = self.into_parts();
128        match (parts.child, parts.reader_handle) {
129            (Some(child), Some(handle)) => Some((child, handle)),
130            _ => None,
131        }
132    }
133
134    fn set_status_callback(&mut self, cb: Option<Box<dyn Fn(PtyStatus) + Send + Sync>>) {
135        crate::Pty::set_status_callback(self, cb)
136    }
137
138    fn requires_app_routing(&self) -> bool {
139        self.tracker.requires_app_routing()
140    }
141
142    fn is_application_cursor_keys_active(&self) -> bool {
143        self.tracker.is_application_cursor_keys_active()
144    }
145
146    fn shared_parser(&mut self) -> Arc<Mutex<vt100::Parser>> {
147        self.shared_parser.clone()
148    }
149
150    fn take_dirty(&self) -> bool {
151        self.dirty.swap(false, std::sync::atomic::Ordering::AcqRel)
152    }
153
154    fn clear_dirty_and_notify(&self) {
155        let (lock, cvar) = &*self.dirty_cond;
156        let _guard = lock.lock().unwrap();
157        self.dirty
158            .store(false, std::sync::atomic::Ordering::Release);
159        cvar.notify_one();
160    }
161}