1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use atty::Stream;
use std::fmt;

#[cfg(not(target_os = "macos"))]
lazy_static::lazy_static! {
    static ref IS_LANG_UTF8: bool = {
        match std::env::var("LANG") {
            Ok(lang) => lang.to_uppercase().ends_with("UTF-8"),
            _ => false,
        }
    };
}

/// An emoji with safety fallback.
///
/// The struct wraps an emoji and only renders it on platforms that actually
/// support it. On non-supported platforms the fallback value is being rendered.
///
/// Support is determined by two factors:
///
/// 1) The processes stdout has to be a tty.
/// 2) Platform dependent:
///     - macOS has emoji support by default
///     - Unix systems have support if the active language supports them.
///     - Windows machines running the new Terminal app support emojis.
pub struct Emoji<'a, 'b>(pub &'a str, pub &'b str);

impl<'a, 'b> Emoji<'a, 'b> {
    /// Create a new emoji.
    ///
    /// # Arguments
    ///
    /// - `emoji`: The unicode emoji to display.
    /// - `fallback`: The fallback value to use on non-supported platforms.
    pub const fn new(emoji: &'a str, fallback: &'b str) -> Self {
        Self(emoji, fallback)
    }
}

impl fmt::Display for Emoji<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if should_display_emoji() {
            write!(f, "{}", self.0)
        } else {
            write!(f, "{}", self.1)
        }
    }
}

// Emojis should only get displayed if the current terminal is a tty and the
// platform does support emojis.
fn should_display_emoji() -> bool {
    atty::is(Stream::Stdout) && is_emoji_supported()
}

// The new Windows Terminal does support emojis. Currently, the terminal will
// set the environment variable `WT_SESSION`. This can be used to check if the
// user uses that specific app.
#[cfg(windows)]
fn is_emoji_supported() -> bool {
    std::env::var("WT_SESSION").is_some()
}

// macOS by default has emoji support.
#[cfg(target_os = "macos")]
fn is_emoji_supported() -> bool {
    true
}

// On unix systems the enabled language decides whether emojis are supported or
// not.
#[cfg(not(target_os = "macos"))]
fn is_emoji_supported() -> bool {
    *IS_LANG_UTF8
}