BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / emoji / typing.rs

typing.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
typing.rs 5.3 KiB Download Raw
1use std::ops::Range;
2
3const MIN_QUERY_CHARS: usize = 2;
4const MAX_CODE_CHARS: usize = 40;
5const SMILEYS: [(&str, &str); 6] = [
6 (":-)", "🙂"),
7 (":)", "🙂"),
8 (":(", "🙁"),
9 (":D", "😀"),
10 (";)", "😉"),
11 ("<3", "❤️"),
12];
13
14fn opens_word(before: &str) -> bool {
15 before
16 .chars()
17 .next_back()
18 .is_none_or(|previous| previous.is_whitespace() || previous == '(')
19}
20
21fn is_code_char(character: char) -> bool {
22 character.is_alphanumeric() || matches!(character, '_' | '-' | '+')
23}
24
25fn is_code(code: &str) -> bool {
26 !code.is_empty() && code.chars().count() <= MAX_CODE_CHARS && code.chars().all(is_code_char)
27}
28
29/// The one character typed just before `cursor`, if that is the only difference to `previous`.
30pub fn typed_char(previous: &str, value: &str, cursor: usize) -> Option<char> {
31 let typed = value.get(..cursor)?.chars().next_back()?;
32 let start = cursor - typed.len_utf8();
33 let one_more = value.len() == previous.len() + typed.len_utf8()
34 && previous.get(..start)? == &value[..start]
35 && previous.get(start..)? == &value[cursor..];
36 one_more.then_some(typed)
37}
38
39/// `:th|` -> the open query `th`. `10:30`, `Hinweis: `, `:+1` and `a:b` never open.
40pub fn active_query(text: &str, cursor: usize) -> Option<(Range<usize>, String)> {
41 let before = text.get(..cursor)?;
42 let colon = before.rfind(':')?;
43 let query = &before[colon + 1..];
44 let first = query.chars().next()?;
45 let opens = opens_word(&before[..colon])
46 && (first.is_alphabetic() || matches!(first, '+' | '-'))
47 && query.chars().count() >= MIN_QUERY_CHARS
48 && is_code(query)
49 && query.chars().any(char::is_alphabetic);
50 opens.then(|| (colon..cursor, query.to_owned()))
51}
52
53/// `:thumbsup:|` -> range of the whole code and the code itself.
54pub fn closing_code(text: &str, cursor: usize) -> Option<(Range<usize>, &str)> {
55 let before = text.get(..cursor)?.strip_suffix(':')?;
56 let colon = before.rfind(':')?;
57 let code = &before[colon + 1..];
58 (opens_word(&before[..colon]) && is_code(code)).then_some((colon..cursor, code))
59}
60
61fn smiley_ending_at(text: &str, end: usize) -> Option<(Range<usize>, &'static str)> {
62 let before = text.get(..end)?;
63 SMILEYS.iter().find_map(|(smiley, glyph)| {
64 let start = end.checked_sub(smiley.len())?;
65 (before.ends_with(smiley) && opens_word(&before[..start])).then_some((start..end, *glyph))
66 })
67}
68
69/// `:) |` -> range of `:)` and its emoji.
70pub fn smiley_before_space(text: &str, cursor: usize) -> Option<(Range<usize>, &'static str)> {
71 let end = cursor.checked_sub(1)?;
72 (text.get(end..cursor)? == " ")
73 .then(|| smiley_ending_at(text, end))
74 .flatten()
75}
76
77/// The smiley that ends the text, converted on Enter.
78pub fn trailing_smiley(text: &str) -> Option<(Range<usize>, &'static str)> {
79 smiley_ending_at(text, text.trim_end().len())
80}
81
82#[cfg(test)]
83mod tests {
84 use super::{active_query, closing_code, smiley_before_space, trailing_smiley, typed_char};
85
86 fn query(text: &str) -> Option<String> {
87 active_query(text, text.len()).map(|(_, query)| query)
88 }
89
90 #[test]
91 fn typed_char_sees_only_single_insertions() {
92 assert_eq!(typed_char("a:ok", "a:ok:", 5), Some(':'));
93 assert_eq!(typed_char("ab", "a:b", 2), Some(':'));
94 assert_eq!(typed_char("👍", ":+1:", 4), None);
95 assert_eq!(typed_char("a", "a::", 3), None);
96 }
97
98 #[test]
99 fn colon_at_word_start_opens_after_two_letters() {
100 assert_eq!(active_query("Danke :th", 9), Some((6..9, "th".into())));
101 assert_eq!(query(":dau"), Some("dau".into()));
102 assert_eq!(query("(:herz"), Some("herz".into()));
103 assert_eq!(query("Danke :t"), None);
104 }
105
106 #[test]
107 fn umlauts_count_as_letters() {
108 assert_eq!(query(":drü"), Some("drü".into()));
109 }
110
111 #[test]
112 fn times_labels_urls_and_numbers_stay_text() {
113 assert_eq!(query("Treffen um 10:30"), None);
114 assert_eq!(query("Hinweis: bitte"), None);
115 assert_eq!(query("Siehe https://example"), None);
116 assert_eq!(query("Ergebnis:ok"), None);
117 assert_eq!(query("Faktor 3:2"), None);
118 assert_eq!(query("Okay :+1"), None);
119 assert_eq!(query(":th\nx"), None);
120 }
121
122 #[test]
123 fn closing_colon_yields_the_code() {
124 let text = "Gute Arbeit :thumbsup:";
125 assert_eq!(closing_code(text, text.len()), Some((12..22, "thumbsup")));
126 assert_eq!(closing_code("Okay :+1:", 9), Some((5..9, "+1")));
127 assert_eq!(closing_code("um 10:30:", 9), None);
128 assert_eq!(closing_code("Hinweis::", 9), None);
129 }
130
131 #[test]
132 fn smiley_converts_on_space_at_word_start() {
133 assert_eq!(smiley_before_space("Hi :) ", 6), Some((3..5, "🙂")));
134 assert_eq!(smiley_before_space("Hi :-) ", 7), Some((3..6, "🙂")));
135 assert_eq!(smiley_before_space("<3 ", 3), Some((0..2, "❤️")));
136 assert_eq!(smiley_before_space("(Hinweis:) ", 11), None);
137 assert_eq!(smiley_before_space("a:D ", 4), None);
138 assert_eq!(smiley_before_space("Hi :)", 5), None);
139 }
140
141 #[test]
142 fn trailing_smiley_converts_on_enter() {
143 assert_eq!(trailing_smiley("Danke ;)"), Some((6..8, "😉")));
144 assert_eq!(trailing_smiley("Danke ;) "), Some((6..8, "😉")));
145 assert_eq!(trailing_smiley("a:D/b"), None);
146 }
147}