Commit 23a729e565

23a729e565cf576dbb5dfb23a15b322d6a4c4794

parent: 0c52375e06

Unregistered key

Bernhard Bliem <bernhard@bliem.xyz> · 2024-03-23 23:35 UTC
committer: <noreply@github.com>

Support displaying shortcodes instead of Emojis in messages (#222)

Layout: unified · split

docs/example_config.json +1
@@ -10,6 +10,7 @@
1010 },
1111 "settings": {
1212 "log_level": "warn",
13 "message_shortcode_display": false,
1314 "reaction_display": true,
1415 "reaction_shortcode_display": false,
1516 "read_receipt_send": true,
docs/iamb.5.md +4
@@ -53,6 +53,10 @@ overridden as described in *PROFILES*.
5353> Specifies the lowest log level that should be shown. Possible values
5454> are: _trace_, _debug_, _info_, _warn_, and _error_.
5555
56**message_shortcode_display** (type: boolean)
57> Defines whether or not emoji characters in messages should be replaced by
58> their respective shortcodes.
59
5660**reaction_display** (type: boolean)
5761> Defines whether or not reactions should be shown.
5862
src/config.rs +6
@@ -469,6 +469,7 @@ impl SortOverrides {
469469#[derive(Clone)]
470470pub struct TunableValues {
471471 pub log_level: Level,
472 pub message_shortcode_display: bool,
472473 pub reaction_display: bool,
473474 pub reaction_shortcode_display: bool,
474475 pub read_receipt_send: bool,
@@ -489,6 +490,7 @@ pub struct TunableValues {
489490#[derive(Clone, Default, Deserialize)]
490491pub struct Tunables {
491492 pub log_level: Option<LogLevel>,
493 pub message_shortcode_display: Option<bool>,
492494 pub reaction_display: Option<bool>,
493495 pub reaction_shortcode_display: Option<bool>,
494496 pub read_receipt_send: Option<bool>,
@@ -511,6 +513,9 @@ impl Tunables {
511513 fn merge(self, other: Self) -> Self {
512514 Tunables {
513515 log_level: self.log_level.or(other.log_level),
516 message_shortcode_display: self
517 .message_shortcode_display
518 .or(other.message_shortcode_display),
514519 reaction_display: self.reaction_display.or(other.reaction_display),
515520 reaction_shortcode_display: self
516521 .reaction_shortcode_display
@@ -534,6 +539,7 @@ impl Tunables {
534539 fn values(self) -> TunableValues {
535540 TunableValues {
536541 log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
542 message_shortcode_display: self.message_shortcode_display.unwrap_or(false),
537543 reaction_display: self.reaction_display.unwrap_or(true),
538544 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
539545 read_receipt_send: self.read_receipt_send.unwrap_or(true),
src/message/html.rs +65 −33
@@ -148,7 +148,7 @@ impl Table {
148148 }
149149 }
150150
151 fn to_text(&self, width: usize, style: Style) -> Text {
151 fn to_text(&self, width: usize, style: Style, emoji_shortcodes: bool) -> Text {
152152 let mut text = Text::default();
153153 let columns = self.columns();
154154 let cell_total = width.saturating_sub(columns).saturating_sub(1);
@@ -166,7 +166,8 @@ impl Table {
166166
167167 if let Some(caption) = &self.caption {
168168 let subw = width.saturating_sub(6);
169 let mut printer = TextPrinter::new(subw, style, true).align(Alignment::Center);
169 let mut printer =
170 TextPrinter::new(subw, style, true, emoji_shortcodes).align(Alignment::Center);
170171 caption.print(&mut printer, style);
171172
172173 for mut line in printer.finish().lines {
@@ -213,7 +214,7 @@ impl Table {
213214 CellType::Data => style,
214215 };
215216
216 cell.to_text(*w, style)
217 cell.to_text(*w, style, emoji_shortcodes)
217218 } else {
218219 space_text(*w, style)
219220 };
@@ -274,8 +275,8 @@ pub enum StyleTreeNode {
274275}
275276
276277impl StyleTreeNode {
277 pub fn to_text(&self, width: usize, style: Style) -> Text {
278 let mut printer = TextPrinter::new(width, style, true);
278 pub fn to_text(&self, width: usize, style: Style, emoji_shortcodes: bool) -> Text {
279 let mut printer = TextPrinter::new(width, style, true, emoji_shortcodes);
279280 self.print(&mut printer, style);
280281 printer.finish()
281282 }
@@ -428,7 +429,7 @@ impl StyleTreeNode {
428429 }
429430 },
430431 StyleTreeNode::Table(table) => {
431 let text = table.to_text(width, style);
432 let text = table.to_text(width, style, printer.emoji_shortcodes());
432433 printer.push_text(text);
433434 },
434435 StyleTreeNode::Break => {
@@ -464,8 +465,14 @@ impl StyleTree {
464465 return links;
465466 }
466467
467 pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> {
468 let mut printer = TextPrinter::new(width, style, hide_reply);
468 pub fn to_text(
469 &self,
470 width: usize,
471 style: Style,
472 hide_reply: bool,
473 emoji_shortcodes: bool,
474 ) -> Text<'_> {
475 let mut printer = TextPrinter::new(width, style, hide_reply, emoji_shortcodes);
469476
470477 for child in self.children.iter() {
471478 child.print(&mut printer, style);
@@ -805,6 +812,7 @@ pub mod tests {
805812 use super::*;
806813 use crate::util::space_span;
807814 use pretty_assertions::assert_eq;
815 use unicode_width::UnicodeWidthStr;
808816
809817 #[test]
810818 fn test_header() {
@@ -812,7 +820,7 @@ pub mod tests {
812820
813821 let s = "<h1>Header 1</h1>";
814822 let tree = parse_matrix_html(s);
815 let text = tree.to_text(20, Style::default(), false);
823 let text = tree.to_text(20, Style::default(), false, false);
816824 assert_eq!(text.lines, vec![Line::from(vec![
817825 Span::styled("#", bold),
818826 Span::styled(" ", bold),
@@ -824,7 +832,7 @@ pub mod tests {
824832
825833 let s = "<h2>Header 2</h2>";
826834 let tree = parse_matrix_html(s);
827 let text = tree.to_text(20, Style::default(), false);
835 let text = tree.to_text(20, Style::default(), false, false);
828836 assert_eq!(text.lines, vec![Line::from(vec![
829837 Span::styled("#", bold),
830838 Span::styled("#", bold),
@@ -837,7 +845,7 @@ pub mod tests {
837845
838846 let s = "<h3>Header 3</h3>";
839847 let tree = parse_matrix_html(s);
840 let text = tree.to_text(20, Style::default(), false);
848 let text = tree.to_text(20, Style::default(), false, false);
841849 assert_eq!(text.lines, vec![Line::from(vec![
842850 Span::styled("#", bold),
843851 Span::styled("#", bold),
@@ -851,7 +859,7 @@ pub mod tests {
851859
852860 let s = "<h4>Header 4</h4>";
853861 let tree = parse_matrix_html(s);
854 let text = tree.to_text(20, Style::default(), false);
862 let text = tree.to_text(20, Style::default(), false, false);
855863 assert_eq!(text.lines, vec![Line::from(vec![
856864 Span::styled("#", bold),
857865 Span::styled("#", bold),
@@ -866,7 +874,7 @@ pub mod tests {
866874
867875 let s = "<h5>Header 5</h5>";
868876 let tree = parse_matrix_html(s);
869 let text = tree.to_text(20, Style::default(), false);
877 let text = tree.to_text(20, Style::default(), false, false);
870878 assert_eq!(text.lines, vec![Line::from(vec![
871879 Span::styled("#", bold),
872880 Span::styled("#", bold),
@@ -882,7 +890,7 @@ pub mod tests {
882890
883891 let s = "<h6>Header 6</h6>";
884892 let tree = parse_matrix_html(s);
885 let text = tree.to_text(20, Style::default(), false);
893 let text = tree.to_text(20, Style::default(), false, false);
886894 assert_eq!(text.lines, vec![Line::from(vec![
887895 Span::styled("#", bold),
888896 Span::styled("#", bold),
@@ -909,7 +917,7 @@ pub mod tests {
909917
910918 let s = "<b>Bold!</b>";
911919 let tree = parse_matrix_html(s);
912 let text = tree.to_text(20, Style::default(), false);
920 let text = tree.to_text(20, Style::default(), false, false);
913921 assert_eq!(text.lines, vec![Line::from(vec![
914922 Span::styled("Bold", bold),
915923 Span::styled("!", bold),
@@ -918,7 +926,7 @@ pub mod tests {
918926
919927 let s = "<strong>Bold!</strong>";
920928 let tree = parse_matrix_html(s);
921 let text = tree.to_text(20, Style::default(), false);
929 let text = tree.to_text(20, Style::default(), false, false);
922930 assert_eq!(text.lines, vec![Line::from(vec![
923931 Span::styled("Bold", bold),
924932 Span::styled("!", bold),
@@ -927,7 +935,7 @@ pub mod tests {
927935
928936 let s = "<i>Italic!</i>";
929937 let tree = parse_matrix_html(s);
930 let text = tree.to_text(20, Style::default(), false);
938 let text = tree.to_text(20, Style::default(), false, false);
931939 assert_eq!(text.lines, vec![Line::from(vec![
932940 Span::styled("Italic", italic),
933941 Span::styled("!", italic),
@@ -936,7 +944,7 @@ pub mod tests {
936944
937945 let s = "<em>Italic!</em>";
938946 let tree = parse_matrix_html(s);
939 let text = tree.to_text(20, Style::default(), false);
947 let text = tree.to_text(20, Style::default(), false, false);
940948 assert_eq!(text.lines, vec![Line::from(vec![
941949 Span::styled("Italic", italic),
942950 Span::styled("!", italic),
@@ -945,7 +953,7 @@ pub mod tests {
945953
946954 let s = "<del>Strikethrough!</del>";
947955 let tree = parse_matrix_html(s);
948 let text = tree.to_text(20, Style::default(), false);
956 let text = tree.to_text(20, Style::default(), false, false);
949957 assert_eq!(text.lines, vec![Line::from(vec![
950958 Span::styled("Strikethrough", strike),
951959 Span::styled("!", strike),
@@ -954,7 +962,7 @@ pub mod tests {
954962
955963 let s = "<strike>Strikethrough!</strike>";
956964 let tree = parse_matrix_html(s);
957 let text = tree.to_text(20, Style::default(), false);
965 let text = tree.to_text(20, Style::default(), false, false);
958966 assert_eq!(text.lines, vec![Line::from(vec![
959967 Span::styled("Strikethrough", strike),
960968 Span::styled("!", strike),
@@ -963,7 +971,7 @@ pub mod tests {
963971
964972 let s = "<u>Underline!</u>";
965973 let tree = parse_matrix_html(s);
966 let text = tree.to_text(20, Style::default(), false);
974 let text = tree.to_text(20, Style::default(), false, false);
967975 assert_eq!(text.lines, vec![Line::from(vec![
968976 Span::styled("Underline", underl),
969977 Span::styled("!", underl),
@@ -972,7 +980,7 @@ pub mod tests {
972980
973981 let s = "<font color=\"#ff0000\">Red!</u>";
974982 let tree = parse_matrix_html(s);
975 let text = tree.to_text(20, Style::default(), false);
983 let text = tree.to_text(20, Style::default(), false, false);
976984 assert_eq!(text.lines, vec![Line::from(vec![
977985 Span::styled("Red", red),
978986 Span::styled("!", red),
@@ -981,7 +989,7 @@ pub mod tests {
981989
982990 let s = "<font color=\"red\">Red!</u>";
983991 let tree = parse_matrix_html(s);
984 let text = tree.to_text(20, Style::default(), false);
992 let text = tree.to_text(20, Style::default(), false, false);
985993 assert_eq!(text.lines, vec![Line::from(vec![
986994 Span::styled("Red", red),
987995 Span::styled("!", red),
@@ -993,7 +1001,7 @@ pub mod tests {
9931001 fn test_paragraph() {
9941002 let s = "<p>Hello world!</p><p>Content</p><p>Goodbye world!</p>";
9951003 let tree = parse_matrix_html(s);
996 let text = tree.to_text(10, Style::default(), false);
1004 let text = tree.to_text(10, Style::default(), false, false);
9971005 assert_eq!(text.lines.len(), 7);
9981006 assert_eq!(
9991007 text.lines[0],
@@ -1020,7 +1028,7 @@ pub mod tests {
10201028 fn test_blockquote() {
10211029 let s = "<blockquote>Hello world!</blockquote>";
10221030 let tree = parse_matrix_html(s);
1023 let text = tree.to_text(10, Style::default(), false);
1031 let text = tree.to_text(10, Style::default(), false, false);
10241032 assert_eq!(text.lines.len(), 2);
10251033 assert_eq!(
10261034 text.lines[0],
@@ -1036,7 +1044,7 @@ pub mod tests {
10361044 fn test_list_unordered() {
10371045 let s = "<ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul>";
10381046 let tree = parse_matrix_html(s);
1039 let text = tree.to_text(8, Style::default(), false);
1047 let text = tree.to_text(8, Style::default(), false, false);
10401048 assert_eq!(text.lines.len(), 6);
10411049 assert_eq!(
10421050 text.lines[0],
@@ -1098,7 +1106,7 @@ pub mod tests {
10981106 fn test_list_ordered() {
10991107 let s = "<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ol>";
11001108 let tree = parse_matrix_html(s);
1101 let text = tree.to_text(9, Style::default(), false);
1109 let text = tree.to_text(9, Style::default(), false, false);
11021110 assert_eq!(text.lines.len(), 6);
11031111 assert_eq!(
11041112 text.lines[0],
@@ -1168,7 +1176,7 @@ pub mod tests {
11681176 <tr><td>a</td><td>b</td><td>c</td></tr>\
11691177 </tbody></table>";
11701178 let tree = parse_matrix_html(s);
1171 let text = tree.to_text(15, Style::default(), false);
1179 let text = tree.to_text(15, Style::default(), false, false);
11721180 let bold = Style::default().add_modifier(StyleModifier::BOLD);
11731181 assert_eq!(text.lines.len(), 11);
11741182
@@ -1261,7 +1269,7 @@ pub mod tests {
12611269 let s = "<mx-reply>This was replied to</mx-reply>This is the reply";
12621270
12631271 let tree = parse_matrix_html(s);
1264 let text = tree.to_text(10, Style::default(), false);
1272 let text = tree.to_text(10, Style::default(), false, false);
12651273 assert_eq!(text.lines.len(), 4);
12661274 assert_eq!(
12671275 text.lines[0],
@@ -1298,7 +1306,7 @@ pub mod tests {
12981306 );
12991307
13001308 let tree = parse_matrix_html(s);
1301 let text = tree.to_text(10, Style::default(), true);
1309 let text = tree.to_text(10, Style::default(), true, false);
13021310 assert_eq!(text.lines.len(), 2);
13031311 assert_eq!(
13041312 text.lines[0],
@@ -1325,7 +1333,7 @@ pub mod tests {
13251333 fn test_self_closing() {
13261334 let s = "Hello<br>World<br>Goodbye";
13271335 let tree = parse_matrix_html(s);
1328 let text = tree.to_text(7, Style::default(), true);
1336 let text = tree.to_text(7, Style::default(), true, false);
13291337 assert_eq!(text.lines.len(), 3);
13301338 assert_eq!(text.lines[0], Line::from(vec![Span::raw("Hello"), Span::raw(" "),]));
13311339 assert_eq!(text.lines[1], Line::from(vec![Span::raw("World"), Span::raw(" "),]));
@@ -1336,7 +1344,7 @@ pub mod tests {
13361344 fn test_embedded_newline() {
13371345 let s = "<p>Hello\nWorld</p>";
13381346 let tree = parse_matrix_html(s);
1339 let text = tree.to_text(15, Style::default(), true);
1347 let text = tree.to_text(15, Style::default(), true, false);
13401348 assert_eq!(text.lines.len(), 1);
13411349 assert_eq!(
13421350 text.lines[0],
@@ -1359,7 +1367,7 @@ pub mod tests {
13591367 "</code></pre>\n"
13601368 );
13611369 let tree = parse_matrix_html(s);
1362 let text = tree.to_text(25, Style::default(), true);
1370 let text = tree.to_text(25, Style::default(), true, false);
13631371 assert_eq!(text.lines.len(), 5);
13641372 assert_eq!(
13651373 text.lines[0],
@@ -1420,4 +1428,28 @@ pub mod tests {
14201428 ])
14211429 );
14221430 }
1431
1432 #[test]
1433 fn test_emoji_shortcodes() {
1434 for shortcode in ["exploding_head", "polar_bear", "canada"] {
1435 let emoji = emojis::get_by_shortcode(shortcode).unwrap().as_str();
1436 let emoji_width = UnicodeWidthStr::width(emoji);
1437 let replacement = format!(":{shortcode}:");
1438 let replacement_width = UnicodeWidthStr::width(replacement.as_str());
1439 let s = format!("<p>{emoji}</p>");
1440 let tree = parse_matrix_html(s.as_str());
1441 // Test with emojis_shortcodes set to false
1442 let text = tree.to_text(20, Style::default(), false, false);
1443 assert_eq!(text.lines, vec![Line::from(vec![
1444 Span::raw(emoji),
1445 space_span(20 - emoji_width, Style::default()),
1446 ]),]);
1447 // Test with emojis_shortcodes set to true
1448 let text = tree.to_text(20, Style::default(), false, true);
1449 assert_eq!(text.lines, vec![Line::from(vec![
1450 Span::raw(replacement.as_str()),
1451 space_span(20 - replacement_width, Style::default()),
1452 ])]);
1453 }
1454 }
14231455}
src/message/mod.rs +26 −7
@@ -61,7 +61,7 @@ use crate::{
6161 base::RoomInfo,
6262 config::ApplicationSettings,
6363 message::html::{parse_matrix_html, StyleTree},
64 util::{space, space_span, take_width, wrapped_text},
64 util::{replace_emojis_in_str, space, space_span, take_width, wrapped_text},
6565};
6666
6767mod html;
@@ -834,7 +834,8 @@ impl Message {
834834
835835 if let Some(r) = &reply {
836836 let w = width.saturating_sub(2);
837 let mut replied = r.show_msg(w, style, true);
837 let mut replied =
838 r.show_msg(w, style, true, settings.tunables.message_shortcode_display);
838839 let mut sender = r.sender_span(info, settings);
839840 let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
840841 let trailing = w.saturating_sub(sender_width + 1);
@@ -862,7 +863,12 @@ impl Message {
862863 }
863864
864865 // Now show the message contents, and the inlined reply if we couldn't find it above.
865 let msg = self.show_msg(width, style, reply.is_some());
866 let msg = self.show_msg(
867 width,
868 style,
869 reply.is_some(),
870 settings.tunables.message_shortcode_display,
871 );
866872 fmt.push_text(msg, style, &mut text);
867873
868874 if text.lines.is_empty() {
@@ -871,7 +877,9 @@ impl Message {
871877 }
872878
873879 if settings.tunables.reaction_display {
874 let mut emojis = printer::TextPrinter::new(width, style, false);
880 // Pass false for emoji_shortcodes parameter because we handle shortcodes ourselves
881 // before pushing to the printer.
882 let mut emojis = printer::TextPrinter::new(width, style, false, false);
875883 let mut reactions = 0;
876884
877885 for (key, count) in info.get_reactions(self.event.event_id()).into_iter() {
@@ -917,7 +925,9 @@ impl Message {
917925
918926 if len > 0 {
919927 let style = Style::default();
920 let mut threaded = printer::TextPrinter::new(width, style, false).literal(true);
928 let emoji_shortcodes = settings.tunables.message_shortcode_display;
929 let mut threaded =
930 printer::TextPrinter::new(width, style, false, emoji_shortcodes).literal(true);
921931 let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD));
922932 threaded.push_str(" \u{2937} ", style);
923933 threaded.push_span_nobreak(len);
@@ -929,11 +939,20 @@ impl Message {
929939 text
930940 }
931941
932 pub fn show_msg(&self, width: usize, style: Style, hide_reply: bool) -> Text {
942 pub fn show_msg(
943 &self,
944 width: usize,
945 style: Style,
946 hide_reply: bool,
947 emoji_shortcodes: bool,
948 ) -> Text {
933949 if let Some(html) = &self.html {
934 html.to_text(width, style, hide_reply)
950 html.to_text(width, style, hide_reply, emoji_shortcodes)
935951 } else {
936952 let mut msg = self.event.body();
953 if emoji_shortcodes {
954 msg = Cow::Owned(replace_emojis_in_str(msg.as_ref()));
955 }
937956
938957 if self.downloaded {
939958 msg.to_mut().push_str(" \u{2705}");
src/message/printer.rs +39 −9
@@ -11,7 +11,13 @@ use ratatui::text::{Line, Span, Text};
1111use unicode_segmentation::UnicodeSegmentation;
1212use unicode_width::UnicodeWidthStr;
1313
14use crate::util::{space_span, take_width};
14use crate::util::{
15 replace_emojis_in_line,
16 replace_emojis_in_span,
17 replace_emojis_in_str,
18 space_span,
19 take_width,
20};
1521
1622/// Wrap styled text for the current terminal width.
1723pub struct TextPrinter<'a> {
@@ -19,6 +25,7 @@ pub struct TextPrinter<'a> {
1925 width: usize,
2026 base_style: Style,
2127 hide_reply: bool,
28 emoji_shortcodes: bool,
2229
2330 alignment: Alignment,
2431 curr_spans: Vec<Span<'a>>,
@@ -28,12 +35,13 @@ pub struct TextPrinter<'a> {
2835
2936impl<'a> TextPrinter<'a> {
3037 /// Create a new printer.
31 pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
38 pub fn new(width: usize, base_style: Style, hide_reply: bool, emoji_shortcodes: bool) -> Self {
3239 TextPrinter {
3340 text: Text::default(),
3441 width,
3542 base_style,
3643 hide_reply,
44 emoji_shortcodes,
3745
3846 alignment: Alignment::Left,
3947 curr_spans: vec![],
@@ -59,6 +67,11 @@ impl<'a> TextPrinter<'a> {
5967 self.hide_reply
6068 }
6169
70 /// Indicates whether emojis should be replaced by shortcodes
71 pub fn emoji_shortcodes(&self) -> bool {
72 self.emoji_shortcodes
73 }
74
6275 /// Indicates the current printer's width.
6376 pub fn width(&self) -> usize {
6477 self.width
@@ -71,6 +84,7 @@ impl<'a> TextPrinter<'a> {
7184 width: self.width.saturating_sub(indent),
7285 base_style: self.base_style,
7386 hide_reply: self.hide_reply,
87 emoji_shortcodes: self.emoji_shortcodes,
7488
7589 alignment: self.alignment,
7690 curr_spans: vec![],
@@ -164,7 +178,10 @@ impl<'a> TextPrinter<'a> {
164178 }
165179
166180 /// Push a [Span] that isn't allowed to break across lines.
167 pub fn push_span_nobreak(&mut self, span: Span<'a>) {
181 pub fn push_span_nobreak(&mut self, mut span: Span<'a>) {
182 if self.emoji_shortcodes {
183 replace_emojis_in_span(&mut span);
184 }
168185 let sw = UnicodeWidthStr::width(span.content.as_ref());
169186
170187 if self.curr_width + sw > self.width {
@@ -200,10 +217,15 @@ impl<'a> TextPrinter<'a> {
200217 continue;
201218 }
202219
203 let sw = UnicodeWidthStr::width(word);
220 let cow = if self.emoji_shortcodes {
221 Cow::Owned(replace_emojis_in_str(word))
222 } else {
223 Cow::Borrowed(word)
224 };
225 let sw = UnicodeWidthStr::width(cow.as_ref());
204226
205227 if sw > self.width {
206 self.push_str_wrapped(word, style);
228 self.push_str_wrapped(cow, style);
207229 continue;
208230 }
209231
@@ -211,13 +233,13 @@ impl<'a> TextPrinter<'a> {
211233 // Word doesn't fit on this line, so start a new one.
212234 self.commit();
213235
214 if !self.literal && word.chars().all(char::is_whitespace) {
236 if !self.literal && cow.chars().all(char::is_whitespace) {
215237 // Drop leading whitespace.
216238 continue;
217239 }
218240 }
219241
220 let span = Span::styled(word, style);
242 let span = Span::styled(cow, style);
221243 self.curr_spans.push(span);
222244 self.curr_width += sw;
223245 }
@@ -229,14 +251,22 @@ impl<'a> TextPrinter<'a> {
229251 }
230252
231253 /// Push a [Line] into the printer.
232 pub fn push_line(&mut self, line: Line<'a>) {
254 pub fn push_line(&mut self, mut line: Line<'a>) {
233255 self.commit();
256 if self.emoji_shortcodes {
257 replace_emojis_in_line(&mut line);
258 }
234259 self.text.lines.push(line);
235260 }
236261
237262 /// Push multiline [Text] into the printer.
238 pub fn push_text(&mut self, text: Text<'a>) {
263 pub fn push_text(&mut self, mut text: Text<'a>) {
239264 self.commit();
265 if self.emoji_shortcodes {
266 for line in &mut text.lines {
267 replace_emojis_in_line(line);
268 }
269 }
240270 self.text.lines.extend(text.lines);
241271 }
242272
src/tests.rs +1
@@ -183,6 +183,7 @@ pub fn mock_tunables() -> TunableValues {
183183 TunableValues {
184184 default_room: None,
185185 log_level: Level::INFO,
186 message_shortcode_display: false,
186187 reaction_display: true,
187188 reaction_shortcode_display: false,
188189 read_receipt_send: true,
src/util.rs +22
@@ -147,6 +147,28 @@ pub fn join_cell_text<'a>(texts: Vec<(Text<'a>, usize)>, join: Span<'a>, style:
147147 text
148148}
149149
150fn replace_emoji_in_grapheme(grapheme: &str) -> String {
151 emojis::get(grapheme)
152 .and_then(|emoji| emoji.shortcode())
153 .map(|shortcode| format!(":{shortcode}:"))
154 .unwrap_or_else(|| grapheme.to_owned())
155}
156
157pub fn replace_emojis_in_str(s: &str) -> String {
158 let graphemes = s.graphemes(true);
159 graphemes.map(replace_emoji_in_grapheme).collect()
160}
161
162pub fn replace_emojis_in_span(span: &mut Span) {
163 span.content = Cow::Owned(replace_emojis_in_str(span.content.as_ref()))
164}
165
166pub fn replace_emojis_in_line(line: &mut Line) {
167 for span in &mut line.spans {
168 replace_emojis_in_span(span);
169 }
170}
171
150172#[cfg(test)]
151173pub mod tests {
152174 use super::*;