Lines
94.44 %
Functions
75 %
Branches
100 %
//! A crossterm backend whose size is the ssh pty's, not a local tty's.
//!
//! `CrosstermBackend::size` reads `/dev/tty`, stdout and finally runs
//! `tput`, none of which exist in the daemon's image; the client's window
//! size is the pty geometry the handler already tracks.
use crate::tui_channel::Geometry;
use ratatui::backend::{Backend, ClearType, CrosstermBackend, WindowSize};
use ratatui::buffer::Cell;
use ratatui::layout::{Position, Size};
use std::io::{self, Write};
use tokio::sync::watch;
pub struct PtyBackend<W: Write> {
inner: CrosstermBackend<W>,
geometry: watch::Receiver<Geometry>,
cursor: Position,
}
impl<W: Write> PtyBackend<W> {
pub fn new(writer: W, geometry: watch::Receiver<Geometry>) -> Self {
Self {
inner: CrosstermBackend::new(writer),
geometry,
cursor: Position::ORIGIN,
impl<W: Write> Backend for PtyBackend<W> {
type Error = io::Error;
fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
where
I: Iterator<Item = (u16, u16, &'a Cell)>,
{
self.inner.draw(content)
fn append_lines(&mut self, n: u16) -> io::Result<()> {
self.inner.append_lines(n)
fn hide_cursor(&mut self) -> io::Result<()> {
self.inner.hide_cursor()
fn show_cursor(&mut self) -> io::Result<()> {
self.inner.show_cursor()
/// The last position set: asking crossterm would query the daemon's own tty.
fn get_cursor_position(&mut self) -> io::Result<Position> {
Ok(self.cursor)
fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> io::Result<()> {
let position = position.into();
self.inner.set_cursor_position(position)?;
self.cursor = position;
Ok(())
fn clear(&mut self) -> io::Result<()> {
self.inner.clear()
fn clear_region(&mut self, clear_type: ClearType) -> io::Result<()> {
self.inner.clear_region(clear_type)
fn size(&self) -> io::Result<Size> {
let Geometry { cols, rows, .. } = *self.geometry.borrow();
Ok(Size::new(cols, rows))
fn window_size(&mut self) -> io::Result<WindowSize> {
let geometry = *self.geometry.borrow();
let (width, height) = geometry.pixels();
Ok(WindowSize {
columns_rows: Size::new(geometry.cols, geometry.rows),
pixels: Size::new(
u16::try_from(width).unwrap_or(u16::MAX),
u16::try_from(height).unwrap_or(u16::MAX),
),
})
fn flush(&mut self) -> io::Result<()> {
Backend::flush(&mut self.inner)