Skip to content

Commit

Permalink
Show a bit more information for attachments.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulak committed Apr 28, 2024
1 parent 2e28f48 commit c51d191
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Chat {
handler.park();

let result = get_text(
Some(message.display()),
Some(&message.display()),
Some(&format!(
"<!-- Edit your message above to change it in {}. -->",
self.room.name
Expand Down Expand Up @@ -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());

Expand Down
62 changes: 47 additions & 15 deletions src/widgets/message.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand All @@ -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');
}
}
Expand Down Expand Up @@ -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()),
_ => {}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -452,7 +483,7 @@ impl Message {

pub fn to_list_items(&self, width: usize) -> Vec<ListItem> {
let items: Vec<ratatui::text::Text> = 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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c51d191

Please sign in to comment.