Skip to main content

agentmux_srv/backend/blockcontroller/
term_size.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The size each block's PTY was last given: at spawn (its runtime options)
5//! and on every resize. Kept so a reader that doesn't drive the terminal (a
6//! paired device watching it) can render at the desktop's width; the PTY
7//! itself keeps no record srv can ask.
8
9use std::collections::HashMap;
10use std::sync::{LazyLock, Mutex};
11
12static SIZES: LazyLock<Mutex<HashMap<String, (u16, u16)>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
13
14/// Record `block_id`'s PTY size as `(rows, cols)`.
15pub fn record(block_id: &str, rows: u16, cols: u16) {
16    SIZES.lock().unwrap_or_else(|p| p.into_inner()).insert(block_id.to_string(), (rows, cols));
17}
18
19/// `block_id`'s PTY size as `(rows, cols)`, if it has had one since srv started.
20pub fn get(block_id: &str) -> Option<(u16, u16)> {
21    SIZES.lock().unwrap_or_else(|p| p.into_inner()).get(block_id).copied()
22}
23
24/// Forget `block_id` (its controller is gone).
25pub fn forget(block_id: &str) {
26    SIZES.lock().unwrap_or_else(|p| p.into_inner()).remove(block_id);
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn the_last_size_wins_until_forgotten() {
35        let block = uuid::Uuid::new_v4().to_string();
36        assert_eq!(get(&block), None);
37        record(&block, 25, 200);
38        record(&block, 40, 120);
39        assert_eq!(get(&block), Some((40, 120)));
40        forget(&block);
41        assert_eq!(get(&block), None);
42    }
43}