use std::fmt;
pub use colored::*;
pub use atty::Stream;
pub fn supports_hyperlinks() -> bool {
if let Ok(arg) = std::env::var("FORCE_HYPERLINK") {
return arg.trim() != "0";
}
if std::env::var("DOMTERM").is_ok() {
return true;
}
if let Ok(version) = std::env::var("VTE_VERSION") {
if version.parse().unwrap_or(0) >= 5000 {
return true;
}
}
if let Ok(program) = std::env::var("TERM_PROGRAM") {
if matches!(
&program[..],
"Hyper" | "iTerm.app" | "terminology" | "WezTerm"
) {
return true;
}
}
if let Ok(term) = std::env::var("TERM") {
if matches!(&term[..], "xterm-kitty") {
return true;
}
}
std::env::var("WT_SESSION").is_ok() || std::env::var("KONSOLE_VERSION").is_ok()
}
pub fn on(stream: atty::Stream) -> bool {
(std::env::var("FORCE_HYPERLINK").is_ok() || atty::is(stream)) && supports_hyperlinks()
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Link<'a> {
pub id: &'a str,
pub text: &'a str,
pub url: &'a str,
}
impl<'a> Link<'a> {
pub fn new(text: &'a str, url: &'a str) -> Self {
Self { text, url, id: "" }
}
pub fn with_id(text: &'a str, url: &'a str, id: &'a str) -> Self {
Self { text, url, id }
}
}
impl fmt::Display for Link<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.id != "" {
write!(
f,
"\u{1b}]8;id={};{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\",
self.id, self.url, self.text
)
} else {
write!(
f,
"\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\",
self.url, self.text
)
}
}
}