use std::io::Write;
use std::path::PathBuf;
use std::process;
use std::process::{ExitStatus, Stdio};
use console::Term;
use miette::IntoDiagnostic;
use ockam_core::env::get_env_with_default;
use crate::util::exitcode;
pub fn render_output(s: &str) {
let pager = get_env_with_default("PAGER", "less".to_string()).expect("Invalid PAGER value");
match which::which(pager) {
Ok(pager_binary_path) => {
paginate_with(pager_binary_path, s).expect("Failed to paginate output");
}
Err(_) => {
println!("{}", s);
}
}
}
fn paginate_with(pager_binary_path: PathBuf, s: &str) -> miette::Result<ExitStatus> {
let pager = pager_binary_path.file_name().unwrap().to_string_lossy();
let mut pager_cmd = process::Command::new(pager.as_ref());
if pager.as_ref() == "less" {
pager_cmd.env("LESS", "FRX");
}
let mut pager_process = pager_cmd.stdin(Stdio::piped()).spawn().into_diagnostic()?;
{
let mut pager_stdin = pager_process
.stdin
.take()
.expect("Failed to get pager's stdin");
pager_stdin.write_all(s.as_bytes()).into_diagnostic()?;
}
pager_process.wait().into_diagnostic()
}
pub fn render_help(help: clap::Error) {
let pager = get_env_with_default("PAGER", "less".to_string()).expect("Invalid PAGER value");
match which::which(pager) {
Ok(pager_binary_path) => {
paginate_help_with(pager_binary_path, help).expect("Failed to paginate help");
}
Err(_) => {
help.exit();
}
}
}
fn paginate_help_with(pager_binary_path: PathBuf, help: clap::Error) -> miette::Result<()> {
let rendered_text = if Term::stdout().is_term() {
help.render().ansi().to_string()
} else {
help.render().to_string()
};
paginate_with(pager_binary_path, &rendered_text)?;
let code = if help.use_stderr() {
exitcode::USAGE
} else {
exitcode::OK
};
process::exit(code);
}