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 @@
10 }, 10 },
11 "settings": { 11 "settings": {
12 "log_level": "warn", 12 "log_level": "warn",
13 "message_shortcode_display": false,
13 "reaction_display": true, 14 "reaction_display": true,
14 "reaction_shortcode_display": false, 15 "reaction_shortcode_display": false,
15 "read_receipt_send": true, 16 "read_receipt_send": true,
docs/iamb.5.md +4
@@ -53,6 +53,10 @@ overridden as described in *PROFILES*.
53> Specifies the lowest log level that should be shown. Possible values 53> Specifies the lowest log level that should be shown. Possible values
54> are: _trace_, _debug_, _info_, _warn_, and _error_. 54> are: _trace_, _debug_, _info_, _warn_, and _error_.
55 55
56**message_shortcode_display** (type: boolean)
57> Defines whether or not emoji characters in messages should be replaced by
58> their respective shortcodes.
59
56**reaction_display** (type: boolean) 60**reaction_display** (type: boolean)
57> Defines whether or not reactions should be shown. 61> Defines whether or not reactions should be shown.
58 62
src/config.rs +6
@@ -469,6 +469,7 @@ impl SortOverrides {
469#[derive(Clone)] 469#[derive(Clone)]
470pub struct TunableValues { 470pub struct TunableValues {
471 pub log_level: Level, 471 pub log_level: Level,
472 pub message_shortcode_display: bool,
472 pub reaction_display: bool, 473 pub reaction_display: bool,
473 pub reaction_shortcode_display: bool, 474 pub reaction_shortcode_display: bool,
474 pub read_receipt_send: bool, 475 pub read_receipt_send: bool,
@@ -489,6 +490,7 @@ pub struct TunableValues {
489#[derive(Clone, Default, Deserialize)] 490#[derive(Clone, Default, Deserialize)]
490pub struct Tunables { 491pub struct Tunables {
491 pub log_level: Option<LogLevel>, 492 pub log_level: Option<LogLevel>,
493 pub message_shortcode_display: Option<bool>,
492 pub reaction_display: Option<bool>, 494 pub reaction_display: Option<bool>,
493 pub reaction_shortcode_display: Option<bool>, 495 pub reaction_shortcode_display: Option<bool>,
494 pub read_receipt_send: Option<bool>, 496 pub read_receipt_send: Option<bool>,
@@ -511,6 +513,9 @@ impl Tunables {
511 fn merge(self, other: Self) -> Self { 513 fn merge(self, other: Self) -> Self {
512 Tunables { 514 Tunables {
513 log_level: self.log_level.or(other.log_level), 515 log_level: self.log_level.or(other.log_level),
516 message_shortcode_display: self
517 .message_shortcode_display
518 .or(other.message_shortcode_display),
514 reaction_display: self.reaction_display.or(other.reaction_display), 519 reaction_display: self.reaction_display.or(other.reaction_display),
515 reaction_shortcode_display: self 520 reaction_shortcode_display: self
516 .reaction_shortcode_display 521 .reaction_shortcode_display
@@ -534,6 +539,7 @@ impl Tunables {
534 fn values(self) -> TunableValues { 539 fn values(self) -> TunableValues {
535 TunableValues { 540 TunableValues {
536 log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO), 541 log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
542 message_shortcode_display: self.message_shortcode_display.unwrap_or(false),
537 reaction_display: self.reaction_display.unwrap_or(true), 543 reaction_display: self.reaction_display.unwrap_or(true),
538 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false), 544 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
539 read_receipt_send: self.read_receipt_send.unwrap_or(true), 545 read_receipt_send: self.read_receipt_send.unwrap_or(true),
src/message/html.rs +65 −33
@@ -148,7 +148,7 @@ impl Table {
148 } 148 }
149 } 149 }
150 150
151 fn to_text(&self, width: usize, style: Style) -> Text { 151 fn to_text(&self, width: usize, style: Style, emoji_shortcodes: bool) -> Text {
152 let mut text = Text::default(); 152 let mut text = Text::default();
153 let columns = self.columns(); 153 let columns = self.columns();
154 let cell_total = width.saturating_sub(columns).saturating_sub(1); 154 let cell_total = width.saturating_sub(columns).saturating_sub(1);
@@ -166,7 +166,8 @@ impl Table {
166 166
167 if let Some(caption) = &self.caption { 167 if let Some(caption) = &self.caption {
168 let subw = width.saturating_sub(6); 168 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);
170 caption.print(&mut printer, style); 171 caption.print(&mut printer, style);
171 172
172 for mut line in printer.finish().lines { 173 for mut line in printer.finish().lines {
@@ -213,7 +214,7 @@ impl Table {
213 CellType::Data => style, 214 CellType::Data => style,
214 }; 215 };
215 216
216 cell.to_text(*w, style) 217 cell.to_text(*w, style, emoji_shortcodes)
217 } else { 218 } else {
218 space_text(*w, style) 219 space_text(*w, style)
219 }; 220 };
@@ -274,8 +275,8 @@ pub enum StyleTreeNode {
274} 275}
275 276
276impl StyleTreeNode { 277impl StyleTreeNode {
277 pub fn to_text(&self, width: usize, style: Style) -> Text { 278 pub fn to_text(&self, width: usize, style: Style, emoji_shortcodes: bool) -> Text {
278 let mut printer = TextPrinter::new(width, style, true); 279 let mut printer = TextPrinter::new(width, style, true, emoji_shortcodes);
279 self.print(&mut printer, style); 280 self.print(&mut printer, style);
280 printer.finish() 281 printer.finish()
281 } 282 }
@@ -428,7 +429,7 @@ impl StyleTreeNode {
428 } 429 }
429 }, 430 },
430 StyleTreeNode::Table(table) => { 431 StyleTreeNode::Table(table) => {
431 let text = table.to_text(width, style); 432 let text = table.to_text(width, style, printer.emoji_shortcodes());
432 printer.push_text(text); 433 printer.push_text(text);
433 }, 434 },
434 StyleTreeNode::Break => { 435 StyleTreeNode::Break => {
@@ -464,8 +465,14 @@ impl StyleTree {
464 return links; 465 return links;
465 } 466 }
466 467
467 pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> { 468 pub fn to_text(
468 let mut printer = TextPrinter::new(width, style, hide_reply); 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);
469 476
470 for child in self.children.iter() { 477 for child in self.children.iter() {
471 child.print(&mut printer, style); 478 child.print(&mut printer, style);
@@ -805,6 +812,7 @@ pub mod tests {
805 use super::*; 812 use super::*;
806 use crate::util::space_span; 813 use crate::util::space_span;
807 use pretty_assertions::assert_eq; 814 use pretty_assertions::assert_eq;
815 use unicode_width::UnicodeWidthStr;
808 816
809 #[test] 817 #[test]
810 fn test_header() { 818 fn test_header() {
@@ -812,7 +820,7 @@ pub mod tests {
812 820
813 let s = "<h1>Header 1</h1>"; 821 let s = "<h1>Header 1</h1>";
814 let tree = parse_matrix_html(s); 822 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);
816 assert_eq!(text.lines, vec![Line::from(vec![ 824 assert_eq!(text.lines, vec![Line::from(vec![
817 Span::styled("#", bold), 825 Span::styled("#", bold),
818 Span::styled(" ", bold), 826 Span::styled(" ", bold),
@@ -824,7 +832,7 @@ pub mod tests {
824 832
825 let s = "<h2>Header 2</h2>"; 833 let s = "<h2>Header 2</h2>";
826 let tree = parse_matrix_html(s); 834 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);
828 assert_eq!(text.lines, vec![Line::from(vec![ 836 assert_eq!(text.lines, vec![Line::from(vec![
829 Span::styled("#", bold), 837 Span::styled("#", bold),
830 Span::styled("#", bold), 838 Span::styled("#", bold),
@@ -837,7 +845,7 @@ pub mod tests {
837 845
838 let s = "<h3>Header 3</h3>"; 846 let s = "<h3>Header 3</h3>";
839 let tree = parse_matrix_html(s); 847 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);
841 assert_eq!(text.lines, vec![Line::from(vec![ 849 assert_eq!(text.lines, vec![Line::from(vec![
842 Span::styled("#", bold), 850 Span::styled("#", bold),
843 Span::styled("#", bold), 851 Span::styled("#", bold),
@@ -851,7 +859,7 @@ pub mod tests {
851 859
852 let s = "<h4>Header 4</h4>"; 860 let s = "<h4>Header 4</h4>";
853 let tree = parse_matrix_html(s); 861 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);
855 assert_eq!(text.lines, vec![Line::from(vec![ 863 assert_eq!(text.lines, vec![Line::from(vec![
856 Span::styled("#", bold), 864 Span::styled("#", bold),
857 Span::styled("#", bold), 865 Span::styled("#", bold),
@@ -866,7 +874,7 @@ pub mod tests {
866 874
867 let s = "<h5>Header 5</h5>"; 875 let s = "<h5>Header 5</h5>";
868 let tree = parse_matrix_html(s); 876 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);
870 assert_eq!(text.lines, vec![Line::from(vec![ 878 assert_eq!(text.lines, vec![Line::from(vec![
871 Span::styled("#", bold), 879 Span::styled("#", bold),
872 Span::styled("#", bold), 880 Span::styled("#", bold),
@@ -882,7 +890,7 @@ pub mod tests {
882 890
883 let s = "<h6>Header 6</h6>"; 891 let s = "<h6>Header 6</h6>";
884 let tree = parse_matrix_html(s); 892 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);
886 assert_eq!(text.lines, vec![Line::from(vec![ 894 assert_eq!(text.lines, vec![Line::from(vec![
887 Span::styled("#", bold), 895 Span::styled("#", bold),
888 Span::styled("#", bold), 896 Span::styled("#", bold),
@@ -909,7 +917,7 @@ pub mod tests {
909 917
910 let s = "<b>Bold!</b>"; 918 let s = "<b>Bold!</b>";
911 let tree = parse_matrix_html(s); 919 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);
913 assert_eq!(text.lines, vec![Line::from(vec![ 921 assert_eq!(text.lines, vec![Line::from(vec![
914 Span::styled("Bold", bold), 922 Span::styled("Bold", bold),
915 Span::styled("!", bold), 923 Span::styled("!", bold),
@@ -918,7 +926,7 @@ pub mod tests {
918 926
919 let s = "<strong>Bold!</strong>"; 927 let s = "<strong>Bold!</strong>";
920 let tree = parse_matrix_html(s); 928 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);
922 assert_eq!(text.lines, vec![Line::from(vec![ 930 assert_eq!(text.lines, vec![Line::from(vec![
923 Span::styled("Bold", bold), 931 Span::styled("Bold", bold),
924 Span::styled("!", bold), 932 Span::styled("!", bold),
@@ -927,7 +935,7 @@ pub mod tests {
927 935
928 let s = "<i>Italic!</i>"; 936 let s = "<i>Italic!</i>";
929 let tree = parse_matrix_html(s); 937 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);
931 assert_eq!(text.lines, vec![Line::from(vec![ 939 assert_eq!(text.lines, vec![Line::from(vec![
932 Span::styled("Italic", italic), 940 Span::styled("Italic", italic),
933 Span::styled("!", italic), 941 Span::styled("!", italic),
@@ -936,7 +944,7 @@ pub mod tests {
936 944
937 let s = "<em>Italic!</em>"; 945 let s = "<em>Italic!</em>";
938 let tree = parse_matrix_html(s); 946 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);
940 assert_eq!(text.lines, vec![Line::from(vec![ 948 assert_eq!(text.lines, vec![Line::from(vec![
941 Span::styled("Italic", italic), 949 Span::styled("Italic", italic),
942 Span::styled("!", italic), 950 Span::styled("!", italic),
@@ -945,7 +953,7 @@ pub mod tests {
945 953
946 let s = "<del>Strikethrough!</del>"; 954 let s = "<del>Strikethrough!</del>";
947 let tree = parse_matrix_html(s); 955 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);
949 assert_eq!(text.lines, vec![Line::from(vec![ 957 assert_eq!(text.lines, vec![Line::from(vec![
950 Span::styled("Strikethrough", strike), 958 Span::styled("Strikethrough", strike),
951 Span::styled("!", strike), 959 Span::styled("!", strike),
@@ -954,7 +962,7 @@ pub mod tests {
954 962
955 let s = "<strike>Strikethrough!</strike>"; 963 let s = "<strike>Strikethrough!</strike>";
956 let tree = parse_matrix_html(s); 964 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);
958 assert_eq!(text.lines, vec![Line::from(vec![ 966 assert_eq!(text.lines, vec![Line::from(vec![
959 Span::styled("Strikethrough", strike), 967 Span::styled("Strikethrough", strike),
960 Span::styled("!", strike), 968 Span::styled("!", strike),
@@ -963,7 +971,7 @@ pub mod tests {
963 971
964 let s = "<u>Underline!</u>"; 972 let s = "<u>Underline!</u>";
965 let tree = parse_matrix_html(s); 973 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);
967 assert_eq!(text.lines, vec![Line::from(vec![ 975 assert_eq!(text.lines, vec![Line::from(vec![
968 Span::styled("Underline", underl), 976 Span::styled("Underline", underl),
969 Span::styled("!", underl), 977 Span::styled("!", underl),
@@ -972,7 +980,7 @@ pub mod tests {
972 980
973 let s = "<font color=\"#ff0000\">Red!</u>"; 981 let s = "<font color=\"#ff0000\">Red!</u>";
974 let tree = parse_matrix_html(s); 982 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);
976 assert_eq!(text.lines, vec![Line::from(vec![ 984 assert_eq!(text.lines, vec![Line::from(vec![
977 Span::styled("Red", red), 985 Span::styled("Red", red),
978 Span::styled("!", red), 986 Span::styled("!", red),
@@ -981,7 +989,7 @@ pub mod tests {
981 989
982 let s = "<font color=\"red\">Red!</u>"; 990 let s = "<font color=\"red\">Red!</u>";
983 let tree = parse_matrix_html(s); 991 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);
985 assert_eq!(text.lines, vec![Line::from(vec![ 993 assert_eq!(text.lines, vec![Line::from(vec![
986 Span::styled("Red", red), 994 Span::styled("Red", red),
987 Span::styled("!", red), 995 Span::styled("!", red),
@@ -993,7 +1001,7 @@ pub mod tests {
993 fn test_paragraph() { 1001 fn test_paragraph() {
994 let s = "<p>Hello world!</p><p>Content</p><p>Goodbye world!</p>"; 1002 let s = "<p>Hello world!</p><p>Content</p><p>Goodbye world!</p>";
995 let tree = parse_matrix_html(s); 1003 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);
997 assert_eq!(text.lines.len(), 7); 1005 assert_eq!(text.lines.len(), 7);
998 assert_eq!( 1006 assert_eq!(
999 text.lines[0], 1007 text.lines[0],
@@ -1020,7 +1028,7 @@ pub mod tests {
1020 fn test_blockquote() { 1028 fn test_blockquote() {
1021 let s = "<blockquote>Hello world!</blockquote>"; 1029 let s = "<blockquote>Hello world!</blockquote>";
1022 let tree = parse_matrix_html(s); 1030 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);
1024 assert_eq!(text.lines.len(), 2); 1032 assert_eq!(text.lines.len(), 2);
1025 assert_eq!( 1033 assert_eq!(
1026 text.lines[0], 1034 text.lines[0],
@@ -1036,7 +1044,7 @@ pub mod tests {
1036 fn test_list_unordered() { 1044 fn test_list_unordered() {
1037 let s = "<ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul>"; 1045 let s = "<ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul>";
1038 let tree = parse_matrix_html(s); 1046 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);
1040 assert_eq!(text.lines.len(), 6); 1048 assert_eq!(text.lines.len(), 6);
1041 assert_eq!( 1049 assert_eq!(
1042 text.lines[0], 1050 text.lines[0],
@@ -1098,7 +1106,7 @@ pub mod tests {
1098 fn test_list_ordered() { 1106 fn test_list_ordered() {
1099 let s = "<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ol>"; 1107 let s = "<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ol>";
1100 let tree = parse_matrix_html(s); 1108 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);
1102 assert_eq!(text.lines.len(), 6); 1110 assert_eq!(text.lines.len(), 6);
1103 assert_eq!( 1111 assert_eq!(
1104 text.lines[0], 1112 text.lines[0],
@@ -1168,7 +1176,7 @@ pub mod tests {
1168 <tr><td>a</td><td>b</td><td>c</td></tr>\ 1176 <tr><td>a</td><td>b</td><td>c</td></tr>\
1169 </tbody></table>"; 1177 </tbody></table>";
1170 let tree = parse_matrix_html(s); 1178 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);
1172 let bold = Style::default().add_modifier(StyleModifier::BOLD); 1180 let bold = Style::default().add_modifier(StyleModifier::BOLD);
1173 assert_eq!(text.lines.len(), 11); 1181 assert_eq!(text.lines.len(), 11);
1174 1182
@@ -1261,7 +1269,7 @@ pub mod tests {
1261 let s = "<mx-reply>This was replied to</mx-reply>This is the reply"; 1269 let s = "<mx-reply>This was replied to</mx-reply>This is the reply";
1262 1270
1263 let tree = parse_matrix_html(s); 1271 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);
1265 assert_eq!(text.lines.len(), 4); 1273 assert_eq!(text.lines.len(), 4);
1266 assert_eq!( 1274 assert_eq!(
1267 text.lines[0], 1275 text.lines[0],
@@ -1298,7 +1306,7 @@ pub mod tests {
1298 ); 1306 );
1299 1307
1300 let tree = parse_matrix_html(s); 1308 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);
1302 assert_eq!(text.lines.len(), 2); 1310 assert_eq!(text.lines.len(), 2);
1303 assert_eq!( 1311 assert_eq!(
1304 text.lines[0], 1312 text.lines[0],
@@ -1325,7 +1333,7 @@ pub mod tests {
1325 fn test_self_closing() { 1333 fn test_self_closing() {
1326 let s = "Hello<br>World<br>Goodbye"; 1334 let s = "Hello<br>World<br>Goodbye";
1327 let tree = parse_matrix_html(s); 1335 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);
1329 assert_eq!(text.lines.len(), 3); 1337 assert_eq!(text.lines.len(), 3);
1330 assert_eq!(text.lines[0], Line::from(vec![Span::raw("Hello"), Span::raw(" "),])); 1338 assert_eq!(text.lines[0], Line::from(vec![Span::raw("Hello"), Span::raw(" "),]));
1331 assert_eq!(text.lines[1], Line::from(vec![Span::raw("World"), Span::raw(" "),])); 1339 assert_eq!(text.lines[1], Line::from(vec![Span::raw("World"), Span::raw(" "),]));
@@ -1336,7 +1344,7 @@ pub mod tests {
1336 fn test_embedded_newline() { 1344 fn test_embedded_newline() {
1337 let s = "<p>Hello\nWorld</p>"; 1345 let s = "<p>Hello\nWorld</p>";
1338 let tree = parse_matrix_html(s); 1346 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);
1340 assert_eq!(text.lines.len(), 1); 1348 assert_eq!(text.lines.len(), 1);
1341 assert_eq!( 1349 assert_eq!(
1342 text.lines[0], 1350 text.lines[0],
@@ -1359,7 +1367,7 @@ pub mod tests {
1359 "</code></pre>\n" 1367 "</code></pre>\n"
1360 ); 1368 );
1361 let tree = parse_matrix_html(s); 1369 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);
1363 assert_eq!(text.lines.len(), 5); 1371 assert_eq!(text.lines.len(), 5);
1364 assert_eq!( 1372 assert_eq!(
1365 text.lines[0], 1373 text.lines[0],
@@ -1420,4 +1428,28 @@ pub mod tests {
1420 ]) 1428 ])
1421 ); 1429 );
1422 } 1430 }
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 }
1423} 1455}
src/message/mod.rs +26 −7
@@ -61,7 +61,7 @@ use crate::{
61 base::RoomInfo, 61 base::RoomInfo,
62 config::ApplicationSettings, 62 config::ApplicationSettings,
63 message::html::{parse_matrix_html, StyleTree}, 63 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},
65}; 65};
66 66
67mod html; 67mod html;
@@ -834,7 +834,8 @@ impl Message {
834 834
835 if let Some(r) = &reply { 835 if let Some(r) = &reply {
836 let w = width.saturating_sub(2); 836 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);
838 let mut sender = r.sender_span(info, settings); 839 let mut sender = r.sender_span(info, settings);
839 let sender_width = UnicodeWidthStr::width(sender.content.as_ref()); 840 let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
840 let trailing = w.saturating_sub(sender_width + 1); 841 let trailing = w.saturating_sub(sender_width + 1);
@@ -862,7 +863,12 @@ impl Message {
862 } 863 }
863 864
864 // Now show the message contents, and the inlined reply if we couldn't find it above. 865 // 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 );
866 fmt.push_text(msg, style, &mut text); 872 fmt.push_text(msg, style, &mut text);
867 873
868 if text.lines.is_empty() { 874 if text.lines.is_empty() {
@@ -871,7 +877,9 @@ impl Message {
871 } 877 }
872 878
873 if settings.tunables.reaction_display { 879 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);
875 let mut reactions = 0; 883 let mut reactions = 0;
876 884
877 for (key, count) in info.get_reactions(self.event.event_id()).into_iter() { 885 for (key, count) in info.get_reactions(self.event.event_id()).into_iter() {
@@ -917,7 +925,9 @@ impl Message {
917 925
918 if len > 0 { 926 if len > 0 {
919 let style = Style::default(); 927 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);
921 let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD)); 931 let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD));
922 threaded.push_str(" \u{2937} ", style); 932 threaded.push_str(" \u{2937} ", style);
923 threaded.push_span_nobreak(len); 933 threaded.push_span_nobreak(len);
@@ -929,11 +939,20 @@ impl Message {
929 text 939 text
930 } 940 }
931 941
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 {
933 if let Some(html) = &self.html { 949 if let Some(html) = &self.html {
934 html.to_text(width, style, hide_reply) 950 html.to_text(width, style, hide_reply, emoji_shortcodes)
935 } else { 951 } else {
936 let mut msg = self.event.body(); 952 let mut msg = self.event.body();
953 if emoji_shortcodes {
954 msg = Cow::Owned(replace_emojis_in_str(msg.as_ref()));
955 }
937 956
938 if self.downloaded { 957 if self.downloaded {
939 msg.to_mut().push_str(" \u{2705}"); 958 msg.to_mut().push_str(" \u{2705}");
src/message/printer.rs +39 −9
@@ -11,7 +11,13 @@ use ratatui::text::{Line, Span, Text};
11use unicode_segmentation::UnicodeSegmentation; 11use unicode_segmentation::UnicodeSegmentation;
12use unicode_width::UnicodeWidthStr; 12use unicode_width::UnicodeWidthStr;
13 13
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};
15 21
16/// Wrap styled text for the current terminal width. 22/// Wrap styled text for the current terminal width.
17pub struct TextPrinter<'a> { 23pub struct TextPrinter<'a> {
@@ -19,6 +25,7 @@ pub struct TextPrinter<'a> {
19 width: usize, 25 width: usize,
20 base_style: Style, 26 base_style: Style,
21 hide_reply: bool, 27 hide_reply: bool,
28 emoji_shortcodes: bool,
22 29
23 alignment: Alignment, 30 alignment: Alignment,
24 curr_spans: Vec<Span<'a>>, 31 curr_spans: Vec<Span<'a>>,
@@ -28,12 +35,13 @@ pub struct TextPrinter<'a> {
28 35
29impl<'a> TextPrinter<'a> { 36impl<'a> TextPrinter<'a> {
30 /// Create a new printer. 37 /// 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 {
32 TextPrinter { 39 TextPrinter {
33 text: Text::default(), 40 text: Text::default(),
34 width, 41 width,
35 base_style, 42 base_style,
36 hide_reply, 43 hide_reply,
44 emoji_shortcodes,
37 45
38 alignment: Alignment::Left, 46 alignment: Alignment::Left,
39 curr_spans: vec![], 47 curr_spans: vec![],
@@ -59,6 +67,11 @@ impl<'a> TextPrinter<'a> {
59 self.hide_reply 67 self.hide_reply
60 } 68 }
61 69
70 /// Indicates whether emojis should be replaced by shortcodes
71 pub fn emoji_shortcodes(&self) -> bool {
72 self.emoji_shortcodes
73 }
74
62 /// Indicates the current printer's width. 75 /// Indicates the current printer's width.
63 pub fn width(&self) -> usize { 76 pub fn width(&self) -> usize {
64 self.width 77 self.width
@@ -71,6 +84,7 @@ impl<'a> TextPrinter<'a> {
71 width: self.width.saturating_sub(indent), 84 width: self.width.saturating_sub(indent),
72 base_style: self.base_style, 85 base_style: self.base_style,
73 hide_reply: self.hide_reply, 86 hide_reply: self.hide_reply,
87 emoji_shortcodes: self.emoji_shortcodes,
74 88
75 alignment: self.alignment, 89 alignment: self.alignment,
76 curr_spans: vec![], 90 curr_spans: vec![],
@@ -164,7 +178,10 @@ impl<'a> TextPrinter<'a> {
164 } 178 }
165 179
166 /// Push a [Span] that isn't allowed to break across lines. 180 /// 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 }
168 let sw = UnicodeWidthStr::width(span.content.as_ref()); 185 let sw = UnicodeWidthStr::width(span.content.as_ref());
169 186
170 if self.curr_width + sw > self.width { 187 if self.curr_width + sw > self.width {
@@ -200,10 +217,15 @@ impl<'a> TextPrinter<'a> {
200 continue; 217 continue;
201 } 218 }
202 219
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());
204 226
205 if sw > self.width { 227 if sw > self.width {
206 self.push_str_wrapped(word, style); 228 self.push_str_wrapped(cow, style);
207 continue; 229 continue;
208 } 230 }
209 231
@@ -211,13 +233,13 @@ impl<'a> TextPrinter<'a> {
211 // Word doesn't fit on this line, so start a new one. 233 // Word doesn't fit on this line, so start a new one.
212 self.commit(); 234 self.commit();
213 235
214 if !self.literal && word.chars().all(char::is_whitespace) { 236 if !self.literal && cow.chars().all(char::is_whitespace) {
215 // Drop leading whitespace. 237 // Drop leading whitespace.
216 continue; 238 continue;
217 } 239 }
218 } 240 }
219 241
220 let span = Span::styled(word, style); 242 let span = Span::styled(cow, style);
221 self.curr_spans.push(span); 243 self.curr_spans.push(span);
222 self.curr_width += sw; 244 self.curr_width += sw;
223 } 245 }
@@ -229,14 +251,22 @@ impl<'a> TextPrinter<'a> {
229 } 251 }
230 252
231 /// Push a [Line] into the printer. 253 /// 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>) {
233 self.commit(); 255 self.commit();
256 if self.emoji_shortcodes {
257 replace_emojis_in_line(&mut line);
258 }
234 self.text.lines.push(line); 259 self.text.lines.push(line);
235 } 260 }
236 261
237 /// Push multiline [Text] into the printer. 262 /// 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>) {
239 self.commit(); 264 self.commit();
265 if self.emoji_shortcodes {
266 for line in &mut text.lines {
267 replace_emojis_in_line(line);
268 }
269 }
240 self.text.lines.extend(text.lines); 270 self.text.lines.extend(text.lines);
241 } 271 }
242 272
src/tests.rs +1
@@ -183,6 +183,7 @@ pub fn mock_tunables() -> TunableValues {
183 TunableValues { 183 TunableValues {
184 default_room: None, 184 default_room: None,
185 log_level: Level::INFO, 185 log_level: Level::INFO,
186 message_shortcode_display: false,
186 reaction_display: true, 187 reaction_display: true,
187 reaction_shortcode_display: false, 188 reaction_shortcode_display: false,
188 read_receipt_send: true, 189 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:
147 text 147 text
148} 148}
149 149
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
150#[cfg(test)] 172#[cfg(test)]
151pub mod tests { 173pub mod tests {
152 use super::*; 174 use super::*;