pub struct Hyperlink<D: core::fmt::Display> {
url: Option<D>,
}
impl<D: core::fmt::Display> Hyperlink<D> {
pub fn with_url(url: D) -> Self {
Self { url: Some(url) }
}
}
#[cfg(feature = "std")]
impl Hyperlink<String> {
#[cfg(feature = "file")]
pub fn with_path(path: &std::path::Path) -> Self {
let url = crate::path_to_url(path);
Self { url }
}
}
impl<D: core::fmt::Display> Default for Hyperlink<D> {
fn default() -> Self {
Self { url: None }
}
}
impl<D: core::fmt::Display> core::fmt::Display for Hyperlink<D> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let Some(url) = self.url.as_ref() else {
return Ok(());
};
if f.alternate() {
write!(f, "\x1B]8;;\x1B\\")
} else {
write!(f, "\x1B]8;;{url}\x1B\\")
}
}
}