Commit ac6ff63d25

ac6ff63d25c987d425774804b23367c1f1e4f68a

parent: 54a0e76823

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-05 20:59 UTC

Avoid breaking up words during wrapping when possible (#47)

Layout: unified · split

src/message/html.rs +261 −53
@@ -6,7 +6,7 @@
66//! The Matrix specification recommends limiting rendered tags and attributes to a safe subset of
77//! HTML. You can read more in section 11.2.1.1, "m.room.message msgtypes":
88//!
9//! https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes
9//! <https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes>
1010//!
1111//! This isn't as important for iamb, since it isn't a browser environment, but we do still map
1212//! input onto an enum of the safe list of tags to keep it easy to understand and process.
@@ -271,10 +271,12 @@ impl StyleTreeNode {
271271 },
272272 StyleTreeNode::Header(child, level) => {
273273 let style = style.add_modifier(StyleModifier::BOLD);
274 let mut hashes = "#".repeat(*level);
275 hashes.push(' ');
276274
277 printer.push_str(hashes, style);
275 for _ in 0..*level {
276 printer.push_str("#", style);
277 }
278
279 printer.push_str(" ", style);
278280 child.print(printer, style);
279281 },
280282 StyleTreeNode::Image(None) => {},
@@ -320,7 +322,9 @@ impl StyleTreeNode {
320322 printer.commit();
321323 },
322324 StyleTreeNode::Ruler => {
323 printer.push_str(line::HORIZONTAL.repeat(width), style);
325 for _ in 0..width {
326 printer.push_str(line::HORIZONTAL, style);
327 }
324328 },
325329 StyleTreeNode::Table(table) => {
326330 let text = table.to_text(width, style);
@@ -636,8 +640,11 @@ pub mod tests {
636640 let tree = parse_matrix_html(s);
637641 let text = tree.to_text(20, Style::default(), false);
638642 assert_eq!(text.lines, vec![Spans(vec![
639 Span::styled("# ", bold),
640 Span::styled("Header 1", bold),
643 Span::styled("#", bold),
644 Span::styled(" ", bold),
645 Span::styled("Header", bold),
646 Span::styled(" ", bold),
647 Span::styled("1", bold),
641648 space_span(10, Style::default())
642649 ])]);
643650
@@ -645,8 +652,12 @@ pub mod tests {
645652 let tree = parse_matrix_html(s);
646653 let text = tree.to_text(20, Style::default(), false);
647654 assert_eq!(text.lines, vec![Spans(vec![
648 Span::styled("## ", bold),
649 Span::styled("Header 2", bold),
655 Span::styled("#", bold),
656 Span::styled("#", bold),
657 Span::styled(" ", bold),
658 Span::styled("Header", bold),
659 Span::styled(" ", bold),
660 Span::styled("2", bold),
650661 space_span(9, Style::default())
651662 ])]);
652663
@@ -654,8 +665,13 @@ pub mod tests {
654665 let tree = parse_matrix_html(s);
655666 let text = tree.to_text(20, Style::default(), false);
656667 assert_eq!(text.lines, vec![Spans(vec![
657 Span::styled("### ", bold),
658 Span::styled("Header 3", bold),
668 Span::styled("#", bold),
669 Span::styled("#", bold),
670 Span::styled("#", bold),
671 Span::styled(" ", bold),
672 Span::styled("Header", bold),
673 Span::styled(" ", bold),
674 Span::styled("3", bold),
659675 space_span(8, Style::default())
660676 ])]);
661677
@@ -663,8 +679,14 @@ pub mod tests {
663679 let tree = parse_matrix_html(s);
664680 let text = tree.to_text(20, Style::default(), false);
665681 assert_eq!(text.lines, vec![Spans(vec![
666 Span::styled("#### ", bold),
667 Span::styled("Header 4", bold),
682 Span::styled("#", bold),
683 Span::styled("#", bold),
684 Span::styled("#", bold),
685 Span::styled("#", bold),
686 Span::styled(" ", bold),
687 Span::styled("Header", bold),
688 Span::styled(" ", bold),
689 Span::styled("4", bold),
668690 space_span(7, Style::default())
669691 ])]);
670692
@@ -672,8 +694,15 @@ pub mod tests {
672694 let tree = parse_matrix_html(s);
673695 let text = tree.to_text(20, Style::default(), false);
674696 assert_eq!(text.lines, vec![Spans(vec![
675 Span::styled("##### ", bold),
676 Span::styled("Header 5", bold),
697 Span::styled("#", bold),
698 Span::styled("#", bold),
699 Span::styled("#", bold),
700 Span::styled("#", bold),
701 Span::styled("#", bold),
702 Span::styled(" ", bold),
703 Span::styled("Header", bold),
704 Span::styled(" ", bold),
705 Span::styled("5", bold),
677706 space_span(6, Style::default())
678707 ])]);
679708
@@ -681,8 +710,16 @@ pub mod tests {
681710 let tree = parse_matrix_html(s);
682711 let text = tree.to_text(20, Style::default(), false);
683712 assert_eq!(text.lines, vec![Spans(vec![
684 Span::styled("###### ", bold),
685 Span::styled("Header 6", bold),
713 Span::styled("#", bold),
714 Span::styled("#", bold),
715 Span::styled("#", bold),
716 Span::styled("#", bold),
717 Span::styled("#", bold),
718 Span::styled("#", bold),
719 Span::styled(" ", bold),
720 Span::styled("Header", bold),
721 Span::styled(" ", bold),
722 Span::styled("6", bold),
686723 space_span(5, Style::default())
687724 ])]);
688725 }
@@ -700,7 +737,8 @@ pub mod tests {
700737 let tree = parse_matrix_html(s);
701738 let text = tree.to_text(20, Style::default(), false);
702739 assert_eq!(text.lines, vec![Spans(vec![
703 Span::styled("Bold!", bold),
740 Span::styled("Bold", bold),
741 Span::styled("!", bold),
704742 space_span(15, def)
705743 ])]);
706744
@@ -708,7 +746,8 @@ pub mod tests {
708746 let tree = parse_matrix_html(s);
709747 let text = tree.to_text(20, Style::default(), false);
710748 assert_eq!(text.lines, vec![Spans(vec![
711 Span::styled("Bold!", bold),
749 Span::styled("Bold", bold),
750 Span::styled("!", bold),
712751 space_span(15, def)
713752 ])]);
714753
@@ -716,7 +755,8 @@ pub mod tests {
716755 let tree = parse_matrix_html(s);
717756 let text = tree.to_text(20, Style::default(), false);
718757 assert_eq!(text.lines, vec![Spans(vec![
719 Span::styled("Italic!", italic),
758 Span::styled("Italic", italic),
759 Span::styled("!", italic),
720760 space_span(13, def)
721761 ])]);
722762
@@ -724,7 +764,8 @@ pub mod tests {
724764 let tree = parse_matrix_html(s);
725765 let text = tree.to_text(20, Style::default(), false);
726766 assert_eq!(text.lines, vec![Spans(vec![
727 Span::styled("Italic!", italic),
767 Span::styled("Italic", italic),
768 Span::styled("!", italic),
728769 space_span(13, def)
729770 ])]);
730771
@@ -732,7 +773,8 @@ pub mod tests {
732773 let tree = parse_matrix_html(s);
733774 let text = tree.to_text(20, Style::default(), false);
734775 assert_eq!(text.lines, vec![Spans(vec![
735 Span::styled("Strikethrough!", strike),
776 Span::styled("Strikethrough", strike),
777 Span::styled("!", strike),
736778 space_span(6, def)
737779 ])]);
738780
@@ -740,7 +782,8 @@ pub mod tests {
740782 let tree = parse_matrix_html(s);
741783 let text = tree.to_text(20, Style::default(), false);
742784 assert_eq!(text.lines, vec![Spans(vec![
743 Span::styled("Strikethrough!", strike),
785 Span::styled("Strikethrough", strike),
786 Span::styled("!", strike),
744787 space_span(6, def)
745788 ])]);
746789
@@ -748,19 +791,28 @@ pub mod tests {
748791 let tree = parse_matrix_html(s);
749792 let text = tree.to_text(20, Style::default(), false);
750793 assert_eq!(text.lines, vec![Spans(vec![
751 Span::styled("Underline!", underl),
794 Span::styled("Underline", underl),
795 Span::styled("!", underl),
752796 space_span(10, def)
753797 ])]);
754798
755799 let s = "<font color=\"#ff0000\">Red!</u>";
756800 let tree = parse_matrix_html(s);
757801 let text = tree.to_text(20, Style::default(), false);
758 assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
802 assert_eq!(text.lines, vec![Spans(vec![
803 Span::styled("Red", red),
804 Span::styled("!", red),
805 space_span(16, def)
806 ])]);
759807
760808 let s = "<font color=\"red\">Red!</u>";
761809 let tree = parse_matrix_html(s);
762810 let text = tree.to_text(20, Style::default(), false);
763 assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
811 assert_eq!(text.lines, vec![Spans(vec![
812 Span::styled("Red", red),
813 Span::styled("!", red),
814 space_span(16, def)
815 ])]);
764816 }
765817
766818 #[test]
@@ -769,13 +821,25 @@ pub mod tests {
769821 let tree = parse_matrix_html(s);
770822 let text = tree.to_text(10, Style::default(), false);
771823 assert_eq!(text.lines.len(), 7);
772 assert_eq!(text.lines[0], Spans(vec![Span::raw("Hello worl")]));
773 assert_eq!(text.lines[1], Spans(vec![Span::raw("d!"), Span::raw(" ")]));
824 assert_eq!(
825 text.lines[0],
826 Spans(vec![Span::raw("Hello"), Span::raw(" "), Span::raw(" ")])
827 );
828 assert_eq!(
829 text.lines[1],
830 Spans(vec![Span::raw("world"), Span::raw("!"), Span::raw(" ")])
831 );
774832 assert_eq!(text.lines[2], Spans(vec![Span::raw(" ")]));
775833 assert_eq!(text.lines[3], Spans(vec![Span::raw("Content"), Span::raw(" ")]));
776834 assert_eq!(text.lines[4], Spans(vec![Span::raw(" ")]));
777 assert_eq!(text.lines[5], Spans(vec![Span::raw("Goodbye wo")]));
778 assert_eq!(text.lines[6], Spans(vec![Span::raw("rld!"), Span::raw(" ")]));
835 assert_eq!(
836 text.lines[5],
837 Spans(vec![Span::raw("Goodbye"), Span::raw(" "), Span::raw(" ")])
838 );
839 assert_eq!(
840 text.lines[6],
841 Spans(vec![Span::raw("world"), Span::raw("!"), Span::raw(" ")])
842 );
779843 }
780844
781845 #[test]
@@ -784,8 +848,14 @@ pub mod tests {
784848 let tree = parse_matrix_html(s);
785849 let text = tree.to_text(10, Style::default(), false);
786850 assert_eq!(text.lines.len(), 2);
787 assert_eq!(text.lines[0], Spans(vec![Span::raw(" "), Span::raw("Hello ")]));
788 assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("world!")]));
851 assert_eq!(
852 text.lines[0],
853 Spans(vec![Span::raw(" "), Span::raw("Hello"), Span::raw(" ")])
854 );
855 assert_eq!(
856 text.lines[1],
857 Spans(vec![Span::raw(" "), Span::raw("world"), Span::raw("!")])
858 );
789859 }
790860
791861 #[test]
@@ -794,12 +864,60 @@ pub mod tests {
794864 let tree = parse_matrix_html(s);
795865 let text = tree.to_text(8, Style::default(), false);
796866 assert_eq!(text.lines.len(), 6);
797 assert_eq!(text.lines[0], Spans(vec![Span::raw("- "), Span::raw("List I")]));
798 assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")]));
799 assert_eq!(text.lines[2], Spans(vec![Span::raw("- "), Span::raw("List I")]));
800 assert_eq!(text.lines[3], Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")]));
801 assert_eq!(text.lines[4], Spans(vec![Span::raw("- "), Span::raw("List I")]));
802 assert_eq!(text.lines[5], Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")]));
867 assert_eq!(
868 text.lines[0],
869 Spans(vec![
870 Span::raw("- "),
871 Span::raw("List"),
872 Span::raw(" "),
873 Span::raw(" ")
874 ])
875 );
876 assert_eq!(
877 text.lines[1],
878 Spans(vec![
879 Span::raw(" "),
880 Span::raw("Item"),
881 Span::raw(" "),
882 Span::raw("1")
883 ])
884 );
885 assert_eq!(
886 text.lines[2],
887 Spans(vec![
888 Span::raw("- "),
889 Span::raw("List"),
890 Span::raw(" "),
891 Span::raw(" ")
892 ])
893 );
894 assert_eq!(
895 text.lines[3],
896 Spans(vec![
897 Span::raw(" "),
898 Span::raw("Item"),
899 Span::raw(" "),
900 Span::raw("2")
901 ])
902 );
903 assert_eq!(
904 text.lines[4],
905 Spans(vec![
906 Span::raw("- "),
907 Span::raw("List"),
908 Span::raw(" "),
909 Span::raw(" ")
910 ])
911 );
912 assert_eq!(
913 text.lines[5],
914 Spans(vec![
915 Span::raw(" "),
916 Span::raw("Item"),
917 Span::raw(" "),
918 Span::raw("3")
919 ])
920 );
803921 }
804922
805923 #[test]
@@ -808,20 +926,59 @@ pub mod tests {
808926 let tree = parse_matrix_html(s);
809927 let text = tree.to_text(9, Style::default(), false);
810928 assert_eq!(text.lines.len(), 6);
811 assert_eq!(text.lines[0], Spans(vec![Span::raw("1. "), Span::raw("List I")]));
929 assert_eq!(
930 text.lines[0],
931 Spans(vec![
932 Span::raw("1. "),
933 Span::raw("List"),
934 Span::raw(" "),
935 Span::raw(" ")
936 ])
937 );
812938 assert_eq!(
813939 text.lines[1],
814 Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")])
940 Spans(vec![
941 Span::raw(" "),
942 Span::raw("Item"),
943 Span::raw(" "),
944 Span::raw("1")
945 ])
946 );
947 assert_eq!(
948 text.lines[2],
949 Spans(vec![
950 Span::raw("2. "),
951 Span::raw("List"),
952 Span::raw(" "),
953 Span::raw(" ")
954 ])
815955 );
816 assert_eq!(text.lines[2], Spans(vec![Span::raw("2. "), Span::raw("List I")]));
817956 assert_eq!(
818957 text.lines[3],
819 Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")])
958 Spans(vec![
959 Span::raw(" "),
960 Span::raw("Item"),
961 Span::raw(" "),
962 Span::raw("2")
963 ])
964 );
965 assert_eq!(
966 text.lines[4],
967 Spans(vec![
968 Span::raw("3. "),
969 Span::raw("List"),
970 Span::raw(" "),
971 Span::raw(" ")
972 ])
820973 );
821 assert_eq!(text.lines[4], Spans(vec![Span::raw("3. "), Span::raw("List I")]));
822974 assert_eq!(
823975 text.lines[5],
824 Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")])
976 Spans(vec![
977 Span::raw(" "),
978 Span::raw("Item"),
979 Span::raw(" "),
980 Span::raw("3")
981 ])
825982 );
826983 }
827984
@@ -854,9 +1011,13 @@ pub mod tests {
8541011 ]);
8551012 assert_eq!(text.lines[2].0, vec![
8561013 Span::raw("│"),
857 Span::styled("mn 1", bold),
1014 Span::styled("mn", bold),
1015 Span::styled(" ", bold),
1016 Span::styled("1", bold),
8581017 Span::raw("│"),
859 Span::styled("mn 2", bold),
1018 Span::styled("mn", bold),
1019 Span::styled(" ", bold),
1020 Span::styled("2", bold),
8601021 Span::raw("│"),
8611022 Span::styled("umn", bold),
8621023 Span::raw("│")
@@ -867,7 +1028,8 @@ pub mod tests {
8671028 Span::raw("│"),
8681029 Span::raw(" "),
8691030 Span::raw("│"),
870 Span::styled(" 3", bold),
1031 Span::styled(" ", bold),
1032 Span::styled("3", bold),
8711033 Span::styled(" ", bold),
8721034 Span::raw("│")
8731035 ]);
@@ -928,15 +1090,61 @@ pub mod tests {
9281090 let tree = parse_matrix_html(s);
9291091 let text = tree.to_text(10, Style::default(), false);
9301092 assert_eq!(text.lines.len(), 4);
931 assert_eq!(text.lines[0], Spans(vec![Span::raw("This was r")]));
932 assert_eq!(text.lines[1], Spans(vec![Span::raw("eplied to"), Span::raw(" ")]));
933 assert_eq!(text.lines[2], Spans(vec![Span::raw("This is th")]));
934 assert_eq!(text.lines[3], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
1093 assert_eq!(
1094 text.lines[0],
1095 Spans(vec![
1096 Span::raw("This"),
1097 Span::raw(" "),
1098 Span::raw("was"),
1099 Span::raw(" "),
1100 Span::raw(" ")
1101 ])
1102 );
1103 assert_eq!(
1104 text.lines[1],
1105 Spans(vec![Span::raw("replied"), Span::raw(" "), Span::raw("to")])
1106 );
1107 assert_eq!(
1108 text.lines[2],
1109 Spans(vec![
1110 Span::raw("This"),
1111 Span::raw(" "),
1112 Span::raw("is"),
1113 Span::raw(" "),
1114 Span::raw(" ")
1115 ])
1116 );
1117 assert_eq!(
1118 text.lines[3],
1119 Spans(vec![
1120 Span::raw("the"),
1121 Span::raw(" "),
1122 Span::raw("reply"),
1123 Span::raw(" ")
1124 ])
1125 );
9351126
9361127 let tree = parse_matrix_html(s);
9371128 let text = tree.to_text(10, Style::default(), true);
9381129 assert_eq!(text.lines.len(), 2);
939 assert_eq!(text.lines[0], Spans(vec![Span::raw("This is th")]));
940 assert_eq!(text.lines[1], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
1130 assert_eq!(
1131 text.lines[0],
1132 Spans(vec![
1133 Span::raw("This"),
1134 Span::raw(" "),
1135 Span::raw("is"),
1136 Span::raw(" "),
1137 Span::raw(" ")
1138 ])
1139 );
1140 assert_eq!(
1141 text.lines[1],
1142 Spans(vec![
1143 Span::raw("the"),
1144 Span::raw(" "),
1145 Span::raw("reply"),
1146 Span::raw(" ")
1147 ])
1148 );
9411149 }
9421150}
src/message/mod.rs +5 −1
@@ -710,7 +710,11 @@ impl Message {
710710 key
711711 };
712712
713 emojis.push_str(format!("[{name} {count}]"), style);
713 emojis.push_str("[", style);
714 emojis.push_str(name, style);
715 emojis.push_str(" ", style);
716 emojis.push_span_nobreak(Span::styled(count.to_string(), style));
717 emojis.push_str("]", style);
714718
715719 reactions += 1;
716720 }
src/message/printer.rs +51 −1
@@ -3,6 +3,7 @@ use std::borrow::Cow;
33use modalkit::tui::layout::Alignment;
44use modalkit::tui::style::Style;
55use modalkit::tui::text::{Span, Spans, Text};
6use unicode_segmentation::UnicodeSegmentation;
67use unicode_width::UnicodeWidthStr;
78
89use crate::util::{space_span, take_width};
@@ -107,7 +108,7 @@ impl<'a> TextPrinter<'a> {
107108 self.push();
108109 }
109110
110 pub fn push_str<T>(&mut self, s: T, style: Style)
111 fn push_str_wrapped<T>(&mut self, s: T, style: Style)
111112 where
112113 T: Into<Cow<'a, str>>,
113114 {
@@ -140,6 +141,55 @@ impl<'a> TextPrinter<'a> {
140141 }
141142 }
142143
144 pub fn push_span_nobreak(&mut self, span: Span<'a>) {
145 let sw = UnicodeWidthStr::width(span.content.as_ref());
146
147 if self.curr_width + sw > self.width {
148 // Span doesn't fit on this line, so start a new one.
149 self.commit();
150 }
151
152 self.curr_spans.push(span);
153 self.curr_width += sw;
154 }
155
156 pub fn push_str(&mut self, s: &'a str, style: Style) {
157 let style = self.base_style.patch(style);
158
159 for word in UnicodeSegmentation::split_word_bounds(s) {
160 if self.width == 0 && word.chars().all(char::is_whitespace) {
161 // Drop leading whitespace.
162 continue;
163 }
164
165 let sw = UnicodeWidthStr::width(word);
166
167 if sw > self.width {
168 self.push_str_wrapped(word, style);
169 continue;
170 }
171
172 if self.curr_width + sw > self.width {
173 // Word doesn't fit on this line, so start a new one.
174 self.commit();
175
176 if word.chars().all(char::is_whitespace) {
177 // Drop leading whitespace.
178 continue;
179 }
180 }
181
182 let span = Span::styled(word, style);
183 self.curr_spans.push(span);
184 self.curr_width += sw;
185 }
186
187 if self.curr_width == self.width {
188 // If the last bit fills the full line, start a new one.
189 self.push();
190 }
191 }
192
143193 pub fn push_line(&mut self, spans: Spans<'a>) {
144194 self.commit();
145195 self.text.lines.push(spans);