use super::Error;
use crossterm::{
execute,
terminal::{Clear, ClearType},
};
use std::{
env::var,
io::stdout,
path::{PathBuf, MAIN_SEPARATOR_STR},
};
pub fn fmt_path(path: impl AsRef<str>) -> Result<PathBuf, Error> {
fn expand(name: &str) -> Result<String, Error> {
let mut buffer = Vec::new();
for part in var(if let Some(stripped) = name.strip_prefix('$') {
expand(stripped)?
} else {
String::from(name)
})?
.split(MAIN_SEPARATOR_STR)
.map(|part| {
if let Some(stripped) = name.strip_prefix('$') {
expand(stripped)
} else {
Ok(String::from(part))
}
}) {
buffer.push(part?)
}
Ok(buffer.join(MAIN_SEPARATOR_STR))
}
let path = path.as_ref();
Ok(PathBuf::from(
path.split(MAIN_SEPARATOR_STR)
.enumerate()
.map(
|(index, part)| match part {
"~" if index == 0 => expand("HOME"),
_ if part.starts_with('$') => expand(&part[1..]),
_ => Ok(String::from(part)),
}, )
.collect::<Result<Vec<String>, Error>>()?
.join(MAIN_SEPARATOR_STR),
)
.canonicalize()?)
}
pub fn clear() -> Result<(), Error> {
execute!(stdout(), Clear(ClearType::CurrentLine)).map_err(Error::Io)?;
print!("\r");
Ok(())
}