#![cfg(feature = "screen")]
use std::io::Write;
use std::time::Duration;
use rust_expect::{Session, SessionBuilder};
#[tokio::main]
async fn main() -> rust_expect::Result<()> {
let mut tmp = tempfile::NamedTempFile::new().expect("tempfile");
for i in 1..=200 {
writeln!(
tmp,
"line {i:03}: the quick brown fox jumps over the lazy dog"
)
.unwrap();
}
let path = tmp.path().to_path_buf();
tmp.as_file_mut().flush().ok();
let path_str = path.to_str().expect("utf-8 path");
eprintln!("[drive_less] spawning less {path_str}");
let config = SessionBuilder::new()
.command("/usr/bin/less")
.arg("-R") .arg(path_str)
.env("TERM", "xterm-256color")
.env("LESS", "")
.dimensions(80, 24)
.timeout(Duration::from_secs(10))
.build();
let mut session =
Session::spawn_with_config("/usr/bin/less", &["-R", path_str], config).await?;
session.attach_screen();
session
.expect_screen_contains("line 001:", Duration::from_secs(3))
.await?;
session
.expect_screen_contains("line 020:", Duration::from_secs(3))
.await?;
session
.expect_screen_contains("line 023:", Duration::from_secs(3))
.await?;
eprintln!("[drive_less] first page rendered as expected");
session.send(b" ").await?;
session
.wait_screen_stable(Duration::from_millis(150), Duration::from_secs(2))
.await?;
session
.expect_screen_contains("line 024:", Duration::from_secs(3))
.await?;
eprintln!("[drive_less] second page rendered after Space");
session.send(b"G").await?;
session
.wait_screen_stable(Duration::from_millis(150), Duration::from_secs(2))
.await?;
session
.expect_screen_contains("line 200:", Duration::from_secs(3))
.await?;
eprintln!("[drive_less] tail rendered after 'G'");
let text = session.screen().unwrap().lock().unwrap().text();
let last_three: Vec<&str> = text
.lines()
.rev()
.filter(|l| !l.trim().is_empty())
.take(3)
.collect();
eprintln!("[drive_less] last three non-blank lines on screen:");
for line in last_three.iter().rev() {
eprintln!("[drive_less] {line}");
}
session.send(b"q").await?;
let _ = session.wait_timeout(Duration::from_secs(3)).await;
eprintln!("[drive_less] less exited cleanly");
Ok(())
}