// sandhole: Expose HTTP/SSH/TCP services through SSH port forwarding
// Copyright (C) 2024-2026 Eric Rodrigues Pires
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License along
// with this program. If not, see .
use std::io::Write;
use ratatui::{
backend::{Backend, ClearType, CrosstermBackend, WindowSize},
buffer::Cell,
layout::{Position, Size},
};
pub(crate) struct PtyBackend {
inner: CrosstermBackend,
size: Size,
}
impl PtyBackend {
pub(crate) fn new(writer: W, size: Size) -> Self {
Self {
inner: CrosstermBackend::new(writer),
size,
}
}
pub(crate) fn set_size(&mut self, size: Size) {
self.size = size;
}
}
impl Backend for PtyBackend {
type Error = std::io::Error;
fn draw<'a, I>(&mut self, content: I) -> std::io::Result<()>
where
I: Iterator- ,
{
self.inner.draw(content)
}
fn hide_cursor(&mut self) -> std::io::Result<()> {
self.inner.hide_cursor()
}
fn show_cursor(&mut self) -> std::io::Result<()> {
self.inner.show_cursor()
}
fn get_cursor_position(&mut self) -> std::io::Result {
self.inner.get_cursor_position()
}
fn set_cursor_position>(&mut self, position: P) -> std::io::Result<()> {
self.inner.set_cursor_position(position)
}
fn clear(&mut self) -> std::io::Result<()> {
self.inner.clear()
}
fn clear_region(&mut self, clear_type: ClearType) -> std::io::Result<()> {
self.inner.clear_region(clear_type)
}
fn size(&self) -> std::io::Result {
Ok(self.size)
}
fn window_size(&mut self) -> std::io::Result {
self.inner.window_size()
}
fn flush(&mut self) -> std::io::Result<()> {
as Backend>::flush(&mut self.inner)
}
}
impl Write for PtyBackend {
fn write(&mut self, buf: &[u8]) -> std::io::Result {
self.inner.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
as Write>::flush(&mut self.inner)
}
}