1
//! A crossterm backend whose size is the ssh pty's, not a local tty's.
2
//!
3
//! `CrosstermBackend::size` reads `/dev/tty`, stdout and finally runs
4
//! `tput`, none of which exist in the daemon's image; the client's window
5
//! size is the pty geometry the handler already tracks.
6

            
7
use crate::tui_channel::Geometry;
8
use ratatui::backend::{Backend, ClearType, CrosstermBackend, WindowSize};
9
use ratatui::buffer::Cell;
10
use ratatui::layout::{Position, Size};
11
use std::io::{self, Write};
12
use tokio::sync::watch;
13

            
14
pub struct PtyBackend<W: Write> {
15
    inner: CrosstermBackend<W>,
16
    geometry: watch::Receiver<Geometry>,
17
    cursor: Position,
18
}
19

            
20
impl<W: Write> PtyBackend<W> {
21
18
    pub fn new(writer: W, geometry: watch::Receiver<Geometry>) -> Self {
22
18
        Self {
23
18
            inner: CrosstermBackend::new(writer),
24
18
            geometry,
25
18
            cursor: Position::ORIGIN,
26
18
        }
27
18
    }
28
}
29

            
30
impl<W: Write> Backend for PtyBackend<W> {
31
    type Error = io::Error;
32

            
33
239
    fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
34
239
    where
35
239
        I: Iterator<Item = (u16, u16, &'a Cell)>,
36
    {
37
239
        self.inner.draw(content)
38
239
    }
39

            
40
    fn append_lines(&mut self, n: u16) -> io::Result<()> {
41
        self.inner.append_lines(n)
42
    }
43

            
44
239
    fn hide_cursor(&mut self) -> io::Result<()> {
45
239
        self.inner.hide_cursor()
46
239
    }
47

            
48
13
    fn show_cursor(&mut self) -> io::Result<()> {
49
13
        self.inner.show_cursor()
50
13
    }
51

            
52
    /// The last position set: asking crossterm would query the daemon's own tty.
53
2
    fn get_cursor_position(&mut self) -> io::Result<Position> {
54
2
        Ok(self.cursor)
55
2
    }
56

            
57
1
    fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> io::Result<()> {
58
1
        let position = position.into();
59
1
        self.inner.set_cursor_position(position)?;
60
1
        self.cursor = position;
61
1
        Ok(())
62
1
    }
63

            
64
13
    fn clear(&mut self) -> io::Result<()> {
65
13
        self.inner.clear()
66
13
    }
67

            
68
5
    fn clear_region(&mut self, clear_type: ClearType) -> io::Result<()> {
69
5
        self.inner.clear_region(clear_type)
70
5
    }
71

            
72
491
    fn size(&self) -> io::Result<Size> {
73
491
        let Geometry { cols, rows, .. } = *self.geometry.borrow();
74
491
        Ok(Size::new(cols, rows))
75
491
    }
76

            
77
2
    fn window_size(&mut self) -> io::Result<WindowSize> {
78
2
        let geometry = *self.geometry.borrow();
79
2
        let (width, height) = geometry.pixels();
80
2
        Ok(WindowSize {
81
2
            columns_rows: Size::new(geometry.cols, geometry.rows),
82
2
            pixels: Size::new(
83
2
                u16::try_from(width).unwrap_or(u16::MAX),
84
2
                u16::try_from(height).unwrap_or(u16::MAX),
85
2
            ),
86
2
        })
87
2
    }
88

            
89
236
    fn flush(&mut self) -> io::Result<()> {
90
236
        Backend::flush(&mut self.inner)
91
236
    }
92
}