Commit c6d42d038f
Unregistered key
Layout: unified · split
docs/iamb.5 +2
| @@ -233,6 +233,8 @@ or | |||
| 233 | .It Sy user_gutter_width | 233 | .It Sy user_gutter_width |
| 234 | Specify the width of the column where usernames are displayed in a room. | 234 | Specify the width of the column where usernames are displayed in a room. |
| 235 | Usernames that are too long are truncated. | 235 | Usernames that are too long are truncated. |
| 236 | If this value is less than 3, the usernames are displayed in an extra line at the top of the message | ||
| 237 | and this value is only used to indent the message. | ||
| 236 | Defaults to 30. | 238 | Defaults to 30. |
| 237 | .It Sy tabstop | 239 | .It Sy tabstop |
| 238 | Number of spaces that a <Tab> counts for. | 240 | Number of spaces that a <Tab> counts for. |
src/message/mod.rs +67 −29
| @@ -602,6 +602,20 @@ impl MessageColumns { | |||
| 602 | } | 602 | } |
| 603 | } | 603 | } |
| 604 | 604 | ||
| 605 | #[derive(Default, Debug)] | ||
| 606 | enum SenderSpan<'a> { | ||
| 607 | /// Show the sender name in the user gutter. | ||
| 608 | /// This is truncated and padded to fit [`user_gutter_width`](`crate::config::TunableValues::user_gutter_width`). | ||
| 609 | Gutter(Span<'a>), | ||
| 610 | |||
| 611 | /// Show the sender name in an extra line at the top of the message. | ||
| 612 | Line(Span<'a>), | ||
| 613 | |||
| 614 | /// The sender name has already been printed. | ||
| 615 | #[default] | ||
| 616 | None, | ||
| 617 | } | ||
| 618 | |||
| 605 | struct MessageFormatter<'a> { | 619 | struct MessageFormatter<'a> { |
| 606 | settings: &'a ApplicationSettings, | 620 | settings: &'a ApplicationSettings, |
| 607 | 621 | ||
| @@ -615,7 +629,7 @@ struct MessageFormatter<'a> { | |||
| 615 | fill: usize, | 629 | fill: usize, |
| 616 | 630 | ||
| 617 | /// The formatted Span for the message sender. | 631 | /// The formatted Span for the message sender. |
| 618 | user: Option<Span<'a>>, | 632 | user: SenderSpan<'a>, |
| 619 | 633 | ||
| 620 | /// The time the message was sent. | 634 | /// The time the message was sent. |
| 621 | time: Option<Span<'a>>, | 635 | time: Option<Span<'a>>, |
| @@ -632,6 +646,20 @@ impl<'a> MessageFormatter<'a> { | |||
| 632 | self.fill | 646 | self.fill |
| 633 | } | 647 | } |
| 634 | 648 | ||
| 649 | fn message_start_line(&self) -> u16 { | ||
| 650 | let mut line = 0; | ||
| 651 | |||
| 652 | if self.date.is_some() { | ||
| 653 | line += 1; | ||
| 654 | } | ||
| 655 | |||
| 656 | if let SenderSpan::Line(_) = self.user { | ||
| 657 | line += 1; | ||
| 658 | } | ||
| 659 | |||
| 660 | line | ||
| 661 | } | ||
| 662 | |||
| 635 | #[inline] | 663 | #[inline] |
| 636 | fn push_spans(&mut self, prev_line: Line<'a>, style: Style, text: &mut Text<'a>) { | 664 | fn push_spans(&mut self, prev_line: Line<'a>, style: Style, text: &mut Text<'a>) { |
| 637 | if let Some(date) = self.date.take() { | 665 | if let Some(date) = self.date.take() { |
| @@ -646,13 +674,21 @@ impl<'a> MessageFormatter<'a> { | |||
| 646 | let user_gutter_empty_span = | 674 | let user_gutter_empty_span = |
| 647 | space_span(self.settings.tunables.user_gutter_width, Style::default()); | 675 | space_span(self.settings.tunables.user_gutter_width, Style::default()); |
| 648 | 676 | ||
| 677 | let user_gutter = match std::mem::take(&mut self.user) { | ||
| 678 | SenderSpan::Line(user) => { | ||
| 679 | text.lines.push(user.into()); | ||
| 680 | user_gutter_empty_span | ||
| 681 | }, | ||
| 682 | SenderSpan::Gutter(user) => user, | ||
| 683 | SenderSpan::None => user_gutter_empty_span, | ||
| 684 | }; | ||
| 685 | |||
| 649 | match self.cols { | 686 | match self.cols { |
| 650 | MessageColumns::Four => { | 687 | MessageColumns::Four => { |
| 651 | let settings = self.settings; | 688 | let settings = self.settings; |
| 652 | let user = self.user.take().unwrap_or(user_gutter_empty_span); | ||
| 653 | let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN); | 689 | let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN); |
| 654 | 690 | ||
| 655 | let mut line = vec![user]; | 691 | let mut line = vec![user_gutter]; |
| 656 | line.extend(prev_line.spans); | 692 | line.extend(prev_line.spans); |
| 657 | line.push(time); | 693 | line.push(time); |
| 658 | 694 | ||
| @@ -672,27 +708,21 @@ impl<'a> MessageFormatter<'a> { | |||
| 672 | text.lines.push(Line::from(line)) | 708 | text.lines.push(Line::from(line)) |
| 673 | }, | 709 | }, |
| 674 | MessageColumns::Three => { | 710 | MessageColumns::Three => { |
| 675 | let user = self.user.take().unwrap_or(user_gutter_empty_span); | ||
| 676 | let time = self.time.take().unwrap_or_else(|| Span::from("")); | 711 | let time = self.time.take().unwrap_or_else(|| Span::from("")); |
| 677 | 712 | ||
| 678 | let mut line = vec![user]; | 713 | let mut line = vec![user_gutter]; |
| 679 | line.extend(prev_line.spans); | 714 | line.extend(prev_line.spans); |
| 680 | line.push(time); | 715 | line.push(time); |
| 681 | 716 | ||
| 682 | text.lines.push(Line::from(line)) | 717 | text.lines.push(Line::from(line)) |
| 683 | }, | 718 | }, |
| 684 | MessageColumns::Two => { | 719 | MessageColumns::Two => { |
| 685 | let user = self.user.take().unwrap_or(user_gutter_empty_span); | 720 | let mut line = vec![user_gutter]; |
| 686 | let mut line = vec![user]; | ||
| 687 | line.extend(prev_line.spans); | 721 | line.extend(prev_line.spans); |
| 688 | 722 | ||
| 689 | text.lines.push(Line::from(line)); | 723 | text.lines.push(Line::from(line)); |
| 690 | }, | 724 | }, |
| 691 | MessageColumns::One => { | 725 | MessageColumns::One => { |
| 692 | if let Some(user) = self.user.take() { | ||
| 693 | text.lines.push(Line::from(vec![user])); | ||
| 694 | } | ||
| 695 | |||
| 696 | let leading = space_span(2, style); | 726 | let leading = space_span(2, style); |
| 697 | let mut line = vec![leading]; | 727 | let mut line = vec![leading]; |
| 698 | line.extend(prev_line.spans); | 728 | line.extend(prev_line.spans); |
| @@ -936,7 +966,7 @@ impl Message { | |||
| 936 | { | 966 | { |
| 937 | let cols = MessageColumns::Four; | 967 | let cols = MessageColumns::Four; |
| 938 | let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER; | 968 | let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER; |
| 939 | let user = self.show_sender(prev, true, info, settings); | 969 | let user = self.show_sender(prev, true, info, settings, width); |
| 940 | let time = self.timestamp.show_time(); | 970 | let time = self.timestamp.show_time(); |
| 941 | let read = info | 971 | let read = info |
| 942 | .event_receipts | 972 | .event_receipts |
| @@ -950,7 +980,7 @@ impl Message { | |||
| 950 | } else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width { | 980 | } else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width { |
| 951 | let cols = MessageColumns::Three; | 981 | let cols = MessageColumns::Three; |
| 952 | let fill = width - user_gutter - TIME_GUTTER; | 982 | let fill = width - user_gutter - TIME_GUTTER; |
| 953 | let user = self.show_sender(prev, true, info, settings); | 983 | let user = self.show_sender(prev, true, info, settings, width); |
| 954 | let time = self.timestamp.show_time(); | 984 | let time = self.timestamp.show_time(); |
| 955 | let read = Vec::new(); | 985 | let read = Vec::new(); |
| 956 | 986 | ||
| @@ -958,7 +988,7 @@ impl Message { | |||
| 958 | } else if user_gutter + MIN_MSG_LEN <= width { | 988 | } else if user_gutter + MIN_MSG_LEN <= width { |
| 959 | let cols = MessageColumns::Two; | 989 | let cols = MessageColumns::Two; |
| 960 | let fill = width - user_gutter; | 990 | let fill = width - user_gutter; |
| 961 | let user = self.show_sender(prev, true, info, settings); | 991 | let user = self.show_sender(prev, true, info, settings, width); |
| 962 | let time = None; | 992 | let time = None; |
| 963 | let read = Vec::new(); | 993 | let read = Vec::new(); |
| 964 | 994 | ||
| @@ -966,7 +996,7 @@ impl Message { | |||
| 966 | } else { | 996 | } else { |
| 967 | let cols = MessageColumns::One; | 997 | let cols = MessageColumns::One; |
| 968 | let fill = width.saturating_sub(2); | 998 | let fill = width.saturating_sub(2); |
| 969 | let user = self.show_sender(prev, false, info, settings); | 999 | let user = self.show_sender(prev, false, info, settings, width); |
| 970 | let time = None; | 1000 | let time = None; |
| 971 | let read = Vec::new(); | 1001 | let read = Vec::new(); |
| 972 | 1002 | ||
| @@ -1009,9 +1039,9 @@ impl Message { | |||
| 1009 | let proto_main = proto.map(|p| { | 1039 | let proto_main = proto.map(|p| { |
| 1010 | let y_off = text.lines.len() as u16; | 1040 | let y_off = text.lines.len() as u16; |
| 1011 | let x_off = fmt.cols.user_gutter_width(settings); | 1041 | let x_off = fmt.cols.user_gutter_width(settings); |
| 1012 | // Adjust y_off by 1 if a date was printed before the message to account for | 1042 | |
| 1013 | // the extra line we're going to print. | 1043 | // Account for extra lines printed before the message; |
| 1014 | let y_off = if fmt.date.is_some() { y_off + 1 } else { y_off }; | 1044 | let y_off = y_off + fmt.message_start_line(); |
| 1015 | (p, x_off, y_off) | 1045 | (p, x_off, y_off) |
| 1016 | }); | 1046 | }); |
| 1017 | 1047 | ||
| @@ -1096,31 +1126,39 @@ impl Message { | |||
| 1096 | fn show_sender<'a>( | 1126 | fn show_sender<'a>( |
| 1097 | &'a self, | 1127 | &'a self, |
| 1098 | prev: Option<&Message>, | 1128 | prev: Option<&Message>, |
| 1099 | align_right: bool, | 1129 | gutter_enabled: bool, |
| 1100 | info: &'a RoomInfo, | 1130 | info: &'a RoomInfo, |
| 1101 | settings: &'a ApplicationSettings, | 1131 | settings: &'a ApplicationSettings, |
| 1102 | ) -> Option<Span<'a>> { | 1132 | width: usize, |
| 1133 | ) -> SenderSpan<'a> { | ||
| 1103 | if let Some(prev) = prev { | 1134 | if let Some(prev) = prev { |
| 1104 | if self.sender == prev.sender && | 1135 | if self.sender == prev.sender && |
| 1105 | self.timestamp.same_day(&prev.timestamp) && | 1136 | self.timestamp.same_day(&prev.timestamp) && |
| 1106 | !self.event.is_emote() | 1137 | !self.event.is_emote() |
| 1107 | { | 1138 | { |
| 1108 | return None; | 1139 | return SenderSpan::None; |
| 1109 | } | 1140 | } |
| 1110 | } | 1141 | } |
| 1111 | 1142 | ||
| 1112 | let Span { content, style } = self.sender_span(info, settings); | 1143 | let Span { content, style } = self.sender_span(info, settings); |
| 1113 | let user_gutter = settings.tunables.user_gutter_width; | 1144 | let user_gutter = settings.tunables.user_gutter_width; |
| 1114 | let ((truncated, width), _) = take_width(content, user_gutter - 2); | ||
| 1115 | let padding = user_gutter - 2 - width; | ||
| 1116 | 1145 | ||
| 1117 | let sender = if align_right { | 1146 | let show_in_gutter = gutter_enabled && user_gutter > 2; |
| 1118 | format!("{}{} ", space(padding), truncated) | 1147 | |
| 1119 | } else { | 1148 | if show_in_gutter { |
| 1120 | format!("{}{} ", truncated, space(padding)) | 1149 | let ((truncated, width), _) = take_width(content, user_gutter - 2); |
| 1121 | }; | 1150 | let padding = user_gutter - 2 - width; |
| 1151 | |||
| 1152 | let sender = format!("{}{} ", space(padding), truncated); | ||
| 1122 | 1153 | ||
| 1123 | Span::styled(sender, style).into() | 1154 | SenderSpan::Gutter(Span::styled(sender, style)) |
| 1155 | } else if UnicodeWidthStr::width(content.as_ref()) > width { | ||
| 1156 | let ((truncated, _), _) = take_width(content, width); | ||
| 1157 | |||
| 1158 | SenderSpan::Line(Span::styled(truncated, style)) | ||
| 1159 | } else { | ||
| 1160 | SenderSpan::Line(Span::styled(content, style)) | ||
| 1161 | } | ||
| 1124 | } | 1162 | } |
| 1125 | 1163 | ||
| 1126 | pub fn redact(&mut self, redaction: SyncRoomRedactionEvent) { | 1164 | pub fn redact(&mut self, redaction: SyncRoomRedactionEvent) { |