use std::{future::Future, io};
use crate::tui::DefaultTerminal;
use anyhow::{anyhow, Context};
use crossterm::{
cursor::{MoveTo, Show},
event::{DisableFocusChange, EnableFocusChange, Event},
execute, queue,
style::Print,
terminal::{disable_raw_mode, Clear, ClearType, LeaveAlternateScreen},
};
use super::{
keyboard_modes, mouse_capture, synced_backend::TerminalScreen, terminal_events::TerminalEvents,
};
pub(super) struct SuspendedRun<T> {
pub(super) operation_result: anyhow::Result<T>,
pub(super) resume_result: anyhow::Result<()>,
}
pub(super) struct TerminalSession {
screen: TerminalScreen,
events: Option<TerminalEvents>,
keyboard: Option<keyboard_modes::Enabled>,
mouse_capture_enabled: bool,
focus_change_enabled: bool,
}
impl TerminalSession {
pub(super) fn acquire(screen: TerminalScreen) -> Self {
Self {
screen,
events: Some(TerminalEvents::new()),
mouse_capture_enabled: acquire_mouse_capture(screen),
keyboard: Some(keyboard_modes::Enabled::acquire()),
focus_change_enabled: enable_focus_change().is_ok(),
}
}
pub(super) fn reassert_mouse_capture(&self) {
match self.screen {
TerminalScreen::Alternate => mouse_capture::reassert(),
TerminalScreen::Main => {}
}
}
pub(super) async fn next_event(&mut self) -> std::io::Result<Event> {
self.events
.as_mut()
.expect("terminal events active")
.next()
.await
}
pub(super) async fn run_suspended<T, F, Fut>(
&mut self,
terminal: &mut DefaultTerminal,
handoff_status: &str,
operation: F,
) -> SuspendedRun<T>
where
F: FnOnce() -> Fut,
Fut: Future<Output = anyhow::Result<T>>,
{
self.stop_events();
if let Err(suspend_error) = self.suspend(handoff_status) {
return SuspendedRun {
operation_result: Err(suspend_error.context("suspended operation was not started")),
resume_result: self
.resume(terminal)
.context("failed to recover Rho after terminal suspension failed"),
};
}
let operation_result = operation().await;
let resume_result = self
.resume(terminal)
.context("failed to resume Rho after suspended operation");
SuspendedRun {
operation_result,
resume_result,
}
}
fn stop_events(&mut self) {
self.events = None;
}
fn suspend(&mut self, handoff_status: &str) -> anyhow::Result<()> {
let mut failures = Vec::new();
if self.focus_change_enabled {
if let Err(error) = disable_focus_change() {
failures.push(format!("disable focus change: {error}"));
}
self.focus_change_enabled = false;
}
if let Some(keyboard) = self.keyboard.take() {
if let Err(error) = keyboard.try_release() {
failures.push(format!("disable keyboard modes: {error}"));
}
}
if self.mouse_capture_enabled {
if let Err(error) = mouse_capture::disable() {
failures.push(format!("disable mouse capture: {error}"));
}
self.mouse_capture_enabled = false;
}
if let Err(error) = hand_off_terminal(self.screen, handoff_status) {
failures.push(format!("restore terminal: {error}"));
}
if failures.is_empty() {
Ok(())
} else {
Err(anyhow!(failures.join("; ")))
}
}
fn resume(&mut self, terminal: &mut DefaultTerminal) -> anyhow::Result<()> {
let mut resumed =
crate::tui::synced_backend::try_init(self.screen).context("initialize terminal")?;
match self.screen {
TerminalScreen::Alternate => {}
TerminalScreen::Main => crate::tui::synced_backend::claim_main_screen(&mut resumed)
.context("claim the main screen")?,
}
*terminal = resumed;
self.mouse_capture_enabled = acquire_mouse_capture(self.screen);
self.keyboard = Some(keyboard_modes::Enabled::acquire());
self.focus_change_enabled = enable_focus_change().is_ok();
self.events = Some(TerminalEvents::new());
Ok(())
}
}
fn enable_focus_change() -> io::Result<()> {
execute!(io::stdout(), EnableFocusChange)
}
fn disable_focus_change() -> io::Result<()> {
execute!(io::stdout(), DisableFocusChange)
}
fn acquire_mouse_capture(screen: TerminalScreen) -> bool {
match screen {
TerminalScreen::Alternate => mouse_capture::enable().is_ok(),
TerminalScreen::Main => false,
}
}
fn hand_off_terminal(screen: TerminalScreen, handoff_status: &str) -> io::Result<()> {
disable_raw_mode()?;
let mut stdout = io::stdout();
match screen {
TerminalScreen::Alternate => queue!(stdout, LeaveAlternateScreen, Clear(ClearType::All))?,
TerminalScreen::Main => {
let (_, rows) = crossterm::terminal::size()?;
super::synced_backend::queue_erase_rows(&mut stdout, 0..rows)?;
}
}
execute!(
stdout,
MoveTo(0, 0),
Show,
Print(handoff_status.replace("\r\n", "\n").replace('\n', "\r\n")),
Print("\r\n"),
)?;
Ok(())
}
impl Drop for TerminalSession {
fn drop(&mut self) {
self.stop_events();
if let Some(keyboard) = self.keyboard.take() {
keyboard.release();
}
if self.mouse_capture_enabled {
let _ = mouse_capture::disable();
self.mouse_capture_enabled = false;
}
if self.focus_change_enabled {
let _ = disable_focus_change();
self.focus_change_enabled = false;
}
}
}