///| The emoji picker's data: every emoji of Unicode Emoji 18.0 in CLDR order, with its name and /// CLDR search keywords (data.mbt, made by scripts/gen-emoji-data.py), and searching it. /// /// Skin tone variants are not listed on their own: an emoji that takes a skin tone carries its /// five variants (light .. dark), and the picker's skin tone choice picks one. ///| One emoji the picker lists. pub(all) struct Emoji { /// 1 Smileys .. 9 Flags (the picker's tab; 0 is Recent). tab : Int text : String name : String /// `name` in lower case, for searching. lower : String keywords : Array[String] /// Empty, or the five skin tone variants, light to dark. tones : Array[String] } ///| The picker's tabs: their names and the emoji drawn on each. pub let tab_names : Array[String] = [ "Recent", "Smileys", "People", "Animals", "Food", "Activities", "Travel", "Objects", "Symbols", "Flags", ] ///| pub let tab_icons : Array[String] = [ "🕘", "😀", "👋", "🐻", "🍔", "⚽", "🚗", "💡", "🔣", "🏁", ] ///| Names of the skin tone choices: 0 is the default yellow, 1-5 light to dark. pub let tone_names : Array[String] = [ "Default skin tone", "Light skin tone", "Medium-light skin tone", "Medium skin tone", "Medium-dark skin tone", "Dark skin tone", ] ///| A hand in each skin tone, for the skin tone choice. pub let tone_swatches : Array[String] = ["✋", "✋🏻", "✋🏼", "✋🏽", "✋🏾", "✋🏿"] ///| let parsed : Array[Emoji] = [] ///| fn words(s : StringView) -> Array[String] { let out : Array[String] = [] for w in s.split(" ") { if w.length() > 0 { out.push(w.to_string()) } } out } ///| Every emoji, in picker order (parsed on first use). pub fn all() -> Array[Emoji] { if parsed.length() == 0 { for line in emoji_data.split("\n") { let f : Array[StringView] = line.split("\t").collect() if f.length() != 5 { abort("emoji data: bad line: " + line.to_string()) } let tab = match f[0].to_string() { "0" => 1 "1" => 2 "2" => 3 "3" => 4 "4" => 5 "5" => 6 "6" => 7 "7" => 8 "8" => 9 t => abort("emoji data: bad tab " + t) } parsed.push({ tab, text: f[1].to_string(), name: f[2].to_string(), lower: f[2].to_string().to_lower(), keywords: words(f[3]), tones: words(f[4]), }) } } parsed } ///| `e` in skin tone `tone` (0 default, 1-5 light to dark); `e` itself when it takes no tone. pub fn Emoji::toned(self : Emoji, tone : Int) -> String { if tone >= 1 && tone <= 5 && self.tones.length() == 5 { self.tones[tone - 1] } else { self.text } } ///| The emoji whose text (or one of whose skin tone variants) is `text`: its index in `all()` and /// the tone (0 for the plain one). pub fn find(text : String) -> (Int, Int)? { let es = all() for i, e in es { if e.text == text { return Some((i, 0)) } for t, v in e.tones { if v == text { return Some((i, t + 1)) } } } None } ///| A word of a name without the punctuation around it. fn bare(w : StringView) -> String { w.trim_start(chars="(\"").trim_end(chars=")\":,").to_string() } ///| How well one query word matches an emoji: lower is better, None when it doesn't. fn word_score(e : Emoji, q : String) -> Int? { if e.lower == q { return Some(0) } let name_words = e.lower.split(" ") for w in name_words { if bare(w) == q { return Some(1) } } if e.lower.has_prefix(q) { return Some(2) } for w in name_words { if bare(w).has_prefix(q) { return Some(3) } } for k in e.keywords { if k == q { return Some(4) } } for k in e.keywords { if k.has_prefix(q) { return Some(5) } } if q.length() >= 3 && e.lower.contains(q) { return Some(6) } None } ///| The emoji matching `query`, best first: every word of the query must start a word of the /// emoji's name or keywords (or, from three letters on, appear in its name). Case doesn't /// matter, and a leading ':' (as in ":smile") is skipped. Only emoji `keep` accepts are listed. pub fn search(query : String, keep? : (Int) -> Bool = _ => true) -> Array[Int] { let q = query.to_lower().trim(chars=" ").to_string() let q = if q.has_prefix(":") { q.trim_start(chars=":").to_string() } else { q } let qs = words(q) if qs.length() == 0 { return [] } let scored : Array[(Int, Int, Int)] = [] for i, e in all() { let mut total = 0 let mut ok = true for w in qs { match word_score(e, w) { Some(s) => total = total + s None => { ok = false break } } } if ok && keep(i) { scored.push((total, e.name.length(), i)) } } // Best score first; then shorter names ("red heart" before "sparkling heart"); then CLDR order. scored.sort_by((a, b) => if a.0 != b.0 { a.0.compare(b.0) } else if a.1 != b.1 { a.1.compare(b.1) } else { a.2.compare(b.2) }) scored.map(s => s.2) }