Commit d13d4b9f7f

d13d4b9f7fe240df13027ce807750e082015a44b

parent: 54ce042384

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-01-13 05:20 UTC

Support replying to messages (#3)

Layout: unified · split

README.md +1 −1
@@ -62,7 +62,7 @@ two other TUI clients and Element Web:
6262| Accepting Invites | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6363| Typing Notification | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6464| E2E | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
65| Replies | :x: ([#3]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
65| Replies | :heavy_check_mark: | :heavy_check_mark: | :x: | :heavy_check_mark: |
6666| Attachment uploading | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: |
6767| Attachment downloading | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
6868| Send stickers | :x: | :x: | :x: | :heavy_check_mark: |
src/base.rs +2
@@ -61,7 +61,9 @@ pub enum VerifyAction {
6161
6262#[derive(Clone, Debug, Eq, PartialEq)]
6363pub enum MessageAction {
64 Cancel,
6465 Download(Option<String>, bool),
66 Reply,
6567}
6668
6769#[derive(Clone, Debug, Eq, PartialEq)]
src/commands.rs +24
@@ -133,6 +133,28 @@ fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
133133 return Ok(step);
134134}
135135
136fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
137 if !desc.arg.text.is_empty() {
138 return Result::Err(CommandError::InvalidArgument);
139 }
140
141 let ract = IambAction::from(MessageAction::Cancel);
142 let step = CommandStep::Continue(ract.into(), ctx.context.take());
143
144 return Ok(step);
145}
146
147fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
148 if !desc.arg.text.is_empty() {
149 return Result::Err(CommandError::InvalidArgument);
150 }
151
152 let ract = IambAction::from(MessageAction::Reply);
153 let step = CommandStep::Continue(ract.into(), ctx.context.take());
154
155 return Ok(step);
156}
157
136158fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
137159 if !desc.arg.text.is_empty() {
138160 return Result::Err(CommandError::InvalidArgument);
@@ -231,11 +253,13 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult
231253}
232254
233255fn add_iamb_commands(cmds: &mut ProgramCommands) {
256 cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
234257 cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
235258 cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
236259 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
237260 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
238261 cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members });
262 cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply });
239263 cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
240264 cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set });
241265 cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
src/message.rs +83 −56
@@ -11,9 +11,12 @@ use unicode_segmentation::UnicodeSegmentation;
1111use unicode_width::UnicodeWidthStr;
1212
1313use matrix_sdk::ruma::{
14 events::{
15 room::message::{MessageType, RoomMessageEventContent},
16 MessageLikeEvent,
14 events::room::message::{
15 MessageType,
16 OriginalRoomMessageEvent,
17 RedactedRoomMessageEvent,
18 RoomMessageEvent,
19 RoomMessageEventContent,
1720 },
1821 MilliSecondsSinceUnixEpoch,
1922 OwnedEventId,
@@ -33,8 +36,7 @@ use crate::{
3336 config::ApplicationSettings,
3437};
3538
36pub type MessageEvent = MessageLikeEvent<RoomMessageEventContent>;
37pub type MessageFetchResult = IambResult<(Option<String>, Vec<MessageEvent>)>;
39pub type MessageFetchResult = IambResult<(Option<String>, Vec<RoomMessageEvent>)>;
3840pub type MessageKey = (MessageTimeStamp, OwnedEventId);
3941pub type Messages = BTreeMap<MessageKey, Message>;
4042
@@ -146,6 +148,13 @@ impl MessageTimeStamp {
146148 fn is_local_echo(&self) -> bool {
147149 matches!(self, MessageTimeStamp::LocalEcho)
148150 }
151
152 pub fn as_millis(&self) -> Option<MilliSecondsSinceUnixEpoch> {
153 match self {
154 MessageTimeStamp::OriginServer(ms) => MilliSecondsSinceUnixEpoch(*ms).into(),
155 MessageTimeStamp::LocalEcho => None,
156 }
157 }
149158}
150159
151160impl Ord for MessageTimeStamp {
@@ -304,61 +313,65 @@ impl PartialOrd for MessageCursor {
304313}
305314
306315#[derive(Clone)]
307pub enum MessageContent {
308 Original(Box<RoomMessageEventContent>),
309 Redacted,
316pub enum MessageEvent {
317 Original(Box<OriginalRoomMessageEvent>),
318 Redacted(Box<RedactedRoomMessageEvent>),
319 Local(Box<RoomMessageEventContent>),
310320}
311321
312impl MessageContent {
322impl MessageEvent {
313323 pub fn show(&self) -> Cow<'_, str> {
314324 match self {
315 MessageContent::Original(ev) => {
316 let s = match &ev.msgtype {
317 MessageType::Text(content) => content.body.as_ref(),
318 MessageType::Emote(content) => content.body.as_ref(),
319 MessageType::Notice(content) => content.body.as_str(),
320 MessageType::ServerNotice(content) => content.body.as_str(),
321
322 MessageType::VerificationRequest(_) => {
323 // XXX: implement
324
325 return Cow::Owned("[verification request]".into());
326 },
327 MessageType::Audio(content) => {
328 return Cow::Owned(format!("[Attached Audio: {}]", content.body));
329 },
330 MessageType::File(content) => {
331 return Cow::Owned(format!("[Attached File: {}]", content.body));
332 },
333 MessageType::Image(content) => {
334 return Cow::Owned(format!("[Attached Image: {}]", content.body));
335 },
336 MessageType::Video(content) => {
337 return Cow::Owned(format!("[Attached Video: {}]", content.body));
338 },
339 _ => {
340 return Cow::Owned(format!("[Unknown message type: {:?}]", ev.msgtype()));
341 },
342 };
343
344 Cow::Borrowed(s)
345 },
346 MessageContent::Redacted => Cow::Borrowed("[redacted]"),
325 MessageEvent::Original(ev) => show_room_content(&ev.content),
326 MessageEvent::Redacted(_) => Cow::Borrowed("[redacted]"),
327 MessageEvent::Local(content) => show_room_content(content),
347328 }
348329 }
349330}
350331
332fn show_room_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
333 let s = match &content.msgtype {
334 MessageType::Text(content) => content.body.as_ref(),
335 MessageType::Emote(content) => content.body.as_ref(),
336 MessageType::Notice(content) => content.body.as_str(),
337 MessageType::ServerNotice(content) => content.body.as_str(),
338
339 MessageType::VerificationRequest(_) => {
340 // XXX: implement
341
342 return Cow::Owned("[verification request]".into());
343 },
344 MessageType::Audio(content) => {
345 return Cow::Owned(format!("[Attached Audio: {}]", content.body));
346 },
347 MessageType::File(content) => {
348 return Cow::Owned(format!("[Attached File: {}]", content.body));
349 },
350 MessageType::Image(content) => {
351 return Cow::Owned(format!("[Attached Image: {}]", content.body));
352 },
353 MessageType::Video(content) => {
354 return Cow::Owned(format!("[Attached Video: {}]", content.body));
355 },
356 _ => {
357 return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype()));
358 },
359 };
360
361 Cow::Borrowed(s)
362}
363
351364#[derive(Clone)]
352365pub struct Message {
353 pub content: MessageContent,
366 pub event: MessageEvent,
354367 pub sender: OwnedUserId,
355368 pub timestamp: MessageTimeStamp,
356369 pub downloaded: bool,
357370}
358371
359372impl Message {
360 pub fn new(content: MessageContent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
361 Message { content, sender, timestamp, downloaded: false }
373 pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
374 Message { event, sender, timestamp, downloaded: false }
362375 }
363376
364377 pub fn show(
@@ -369,7 +382,7 @@ impl Message {
369382 settings: &ApplicationSettings,
370383 ) -> Text {
371384 let width = vwctx.get_width();
372 let mut msg = self.content.show();
385 let mut msg = self.event.show();
373386
374387 if self.downloaded {
375388 msg.to_mut().push_str(" \u{2705}");
@@ -465,24 +478,38 @@ impl Message {
465478 }
466479}
467480
468impl From<MessageEvent> for Message {
469 fn from(event: MessageEvent) -> Self {
470 match event {
471 MessageLikeEvent::Original(ev) => {
472 let content = MessageContent::Original(ev.content.into());
481impl From<OriginalRoomMessageEvent> for Message {
482 fn from(event: OriginalRoomMessageEvent) -> Self {
483 let timestamp = event.origin_server_ts.into();
484 let user_id = event.sender.clone();
485 let content = MessageEvent::Original(event.into());
473486
474 Message::new(content, ev.sender, ev.origin_server_ts.into())
475 },
476 MessageLikeEvent::Redacted(ev) => {
477 Message::new(MessageContent::Redacted, ev.sender, ev.origin_server_ts.into())
478 },
487 Message::new(content, user_id, timestamp)
488 }
489}
490
491impl From<RedactedRoomMessageEvent> for Message {
492 fn from(event: RedactedRoomMessageEvent) -> Self {
493 let timestamp = event.origin_server_ts.into();
494 let user_id = event.sender.clone();
495 let content = MessageEvent::Redacted(event.into());
496
497 Message::new(content, user_id, timestamp)
498 }
499}
500
501impl From<RoomMessageEvent> for Message {
502 fn from(event: RoomMessageEvent) -> Self {
503 match event {
504 RoomMessageEvent::Original(ev) => ev.into(),
505 RoomMessageEvent::Redacted(ev) => ev.into(),
479506 }
480507 }
481508}
482509
483510impl ToString for Message {
484511 fn to_string(&self) -> String {
485 self.content.show().into_owned()
512 self.event.show().into_owned()
486513 }
487514}
488515
src/tests.rs +40 −24
@@ -3,10 +3,11 @@ use std::path::PathBuf;
33
44use matrix_sdk::ruma::{
55 event_id,
6 events::room::message::RoomMessageEventContent,
6 events::room::message::{OriginalRoomMessageEvent, RoomMessageEventContent},
77 server_name,
88 user_id,
99 EventId,
10 OwnedEventId,
1011 OwnedRoomId,
1112 OwnedUserId,
1213 RoomId,
@@ -30,7 +31,7 @@ use crate::{
3031 },
3132 message::{
3233 Message,
33 MessageContent,
34 MessageEvent,
3435 MessageKey,
3536 MessageTimeStamp::{LocalEcho, OriginServer},
3637 Messages,
@@ -45,54 +46,69 @@ lazy_static! {
4546 pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
4647 pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
4748 pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
48 pub static ref MSG1_KEY: MessageKey = (LocalEcho, EventId::new(server_name!("example.com")));
49 pub static ref MSG2_KEY: MessageKey =
50 (OriginServer(UInt::new(1).unwrap()), EventId::new(server_name!("example.com")));
51 pub static ref MSG3_KEY: MessageKey = (
52 OriginServer(UInt::new(2).unwrap()),
53 event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned()
54 );
55 pub static ref MSG4_KEY: MessageKey = (
56 OriginServer(UInt::new(2).unwrap()),
57 event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned()
58 );
59 pub static ref MSG5_KEY: MessageKey =
60 (OriginServer(UInt::new(8).unwrap()), EventId::new(server_name!("example.com")));
49 pub static ref MSG1_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
50 pub static ref MSG2_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
51 pub static ref MSG3_EVID: OwnedEventId =
52 event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
53 pub static ref MSG4_EVID: OwnedEventId =
54 event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
55 pub static ref MSG5_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
56 pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
57 pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
58 pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
59 pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
60 pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
61}
62
63pub fn mock_room1_message(
64 content: RoomMessageEventContent,
65 sender: OwnedUserId,
66 key: MessageKey,
67) -> Message {
68 let origin_server_ts = key.0.as_millis().unwrap();
69 let event_id = key.1;
70
71 let event = OriginalRoomMessageEvent {
72 content,
73 event_id,
74 sender,
75 origin_server_ts,
76 room_id: TEST_ROOM1_ID.clone(),
77 unsigned: Default::default(),
78 };
79
80 event.into()
6181}
6282
6383pub fn mock_message1() -> Message {
6484 let content = RoomMessageEventContent::text_plain("writhe");
65 let content = MessageContent::Original(content.into());
85 let content = MessageEvent::Local(content.into());
6686
6787 Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
6888}
6989
7090pub fn mock_message2() -> Message {
7191 let content = RoomMessageEventContent::text_plain("helium");
72 let content = MessageContent::Original(content.into());
7392
74 Message::new(content, TEST_USER2.clone(), MSG2_KEY.0)
93 mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
7594}
7695
7796pub fn mock_message3() -> Message {
7897 let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
79 let content = MessageContent::Original(content.into());
8098
81 Message::new(content, TEST_USER2.clone(), MSG3_KEY.0)
99 mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
82100}
83101
84102pub fn mock_message4() -> Message {
85103 let content = RoomMessageEventContent::text_plain("help");
86 let content = MessageContent::Original(content.into());
87104
88 Message::new(content, TEST_USER1.clone(), MSG4_KEY.0)
105 mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
89106}
90107
91108pub fn mock_message5() -> Message {
92109 let content = RoomMessageEventContent::text_plain("character");
93 let content = MessageContent::Original(content.into());
94110
95 Message::new(content, TEST_USER2.clone(), MSG5_KEY.0)
111 mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
96112}
97113
98114pub fn mock_messages() -> Messages {
src/windows/room/chat.rs +85 −13
@@ -8,14 +8,24 @@ use matrix_sdk::{
88 media::{MediaFormat, MediaRequest},
99 room::Room as MatrixRoom,
1010 ruma::{
11 events::room::message::{MessageType, RoomMessageEventContent, TextMessageEventContent},
11 events::room::message::{
12 MessageType,
13 OriginalRoomMessageEvent,
14 RoomMessageEventContent,
15 TextMessageEventContent,
16 },
1217 OwnedRoomId,
1318 RoomId,
1419 },
1520};
1621
1722use modalkit::{
18 tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget},
23 tui::{
24 buffer::Buffer,
25 layout::Rect,
26 text::{Span, Spans},
27 widgets::{Paragraph, StatefulWidget, Widget},
28 },
1929 widgets::textbox::{TextBox, TextBoxState},
2030 widgets::TerminalCursor,
2131 widgets::{PromptActions, WindowOps},
@@ -52,10 +62,11 @@ use crate::base::{
5262 ProgramContext,
5363 ProgramStore,
5464 RoomFocus,
65 RoomInfo,
5566 SendAction,
5667};
5768
58use crate::message::{Message, MessageContent, MessageTimeStamp};
69use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp};
5970
6071use super::scrollback::{Scrollback, ScrollbackState};
6172
@@ -69,6 +80,8 @@ pub struct ChatState {
6980
7081 scrollback: ScrollbackState,
7182 focus: RoomFocus,
83
84 reply_to: Option<MessageKey>,
7285}
7386
7487impl ChatState {
@@ -89,9 +102,27 @@ impl ChatState {
89102
90103 scrollback,
91104 focus: RoomFocus::MessageBar,
105
106 reply_to: None,
92107 }
93108 }
94109
110 fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
111 let key = self.reply_to.as_ref()?;
112 let msg = info.messages.get(key)?;
113
114 if let MessageEvent::Original(ev) = &msg.event {
115 Some(ev)
116 } else {
117 None
118 }
119 }
120
121 fn reset(&mut self) -> EditRope {
122 self.reply_to = None;
123 self.tbox.reset()
124 }
125
95126 pub fn refresh_room(&mut self, store: &mut ProgramStore) {
96127 if let Some(room) = store.application.worker.client.get_room(self.id()) {
97128 self.room = room;
@@ -112,8 +143,13 @@ impl ChatState {
112143 let msg = self.scrollback.get_mut(info).ok_or(IambError::NoSelectedMessage)?;
113144
114145 match act {
146 MessageAction::Cancel => {
147 self.reply_to = None;
148
149 Ok(None)
150 },
115151 MessageAction::Download(filename, force) => {
116 if let MessageContent::Original(ev) = &msg.content {
152 if let MessageEvent::Original(ev) = &msg.event {
117153 let media = client.media();
118154
119155 let mut filename = match filename {
@@ -121,7 +157,7 @@ impl ChatState {
121157 None => settings.dirs.downloads.clone(),
122158 };
123159
124 let source = match &ev.msgtype {
160 let source = match &ev.content.msgtype {
125161 MessageType::Audio(c) => {
126162 if filename.is_dir() {
127163 filename.push(c.body.as_str());
@@ -188,6 +224,12 @@ impl ChatState {
188224
189225 Err(IambError::NoAttachment.into())
190226 },
227 MessageAction::Reply => {
228 self.reply_to = self.scrollback.get_key(info);
229 self.focus = RoomFocus::MessageBar;
230
231 Ok(None)
232 },
191233 }
192234 }
193235
@@ -203,6 +245,7 @@ impl ChatState {
203245 .client
204246 .get_joined_room(self.id())
205247 .ok_or(IambError::NotJoined)?;
248 let info = store.application.rooms.entry(self.id().to_owned()).or_default();
206249
207250 let (event_id, msg) = match act {
208251 SendAction::Submit => {
@@ -214,15 +257,21 @@ impl ChatState {
214257
215258 let msg = TextMessageEventContent::plain(msg);
216259 let msg = MessageType::Text(msg);
217 let msg = RoomMessageEventContent::new(msg);
260
261 let mut msg = RoomMessageEventContent::new(msg);
262
263 if let Some(m) = self.get_reply_to(info) {
264 // XXX: Switch to RoomMessageEventContent::reply() once it's stable?
265 msg = msg.make_reply_to(m);
266 }
218267
219268 // XXX: second parameter can be a locally unique transaction id.
220269 // Useful for doing retries.
221270 let resp = room.send(msg.clone(), None).await.map_err(IambError::from)?;
222271 let event_id = resp.event_id;
223272
224 // Clear the TextBoxState contents now that the message is sent.
225 self.tbox.reset();
273 // Reset message bar state now that it's been sent.
274 self.reset();
226275
227276 (event_id, msg)
228277 },
@@ -252,12 +301,14 @@ impl ChatState {
252301 };
253302
254303 let user = store.application.settings.profile.user_id.clone();
255 let info = store.application.get_room_info(self.id().to_owned());
256304 let key = (MessageTimeStamp::LocalEcho, event_id);
257 let msg = MessageContent::Original(msg.into());
305 let msg = MessageEvent::Local(msg.into());
258306 let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
259307 info.messages.insert(key, msg);
260308
309 // Jump to the end of the scrollback to show the message.
310 self.scrollback.goto_latest();
311
261312 Ok(None)
262313 }
263314
@@ -333,6 +384,8 @@ impl WindowOps<IambInfo> for ChatState {
333384
334385 scrollback: self.scrollback.dup(store),
335386 focus: self.focus,
387
388 reply_to: None,
336389 }
337390 }
338391
@@ -432,7 +485,7 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
432485 return Ok(vec![]);
433486 }
434487
435 let text = self.tbox.reset().trim();
488 let text = self.reset().trim();
436489
437490 if text.is_empty() {
438491 let _ = self.sent.end();
@@ -506,15 +559,34 @@ impl<'a> StatefulWidget for Chat<'a> {
506559 let lines = state.tbox.has_lines(5).max(1) as u16;
507560 let drawh = area.height;
508561 let texth = lines.min(drawh).clamp(1, 5);
509 let scrollh = drawh.saturating_sub(texth);
562 let desch = if state.reply_to.is_some() {
563 drawh.saturating_sub(texth).min(1)
564 } else {
565 0
566 };
567 let scrollh = drawh.saturating_sub(texth).saturating_sub(desch);
510568
511569 let scrollarea = Rect::new(area.x, area.y, area.width, scrollh);
512 let textarea = Rect::new(scrollarea.x, scrollarea.y + scrollh, scrollarea.width, texth);
570 let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
571 let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
513572
514573 let scrollback_focused = state.focus.is_scrollback() && self.focused;
515574 let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
516575 scrollback.render(scrollarea, buf, &mut state.scrollback);
517576
577 let desc_spans = state.reply_to.as_ref().and_then(|k| {
578 let room = self.store.application.rooms.get(state.id())?;
579 let msg = room.messages.get(k)?;
580 let user = self.store.application.settings.get_user_span(msg.sender.as_ref());
581 let spans = Spans(vec![Span::from("Replying to "), user]);
582
583 spans.into()
584 });
585
586 if let Some(desc_spans) = desc_spans {
587 Paragraph::new(desc_spans).render(descarea, buf);
588 }
589
518590 let prompt = if self.focused { "> " } else { " " };
519591
520592 let tbox = TextBox::new().prompt(prompt);
src/windows/room/scrollback.rs +44 −7
@@ -104,11 +104,26 @@ fn nth_after(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageCursor {
104104}
105105
106106pub struct ScrollbackState {
107 /// The room identifier.
107108 room_id: OwnedRoomId,
109
110 /// The buffer identifier used for saving marks, etc.
108111 id: IambBufferId,
112
113 /// The currently selected message in the scrollback.
109114 cursor: MessageCursor,
115
116 /// Contextual info about the viewport used during rendering.
110117 viewctx: ViewportContext<MessageCursor>,
118
119 /// The jumplist of visited messages.
111120 jumped: HistoryList<MessageCursor>,
121
122 /// Whether the full message should be drawn during the next render() call.
123 ///
124 /// This is used to ensure that ^E/^Y work nicely when the cursor is currently
125 /// on a multiline message.
126 show_full_on_redraw: bool,
112127}
113128
114129impl ScrollbackState {
@@ -117,8 +132,20 @@ impl ScrollbackState {
117132 let cursor = MessageCursor::default();
118133 let viewctx = ViewportContext::default();
119134 let jumped = HistoryList::default();
135 let show_full_on_redraw = false;
120136
121 ScrollbackState { room_id, id, cursor, viewctx, jumped }
137 ScrollbackState {
138 room_id,
139 id,
140 cursor,
141 viewctx,
142 jumped,
143 show_full_on_redraw,
144 }
145 }
146
147 pub fn goto_latest(&mut self) {
148 self.cursor = MessageCursor::latest();
122149 }
123150
124151 /// Set the dimensions and placement within the terminal window for this list.
@@ -126,6 +153,13 @@ impl ScrollbackState {
126153 self.viewctx.dimensions = (area.width as usize, area.height as usize);
127154 }
128155
156 pub fn get_key(&self, info: &mut RoomInfo) -> Option<MessageKey> {
157 self.cursor
158 .timestamp
159 .clone()
160 .or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
161 }
162
129163 pub fn get_mut<'a>(&mut self, info: &'a mut RoomInfo) -> Option<&'a mut Message> {
130164 if let Some(k) = &self.cursor.timestamp {
131165 info.messages.get_mut(k)
@@ -397,7 +431,7 @@ impl ScrollbackState {
397431 continue;
398432 }
399433
400 if needle.is_match(msg.content.show().as_ref()) {
434 if needle.is_match(msg.event.show().as_ref()) {
401435 mc = MessageCursor::from(key.clone()).into();
402436 count -= 1;
403437 }
@@ -421,7 +455,7 @@ impl ScrollbackState {
421455 break;
422456 }
423457
424 if needle.is_match(msg.content.show().as_ref()) {
458 if needle.is_match(msg.event.show().as_ref()) {
425459 mc = MessageCursor::from(key.clone()).into();
426460 count -= 1;
427461 }
@@ -462,6 +496,7 @@ impl WindowOps<IambInfo> for ScrollbackState {
462496 cursor: self.cursor.clone(),
463497 viewctx: self.viewctx.clone(),
464498 jumped: self.jumped.clone(),
499 show_full_on_redraw: false,
465500 }
466501 }
467502
@@ -582,6 +617,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
582617 self.cursor = pos;
583618 }
584619
620 self.show_full_on_redraw = true;
621
585622 return Ok(None);
586623 },
587624 EditAction::Yank => {
@@ -667,7 +704,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
667704 let mut yanked = EditRope::from("");
668705
669706 for (_, msg) in self.messages(range, info) {
670 yanked += EditRope::from(msg.content.show().into_owned());
707 yanked += EditRope::from(msg.event.show().into_owned());
671708 yanked += EditRope::from('\n');
672709 }
673710
@@ -1173,15 +1210,15 @@ impl<'a> StatefulWidget for Scrollback<'a> {
11731210 nth_key_before(cursor_key.clone(), height, info)
11741211 };
11751212
1176 let full = cursor.timestamp.is_none();
1177
1213 let foc = self.focused || cursor.timestamp.is_some();
1214 let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
11781215 let mut lines = vec![];
11791216 let mut sawit = false;
11801217 let mut prev = None;
11811218
11821219 for (key, item) in info.messages.range(&corner_key..) {
11831220 let sel = key == cursor_key;
1184 let txt = item.show(prev, self.focused && sel, &state.viewctx, settings);
1221 let txt = item.show(prev, foc && sel, &state.viewctx, settings);
11851222
11861223 prev = Some(item);
11871224