agent-ssh 0.4.4

Arrange agent terminal layouts and reconnect through provider attachment plans
Documentation
use std::{cell::Cell, io::IsTerminal, time::Duration};

use indicatif::{ProgressBar, ProgressStyle};

/// Transient stderr status; redirected output and machine-readable plans stay clean.
pub struct Progress(ProgressBar, Cell<bool>);

impl Progress {
    pub fn new(message: &'static str, enabled: bool) -> Self {
        let bar = if enabled && std::io::stderr().is_terminal() {
            ProgressBar::new_spinner().with_style(
                ProgressStyle::with_template("{spinner:.cyan} {msg}")
                    .expect("valid progress template")
                    .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
            )
        } else {
            ProgressBar::hidden()
        };
        bar.set_message(message);
        if !bar.is_hidden() {
            bar.enable_steady_tick(Duration::from_millis(80));
            bar.tick();
        }
        Self(bar, Cell::new(false))
    }

    pub fn message(&self, message: impl Into<String>) {
        self.0.set_message(message.into());
    }

    pub fn waiting_for_checkpoint(&self, elapsed: Duration) {
        if self.0.is_hidden() && !self.1.replace(true) {
            eprintln!("Waiting for Terminal layout checkpoint (up to 6 minutes, Ctrl+C to cancel)");
        }
        self.message(format!(
            "Waiting for Terminal layout ({}:{:02} elapsed, Ctrl+C to cancel)",
            elapsed.as_secs() / 60,
            elapsed.as_secs() % 60,
        ));
    }

    pub fn warn(&self, message: impl std::fmt::Display) {
        self.0.suspend(|| eprintln!("Warning: {message}"));
    }
}

impl Drop for Progress {
    fn drop(&mut self) {
        self.0.finish_and_clear();
    }
}