supports_hyperlinks/
lib.rs

1#![doc = include_str!("../README.md")]
2
3/// possible stream sources
4#[derive(Clone, Copy, Debug)]
5pub enum Stream {
6    Stdout,
7    Stderr,
8}
9
10/// Returns true if the current terminal, detected through various environment
11/// variables, is known to support hyperlink rendering.
12pub fn supports_hyperlinks() -> bool {
13    // Hyperlinks can be forced through this env var.
14    if let Ok(arg) = std::env::var("FORCE_HYPERLINK") {
15        return arg.trim() != "0";
16    }
17
18    if std::env::var("DOMTERM").is_ok() {
19        // DomTerm
20        return true;
21    }
22
23    if let Ok(version) = std::env::var("VTE_VERSION") {
24        // VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
25        if version.parse().unwrap_or(0) >= 5000 {
26            return true;
27        }
28    }
29
30    if let Ok(program) = std::env::var("TERM_PROGRAM") {
31        if matches!(
32            &program[..],
33            "Hyper" | "iTerm.app" | "terminology" | "WezTerm" | "vscode" | "ghostty"
34        ) {
35            return true;
36        }
37    }
38
39    if let Ok(term) = std::env::var("TERM") {
40        if matches!(&term[..], "xterm-kitty" | "alacritty" | "alacritty-direct") {
41            return true;
42        }
43    }
44
45    if let Ok(term) = std::env::var("COLORTERM") {
46        if matches!(&term[..], "xfce4-terminal") {
47            return true;
48        }
49    }
50
51    // Windows Terminal and Konsole
52    std::env::var("WT_SESSION").is_ok() || std::env::var("KONSOLE_VERSION").is_ok()
53}
54
55fn is_a_tty(stream: Stream) -> bool {
56    use std::io::IsTerminal;
57    match stream {
58        Stream::Stdout => std::io::stdout().is_terminal(),
59        Stream::Stderr => std::io::stderr().is_terminal(),
60    }
61}
62
63/// Returns true if `stream` is a TTY, and the current terminal
64/// [supports_hyperlinks].
65pub fn on(stream: Stream) -> bool {
66    (std::env::var("FORCE_HYPERLINK").is_ok() || is_a_tty(stream)) && supports_hyperlinks()
67}