diff --git a/Cargo.lock b/Cargo.lock index a46d3b6..b043338 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1493,6 +1493,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[package]] +name = "human_bytes" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91f255a4535024abf7640cb288260811fc14794f62b063652ed349f9a6c2348e" + [[package]] name = "hyper" version = "0.14.27" @@ -2157,6 +2163,7 @@ dependencies = [ "dirs", "emojis", "futures", + "human_bytes", "image", "lazy_static", "linkify", diff --git a/Cargo.toml b/Cargo.toml index 6c36ad1..bf5aa38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ config = { version = "0.13", features = ["toml"] } dirs = "4.0" emojis = "0.5" futures = "0.3.24" +human_bytes = "0.4.3" image = "0.24" linkify = "0.9" lazy_static = "1.4" diff --git a/src/matrix/notify.rs b/src/matrix/notify.rs index 547cf61..3f7c45a 100644 --- a/src/matrix/notify.rs +++ b/src/matrix/notify.rs @@ -80,7 +80,7 @@ impl Notify { let avatar = Notify::get_image(room.clone(), user.clone()).await; let body = message.display(); - self.send_notification(user.name(), body, room, avatar)?; + self.send_notification(user.name(), &body, room, avatar)?; } Ok(()) diff --git a/src/widgets/chat.rs b/src/widgets/chat.rs index 8a722b1..6d4c58e 100644 --- a/src/widgets/chat.rs +++ b/src/widgets/chat.rs @@ -190,7 +190,7 @@ impl Chat { handler.park(); let result = get_text( - Some(message.display()), + Some(&message.display()), Some(&format!( "", self.room.name @@ -259,7 +259,7 @@ impl Chat { .initial_indent(" ") .subsequent_indent(" "); - let body = textwrap::wrap(message.display(), &wrap_options).join("\n"); + let body = textwrap::wrap(&message.display(), &wrap_options).join("\n"); let send = self.matrix.begin_typing(self.room()); diff --git a/src/widgets/message.rs b/src/widgets/message.rs index 539af8e..7619824 100644 --- a/src/widgets/message.rs +++ b/src/widgets/message.rs @@ -1,5 +1,6 @@ use crate::widgets::message::MessageType::File; use chrono::TimeZone; +use human_bytes::human_bytes; use std::cell::Cell; use std::collections::BinaryHeap; use std::time::{Duration, SystemTime}; @@ -73,18 +74,48 @@ impl Message { } } - fn display_body(body: &MessageType) -> &str { + fn display_body(body: &MessageType) -> String { match body { - Text(TextMessageEventContent { body, .. }) => body, - Image(ImageMessageEventContent { body, .. }) => body, - Video(VideoMessageEventContent { body, .. }) => body, - File(FileMessageEventContent { body, .. }) => body, - _ => "unknown", + Text(TextMessageEventContent { body, .. }) => body.to_string(), + Image(ImageMessageEventContent { body, info, .. }) => { + if let Some(info) = info { + if let Some(size) = info.size { + format!("Image: {} ({})", body, human_bytes(size)) + } else { + body.to_string() + } + } else { + body.to_string() + } + } + Video(VideoMessageEventContent { body, info, .. }) => { + if let Some(info) = info { + if let Some(size) = info.size { + format!("Video: {} ({})", body, human_bytes(size)) + } else { + body.to_string() + } + } else { + body.to_string() + } + } + File(FileMessageEventContent { body, info, .. }) => { + if let Some(info) = info { + if let Some(size) = info.size { + format!("File: {} ({})", body, human_bytes(size)) + } else { + body.to_string() + } + } else { + body.to_string() + } + } + _ => "unknown".to_string(), } } - pub fn display(&self) -> &str { - Message::display_body(&self.body).trim() + pub fn display(&self) -> String { + Message::display_body(&self.body).trim().to_string() } pub fn display_full(&self) -> String { @@ -97,7 +128,7 @@ impl Message { self.sender.id ); - ret.push_str(self.display()); + ret.push_str(&self.display()); ret.push_str("\n\n"); if !self.reactions.is_empty() { @@ -122,7 +153,7 @@ impl Message { for h in reversed_history.into_iter() { ret.push_str("* "); - ret.push_str(Message::display_body(&h)); + ret.push_str(&Message::display_body(&h)); ret.push('\n'); } } @@ -154,7 +185,7 @@ impl Message { Image(_) => matrix.download_content(self.body.clone(), AfterDownload::View), Video(_) => matrix.download_content(self.body.clone(), AfterDownload::View), File(_) => matrix.download_content(self.body.clone(), AfterDownload::Save), - Text(_) => view_text(self.display()), + Text(_) => view_text(&self.display()), _ => {} } } @@ -413,9 +444,9 @@ impl Message { } let mut height = if reply { - textwrap::wrap(Message::remove_reply_header(self.display()), width).len() + textwrap::wrap(Message::remove_reply_header(&self.display()), width).len() } else { - textwrap::wrap(self.display(), width).len() + textwrap::wrap(&self.display(), width).len() }; height += 2; @@ -452,7 +483,7 @@ impl Message { pub fn to_list_items(&self, width: usize) -> Vec { let items: Vec = self - .to_list_items_internal(self.display(), width) + .to_list_items_internal(&self.display(), width) .into_iter() .map(|spans| ratatui::text::Text::from(Line::from(spans))) .collect(); @@ -522,7 +553,8 @@ impl Message { // replies for (i, r) in self.replies.iter().enumerate() { - let body = Message::remove_reply_header(r.display()); + let reply = r.display(); + let body = Message::remove_reply_header(&reply); let mut reply_lines = r.to_list_items_internal(body, width - 2); Message::indent(&mut reply_lines, i == 0); lines.append(&mut reply_lines);