use super::target::Origin;
use crate::common::report::prepare_report_message_and_divert;
use yash_env::Env;
use yash_env::io::Fd;
use yash_env::path::Path;
use yash_env::source::pretty::{Report, ReportType};
use yash_env::system::{Errno, Fcntl, Isatty, Write};
impl Origin {
pub fn should_print_path(&self) -> bool {
use Origin::*;
match self {
Oldpwd | Cdpath => true,
Home | Literal => false,
}
}
}
pub async fn print_path<S>(env: &mut Env<S>, path: &Path, origin: &Origin)
where
S: Fcntl + Isatty + Write,
{
if !origin.should_print_path() {
return;
}
let line = format!("{}\n", path.display());
match env.system.write_all(Fd::STDOUT, line.as_bytes()).await {
Ok(_) => (),
Err(errno) => handle_print_error(env, errno).await,
}
}
async fn handle_print_error<S>(env: &mut Env<S>, errno: Errno)
where
S: Fcntl + Isatty + Write,
{
let mut report = Report::new();
report.r#type = ReportType::Warning;
report.title = format!("cannot print new $PWD: {errno}").into();
let (message, _divert) = prepare_report_message_and_divert(env, report);
env.system.print_error(&message).await;
}