#[cfg(target_os = "linux")]
mod dev_tty;
mod keys;
mod stdio;
use std::sync::atomic::{AtomicBool, Ordering};
pub use keys::{parse_key, Input, KeyEvent};
use crate::frame::{self, Chrome, Screen, TermSize};
use crate::render;
use crate::theme::Theme;
static INTERRUPTED: AtomicBool = AtomicBool::new(false);
pub fn take_interrupt() -> bool {
INTERRUPTED.swap(false, Ordering::SeqCst)
}
pub fn probe_size() -> TermSize {
match ratatui::crossterm::terminal::size() {
Ok((c, r)) => TermSize {
cols: usize::from(c.max(1)),
rows: usize::from(r.max(1)),
},
Err(_) => TermSize { cols: 80, rows: 24 },
}
}
enum Backend {
#[cfg(target_os = "linux")]
DevTty(dev_tty::DevTty),
Stdio(stdio::Stdio),
}
pub struct Tty {
backend: Backend,
chrome: Chrome,
last_size: Option<TermSize>,
}
impl Tty {
fn with(backend: Backend) -> Tty {
Tty {
backend,
chrome: Chrome::default(),
last_size: None,
}
}
#[cfg(target_os = "linux")]
pub fn open_controlling() -> std::io::Result<Option<Tty>> {
Ok(dev_tty::DevTty::open().map(|b| Tty::with(Backend::DevTty(b))))
}
#[cfg(not(target_os = "linux"))]
pub fn open_controlling() -> std::io::Result<Option<Tty>> {
Ok(None)
}
#[cfg(target_os = "linux")]
pub fn is_available_controlling() -> bool {
dev_tty::DevTty::is_available()
}
#[cfg(not(target_os = "linux"))]
pub fn is_available_controlling() -> bool {
false
}
pub fn open_stdio() -> std::io::Result<Option<Tty>> {
Ok(stdio::Stdio::open()?.map(|b| Tty::with(Backend::Stdio(b))))
}
pub fn is_available_stdio() -> bool {
stdio::Stdio::is_available()
}
pub fn size(&self) -> TermSize {
probe_size()
}
pub fn body_width(&self) -> usize {
render::panel_body_width(frame::panel_width(probe_size().cols))
}
pub fn set_chrome(&mut self, step: usize, total: usize, label: &str) {
self.chrome = Chrome {
step,
total,
label: label.to_string(),
};
}
pub fn render(&mut self, theme: &Theme, section: &str, body: &[String], footer: &str) {
let size = probe_size();
let cleared = self.last_size != Some(size);
let screen = Screen {
section,
body,
footer,
};
let grid = frame::compose(theme, &self.chrome, &screen, size);
self.write(&frame::to_ansi(&grid, cleared, theme));
self.last_size = Some(size);
}
pub fn present(&mut self, grid: &[String], theme: &Theme) {
let size = probe_size();
let cleared = self.last_size != Some(size);
self.write(&frame::to_ansi(grid, cleared, theme));
self.last_size = Some(size);
}
pub fn force_clear_next(&mut self) {
self.last_size = None;
}
pub fn flush_input(&mut self) {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(t) => t.flush_input(),
Backend::Stdio(s) => s.flush_input(),
}
}
pub fn enable_signals(&mut self) {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(t) => t.enable_signals(),
Backend::Stdio(_) => {}
}
}
pub fn poll_interrupt(&mut self) {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(_) => {}
Backend::Stdio(s) => s.poll_interrupt(),
}
}
pub fn read_key(&mut self) -> std::io::Result<KeyEvent> {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(t) => t.read_key(),
Backend::Stdio(s) => s.read_key(),
}
}
pub fn read_input(&mut self, ms: u64) -> Input {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(t) => t.read_input(ms),
Backend::Stdio(s) => s.read_input(ms),
}
}
fn write(&mut self, out: &str) {
match &mut self.backend {
#[cfg(target_os = "linux")]
Backend::DevTty(t) => t.write(out),
Backend::Stdio(s) => s.write(out),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn probe_size_is_never_zero() {
let s = probe_size();
assert!(s.cols >= 1 && s.rows >= 1, "probed size must be usable");
}
#[test]
fn interrupt_flag_is_consumed_once() {
INTERRUPTED.store(true, Ordering::SeqCst);
assert!(take_interrupt());
assert!(!take_interrupt());
}
}