Skip to main content

hyprstream_tui/
vt_pane.rs

1//! Generic VT pane — avt virtual terminal + channel-based I/O.
2//!
3//! Extracted from `PaneWindow` to decouple VTE parsing from PTY lifecycle.
4//! Used by both `PaneWindow` (PTY shells) and `ContainerTermApp` (container attach).
5
6#![cfg(not(target_os = "wasi"))]
7
8use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
9
10use avt::Vt;
11
12/// A virtual terminal backed by byte channels.
13///
14/// Receives stdout bytes via `stdout_rx`, feeds them into the avt VT parser.
15/// Optionally sends stdin bytes via `stdin_tx` (None for read-only streams).
16pub struct VtPane {
17    pub vt: Vt,
18    pub stdout_rx: Receiver<Vec<u8>>,
19    pub stdin_tx: Option<SyncSender<Vec<u8>>>,
20}
21
22impl VtPane {
23    /// Create a new VT pane with the given dimensions and I/O channels.
24    pub fn new(
25        cols: u16,
26        rows: u16,
27        stdout_rx: Receiver<Vec<u8>>,
28        stdin_tx: Option<SyncSender<Vec<u8>>>,
29    ) -> Self {
30        let vt = Vt::builder().size(cols as usize, rows as usize).build();
31        Self {
32            vt,
33            stdout_rx,
34            stdin_tx,
35        }
36    }
37
38    /// Drain stdout_rx into the VTE parser.
39    ///
40    /// Returns `(got_data, is_dead)`:
41    /// - `got_data` — true if any bytes arrived (caller should redraw)
42    /// - `is_dead`  — true if the channel disconnected (source exited)
43    pub fn drain(&mut self) -> (bool, bool) {
44        let mut got = false;
45        loop {
46            match self.stdout_rx.try_recv() {
47                Ok(data) => {
48                    let s = String::from_utf8_lossy(&data);
49                    self.vt.feed_str(&s);
50                    got = true;
51                }
52                Err(TryRecvError::Empty) => return (got, false),
53                Err(TryRecvError::Disconnected) => return (got, true),
54            }
55        }
56    }
57
58    /// Send raw bytes to stdin (if a sender is configured).
59    pub fn send_bytes(&self, data: Vec<u8>) {
60        if let Some(ref tx) = self.stdin_tx {
61            let _ = tx.send(data);
62        }
63    }
64
65    /// Resize the VT to new dimensions (protocol-agnostic, no escape sequences).
66    pub fn resize_vt(&mut self, cols: u16, rows: u16) {
67        self.vt = Vt::builder().size(cols as usize, rows as usize).build();
68    }
69}