termwiz/
emoji.rs

1use crate::emoji_variation::VARIATION_MAP;
2#[cfg(feature = "use_serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
7pub enum Presentation {
8    Text,
9    Emoji,
10}
11
12impl Presentation {
13    /// Returns the default presentation followed
14    /// by the explicit presentation if specified
15    /// by a variation selector
16    pub fn for_grapheme(s: &str) -> (Self, Option<Self>) {
17        if let Some((a, b)) = VARIATION_MAP.get(s) {
18            return (*a, Some(*b));
19        }
20        let mut presentation = Self::Text;
21        for c in s.chars() {
22            if Self::for_char(c) == Self::Emoji {
23                presentation = Self::Emoji;
24                break;
25            }
26            // Note that `c` may be some other combining
27            // sequence that doesn't definitively indicate
28            // that we're text, so we only positively
29            // change presentation when we identify an
30            // emoji char.
31        }
32        (presentation, None)
33    }
34
35    pub fn for_char(c: char) -> Self {
36        if crate::emoji_presentation::EMOJI_PRESENTATION.contains_u32(c as u32) {
37            Self::Emoji
38        } else {
39            Self::Text
40        }
41    }
42}