#![cfg(feature = "screen")]
use std::time::Duration;
use rust_expect::{Session, SessionBuilder};
#[tokio::main]
async fn main() -> rust_expect::Result<()> {
let config = SessionBuilder::new()
.command("/usr/bin/htop")
.arg("--no-color")
.env("TERM", "xterm-256color")
.dimensions(120, 30)
.timeout(Duration::from_secs(10))
.build();
eprintln!("[drive_htop] spawning htop --no-color");
let mut session = Session::spawn_with_config("/usr/bin/htop", &["--no-color"], config).await?;
session.attach_screen();
session
.expect_screen_contains("F10", Duration::from_secs(5))
.await?;
eprintln!("[drive_htop] htop is up; footer 'F10' anchor matched");
let header_anchor = if session
.expect_screen_contains("CPU%", Duration::from_secs(2))
.await
.is_ok()
{
"CPU%"
} else {
session
.expect_screen_contains("PID", Duration::from_secs(2))
.await?;
"PID"
};
eprintln!("[drive_htop] table header anchor: {header_anchor}");
eprintln!("[drive_htop] dwelling 1s while htop redraws");
let _ = session
.expect_screen_contains(
"__never_match__sentinel_for_dwell__",
Duration::from_millis(1000),
)
.await;
session
.expect_screen_contains(header_anchor, Duration::from_millis(500))
.await?;
eprintln!("[drive_htop] header still on screen after 1s of redraws");
eprintln!("[drive_htop] sending 'q' to quit");
session.send(b"q").await?;
let _ = session.wait_timeout(Duration::from_secs(3)).await;
eprintln!("[drive_htop] htop exited cleanly");
Ok(())
}