1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! Live PTY sessions embedded in slides.
//!
//! Each `TermBlock` in the deck gets at most one `TermSession`: a child
//! process on a PTY, with output fed into a vt100 emulator that the
//! presenter UI renders via `tui-term`.
use std::io::{Read, Write};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use anyhow::Result;
use portable_pty::{Child, CommandBuilder, MasterPty, PtySize, native_pty_system};
/// Lines of history kept per terminal for scrollback viewing.
const SCROLLBACK_LINES: usize = 10_000;
/// One live PTY session: a child process, its master side, and a vt100
/// emulator fed by a background reader thread.
pub struct TermSession {
/// The terminal emulator; lock it and render `screen()` to display.
pub parser: Arc<Mutex<vt100::Parser>>,
/// Set by the reader thread when the child's output stream closes.
pub exited: Arc<AtomicBool>,
writer: Box<dyn Write + Send>,
master: Box<dyn MasterPty>,
child: Box<dyn Child + Send + Sync>,
reaped: bool,
rows: u16,
cols: u16,
}
impl TermSession {
/// Spawn `command` via `$SHELL -c` (or an interactive `$SHELL` when
/// `None`) on a fresh PTY of the given size, in `cwd`.
pub fn spawn(command: Option<&str>, rows: u16, cols: u16, cwd: &Path) -> Result<Self> {
let pty = native_pty_system();
let pair = pty.openpty(PtySize {
rows,
cols,
pixel_width: 0,
pixel_height: 0,
})?;
let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
let mut cmd = match command {
Some(script) => {
let mut c = CommandBuilder::new(&shell);
c.arg("-c");
c.arg(script);
c
}
None => CommandBuilder::new(&shell),
};
cmd.cwd(cwd);
cmd.env("TERM", "xterm-256color");
let child = pair.slave.spawn_command(cmd)?;
drop(pair.slave);
let writer = pair.master.take_writer()?;
let mut reader = pair.master.try_clone_reader()?;
let parser = Arc::new(Mutex::new(vt100::Parser::new(rows, cols, SCROLLBACK_LINES)));
let exited = Arc::new(AtomicBool::new(false));
{
let parser = Arc::clone(&parser);
let exited = Arc::clone(&exited);
std::thread::spawn(move || {
let mut buf = [0u8; 8192];
loop {
match reader.read(&mut buf) {
Ok(0) | Err(_) => {
exited.store(true, Ordering::SeqCst);
break;
}
Ok(n) => parser
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.process(&buf[..n]),
}
}
});
}
Ok(TermSession {
parser,
exited,
writer,
master: pair.master,
child,
reaped: false,
rows,
cols,
})
}
/// Whether the child's output stream has closed.
pub fn exited(&self) -> bool {
self.exited.load(Ordering::SeqCst)
}
/// Lock the emulator, shrugging off poison: the reader thread holds
/// no invariants worth killing a presentation over.
pub fn parser_lock(&self) -> std::sync::MutexGuard<'_, vt100::Parser> {
self.parser
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Send bytes to the child's stdin; also snaps scrollback to live.
pub fn write_input(&mut self, bytes: &[u8]) {
// Typing jumps back to the live view, like a normal terminal.
{
let mut p = self.parser_lock();
if p.screen().scrollback() > 0 {
p.set_scrollback(0);
}
}
let _ = self.writer.write_all(bytes);
let _ = self.writer.flush();
}
/// Move the scrollback view; positive = further into history.
pub fn scroll_lines(&mut self, delta: isize) {
let mut p = self.parser_lock();
let cur = p.screen().scrollback() as isize;
let new = (cur + delta).max(0) as usize;
p.set_scrollback(new);
}
/// Route a wheel tick at cell (col, row): into the child when it
/// wants it (mouse reporting, or alternate-screen arrow keys),
/// otherwise through the local history view.
pub fn wheel(&mut self, up: bool, col: u16, row: u16) {
let to_child = {
let p = self.parser_lock();
wheel_bytes(p.screen(), up, col, row)
};
match to_child {
Some(bytes) => self.write_input(&bytes),
None => self.scroll_lines(if up { 3 } else { -3 }),
}
}
/// Forward a left click at (col, row) when the child reports mouse;
/// returns whether it was sent (false keeps the caller's own click
/// semantics).
pub fn click(&mut self, col: u16, row: u16) -> bool {
let report = {
let p = self.parser_lock();
click_bytes(p.screen(), col, row)
};
match report {
Some(bytes) => {
self.write_input(&bytes);
true
}
None => false,
}
}
/// Scroll the view by half the viewport height.
pub fn scroll_page(&mut self, up: bool) {
let half = (self.rows / 2).max(1) as isize;
self.scroll_lines(if up { half } else { -half });
}
/// How far into history the view currently is (0 = live).
pub fn scroll_offset(&self) -> usize {
self.parser_lock().screen().scrollback()
}
/// (current offset, total history lines available). vt100 doesn't
/// expose the used scrollback length, but `set_scrollback` clamps to
/// it — so probe with a huge value and restore.
pub fn scroll_info(&self) -> (usize, usize) {
let mut p = self.parser_lock();
let cur = p.screen().scrollback();
p.set_scrollback(usize::MAX);
let avail = p.screen().scrollback();
p.set_scrollback(cur);
(cur, avail)
}
/// Resize the PTY and emulator together; no-op if unchanged.
pub fn resize(&mut self, rows: u16, cols: u16) {
if rows == self.rows && cols == self.cols {
return;
}
self.rows = rows;
self.cols = cols;
let _ = self.master.resize(PtySize {
rows,
cols,
pixel_width: 0,
pixel_height: 0,
});
self.parser_lock().set_size(rows, cols);
}
/// Reap the child once its output stream has closed, so commands
/// that finish on their own don't linger as zombies for the rest of
/// the presentation. No-op while the child is running (or once
/// reaped); cheap enough to call every frame.
pub fn reap(&mut self) {
if self.reaped || !self.exited() {
return;
}
if let Ok(Some(_)) = self.child.try_wait() {
self.reaped = true;
}
}
/// Kill the child process and reap it.
pub fn kill(&mut self) {
// Once reaped the pid may be reused — signaling it could hit an
// unrelated process.
if self.reaped {
return;
}
let _ = self.child.kill();
let _ = self.child.wait();
self.reaped = true;
}
}
/// What a wheel tick should send to the child, per its screen state:
/// mouse-reporting programs get a wheel report, alternate-screen
/// programs without mouse reporting get arrow keys (xterm's
/// "alternate scroll" convention — how `less` and `vim` scroll under a
/// wheel), and `None` means the child doesn't care — the caller moves
/// the local history view instead.
fn wheel_bytes(screen: &vt100::Screen, up: bool, col: u16, row: u16) -> Option<Vec<u8>> {
if screen.mouse_protocol_mode() != vt100::MouseProtocolMode::None {
return Some(mouse_report(
screen,
if up { 64 } else { 65 },
col,
row,
true,
));
}
if screen.alternate_screen() {
let arrow: &[u8] = match (screen.application_cursor(), up) {
(true, true) => b"\x1bOA",
(true, false) => b"\x1bOB",
(false, true) => b"\x1b[A",
(false, false) => b"\x1b[B",
};
return Some(arrow.repeat(3));
}
None
}
/// A left press+release pair, if the child asked for mouse reports.
fn click_bytes(screen: &vt100::Screen, col: u16, row: u16) -> Option<Vec<u8>> {
if screen.mouse_protocol_mode() == vt100::MouseProtocolMode::None {
return None;
}
let mut bytes = mouse_report(screen, 0, col, row, true);
bytes.extend(mouse_report(screen, 0, col, row, false));
Some(bytes)
}
/// One mouse report in the child's negotiated encoding (1-based cells).
fn mouse_report(screen: &vt100::Screen, button: u8, col: u16, row: u16, press: bool) -> Vec<u8> {
let (x, y) = (u32::from(col) + 1, u32::from(row) + 1);
if screen.mouse_protocol_encoding() == vt100::MouseProtocolEncoding::Sgr {
let suffix = if press { 'M' } else { 'm' };
return format!("\x1b[<{button};{x};{y}{suffix}").into_bytes();
}
// Legacy X10 bytes (utf8 mode only diverges past column 95, where
// real programs negotiate SGR anyway). Release is button 3;
// coordinates saturate at the encodable maximum.
let b = if press { button } else { 3 };
vec![
0x1b,
b'[',
b'M',
32 + b,
(32 + x.min(223)) as u8,
(32 + y.min(223)) as u8,
]
}
#[cfg(test)]
mod tests {
use super::*;
fn screen_after(setup: &[u8]) -> vt100::Parser {
let mut p = vt100::Parser::new(24, 80, 0);
p.process(setup);
p
}
#[test]
fn wheel_scrolls_history_on_plain_screens() {
let p = screen_after(b"plain output\r\n");
assert_eq!(wheel_bytes(p.screen(), true, 5, 5), None);
}
#[test]
fn wheel_sends_arrows_to_alternate_screen_tuis() {
let p = screen_after(b"\x1b[?1049h");
assert_eq!(
wheel_bytes(p.screen(), true, 0, 0).unwrap(),
b"\x1b[A\x1b[A\x1b[A"
);
// Application cursor keys (vim, htop) get SS3 arrows.
let p = screen_after(b"\x1b[?1049h\x1b[?1h");
assert_eq!(
wheel_bytes(p.screen(), false, 0, 0).unwrap(),
b"\x1bOB\x1bOB\x1bOB"
);
}
#[test]
fn wheel_reports_to_mouse_aware_programs() {
let p = screen_after(b"\x1b[?1000h\x1b[?1006h");
assert_eq!(
wheel_bytes(p.screen(), true, 2, 4).unwrap(),
b"\x1b[<64;3;5M"
);
let p = screen_after(b"\x1b[?1000h");
assert_eq!(
wheel_bytes(p.screen(), false, 2, 4).unwrap(),
vec![0x1b, b'[', b'M', 32 + 65, 32 + 3, 32 + 5]
);
}
#[test]
fn clicks_forward_only_when_reported() {
let p = screen_after(b"");
assert_eq!(click_bytes(p.screen(), 1, 1), None);
let p = screen_after(b"\x1b[?1000h\x1b[?1006h");
assert_eq!(
click_bytes(p.screen(), 1, 1).unwrap(),
b"\x1b[<0;2;2M\x1b[<0;2;2m"
);
}
}