#![cfg(not(tarpaulin_include))]
use std::env;
use std::io::{self, Write};
use std::process::{Command, Stdio};
use std::sync::LazyLock;
pub static PAGER: LazyLock<String> =
LazyLock::new(|| env::var("PAGER").unwrap_or_else(|_| String::from("less")));
pub struct Pager;
impl Pager {
pub fn page_or_print(content: &str) {
if Self::page(content).is_err() {
if content.ends_with('\n') {
print!("{content}");
} else {
println!("{content}");
}
}
}
pub fn page(content: &str) -> Result<(), io::Error> {
let mut pager = Command::new(&*PAGER);
pager.stdin(Stdio::piped());
pager.stdout(Stdio::inherit());
pager.stderr(Stdio::inherit());
if *PAGER == "less" || PAGER.ends_with("/less") {
pager.env("LESSCHARSET", "UTF-8");
pager.arg("-R"); pager.arg("-F"); pager.arg("-X"); }
let mut child = pager.spawn()?;
let Some(stdin) = child.stdin.as_mut() else {
return Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"Failed to open stdin.",
));
};
if content.ends_with('\n') {
write!(stdin, "{content}")?;
} else {
writeln!(stdin, "{content}")?;
}
child.wait()?;
Ok(())
}
}