gmocli/
lib.rs

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::collections::HashMap;

const GMOCLI_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/data/gmocli.tsv"));

type GmocliMap<'a> = HashMap<&'a str, Gmocli<'a>>;

#[derive(Debug)]
pub struct Gmocli<'a> {
    pub emoji: &'a str,
    pub name: &'a str,
    pub category_name: &'a str,
    pub subcategory_name: &'a str,
    pub en_keywords: &'a str,
    pub en_tts_description: &'a str,
    pub gitmoji_description: &'a str,
}

impl<'a> Gmocli<'a> {
    pub fn print(&self, with_info: bool) {
        match with_info {
            true => {
                let mut gitmoji_description = String::new();
                if self.gitmoji_description.len() > 0 {
                    gitmoji_description = format!(" # {}", self.gitmoji_description);
                }
                println!(
                    "{} {} | {} / {} | {}{}",
                    self.emoji,
                    self.name,
                    self.category_name,
                    self.subcategory_name,
                    self.en_keywords,
                    &gitmoji_description[..],
                )
            }
            false => {
                print!("{}", self.emoji);
            }
        }
    }
}

#[derive(Debug)]
pub struct GmocliIndex<'a> {
    pub ordering: Vec<&'a str>,
    pub map: GmocliMap<'a>,
}

impl<'a> GmocliIndex<'a> {
    pub fn get_emoji_by_name(&self, name: &str) -> Option<&'a str> {
        let mut ret = None;
        for emoji in self.ordering.iter() {
            let gmocli = self.map.get(emoji).unwrap();
            if gmocli.name == name {
                ret = Some(&emoji[..]);
                break;
            }
        }
        ret
    }

    pub fn new() -> GmocliIndex<'a> {
        let mut ordering: Vec<&'a str> = vec![];
        let mut map: GmocliMap = GmocliMap::new();
        let gmocli_data = std::str::from_utf8(GMOCLI_DATA).unwrap();

        for line in gmocli_data.lines() {
            let split_tabs: Vec<&str> = line.split("\t").collect();
            let emoji = split_tabs[0];
            let name = split_tabs[1];
            let category_name = split_tabs[2];
            let subcategory_name = split_tabs[3];
            let en_tts_description = split_tabs[4];
            let en_keywords = split_tabs[5];
            let gitmoji_description = split_tabs[6];
            ordering.push(emoji);
            map.insert(
                emoji,
                Gmocli {
                    emoji,
                    name,
                    category_name,
                    subcategory_name,
                    en_keywords,
                    en_tts_description,
                    gitmoji_description,
                },
            );
        }

        GmocliIndex { ordering, map }
    }

    pub fn print_list(&self, with_info: bool) {
        for emoji in self.ordering.iter() {
            self.map.get(emoji).unwrap().print(with_info);
        }
    }

    pub fn search_gmoclis(&self, search_keys: Vec<&str>) -> Vec<&'a str> {
        let mut matches: Vec<&'a str> = vec![];
        if &search_keys.len() > &0 {
            for emoji in self.ordering.iter() {
                let gmocli = self.map.get(emoji).unwrap();
                let search_string = format!(
                    "{} {} {} {} {} {} {}",
                    gmocli.emoji,
                    gmocli.name,
                    gmocli.category_name,
                    gmocli.subcategory_name,
                    gmocli.en_keywords,
                    gmocli.en_tts_description,
                    gmocli.gitmoji_description,
                );
                let mut has_match = false;
                for search_key in &search_keys {
                    if search_string
                        .to_lowercase()
                        .contains(search_key.to_lowercase().as_str())
                    {
                        has_match = true;
                        break;
                    }
                }
                if has_match {
                    matches.push(emoji);
                }
            }
        }
        matches
    }
}