From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/supports-hyperlinks/src/lib.rs | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 vendor/supports-hyperlinks/src/lib.rs (limited to 'vendor/supports-hyperlinks/src') diff --git a/vendor/supports-hyperlinks/src/lib.rs b/vendor/supports-hyperlinks/src/lib.rs new file mode 100644 index 0000000..83290b2 --- /dev/null +++ b/vendor/supports-hyperlinks/src/lib.rs @@ -0,0 +1,67 @@ +#![doc = include_str!("../README.md")] + +/// possible stream sources +#[derive(Clone, Copy, Debug)] +pub enum Stream { + Stdout, + Stderr, +} + +/// Returns true if the current terminal, detected through various environment +/// variables, is known to support hyperlink rendering. +pub fn supports_hyperlinks() -> bool { + // Hyperlinks can be forced through this env var. + if let Ok(arg) = std::env::var("FORCE_HYPERLINK") { + return arg.trim() != "0"; + } + + if std::env::var("DOMTERM").is_ok() { + // DomTerm + return true; + } + + if let Ok(version) = std::env::var("VTE_VERSION") { + // VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc) + 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" | "vscode" + ) { + return true; + } + } + + if let Ok(term) = std::env::var("TERM") { + if matches!(&term[..], "xterm-kitty" | "alacritty" | "alacritty-direct") { + return true; + } + } + + if let Ok(term) = std::env::var("COLORTERM") { + if matches!(&term[..], "xfce4-terminal") { + return true; + } + } + + // Windows Terminal and Konsole + std::env::var("WT_SESSION").is_ok() || std::env::var("KONSOLE_VERSION").is_ok() +} + +fn is_a_tty(stream: Stream) -> bool { + use is_terminal::*; + match stream { + Stream::Stdout => std::io::stdout().is_terminal(), + Stream::Stderr => std::io::stderr().is_terminal(), + } +} + +/// Returns true if `stream` is a TTY, and the current terminal +/// [supports_hyperlinks]. +pub fn on(stream: Stream) -> bool { + (std::env::var("FORCE_HYPERLINK").is_ok() || is_a_tty(stream)) && supports_hyperlinks() +} -- cgit v1.2.3