use super::target::Origin;
use crate::common::arrange_message_and_divert;
use yash_env::Env;
use yash_env::path::Path;
use yash_env::system::Errno;
use yash_syntax::source::pretty::AnnotationType;
use yash_syntax::source::pretty::Message;
use yash_syntax::syntax::Fd;
impl Origin {
pub fn should_print_path(&self) -> bool {
use Origin::*;
match self {
Oldpwd | Cdpath => true,
Home | Literal => false,
}
}
}
pub async fn print_path(env: &mut Env, path: &Path, origin: &Origin) {
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(env: &mut Env, errno: Errno) {
let message = Message {
r#type: AnnotationType::Warning,
title: format!("cannot print new $PWD: {errno}").into(),
annotations: vec![],
footers: vec![],
};
let (message, _divert) = arrange_message_and_divert(env, message);
env.system.print_error(&message).await;
}