Skip to content

Commit

Permalink
View full message details.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulak committed Apr 15, 2023
1 parent 795f5b6 commit 76646b3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
1 change: 1 addition & 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 @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
chrono = "0.4"
crossterm = "0.26"
config = { version = "0.13", features = ["toml"] }
dirs = "5.0"
Expand Down
4 changes: 2 additions & 2 deletions src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use notify_rust::Hint;
use std::env::var;
use std::io::{Cursor, Read};
use std::process::{Command, Stdio};
use tempfile::NamedTempFile;
use tempfile::Builder;

pub fn get_text(existing: Option<&str>) -> anyhow::Result<Option<String>> {
let editor = &var("EDITOR")?;
let mut tmpfile = NamedTempFile::new()?;
let mut tmpfile = Builder::new().suffix(".md").tempfile()?;

if let Some(str) = existing {
std::fs::write(&tmpfile, str)?;
Expand Down
13 changes: 13 additions & 0 deletions src/widgets/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ impl Chat {
bail!("Couldn't read from editor.")
}
}
KeyCode::Char('O') => {
let message = match self.selected_message() {
Some(m) => m,
None => return Ok(EventResult::Ignored),
};

handler.park();
get_text(Some(&message.display_full()))?;
handler.unpark();

App::get_sender().send(Event::Redraw)?;
return Ok(EventResult::Consumed(Action::Typing));
}
KeyCode::Char('r') => {
self.react = Some(React::new(
self.selected_reactions()
Expand Down
100 changes: 83 additions & 17 deletions src/widgets/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use chrono::TimeZone;
use std::time::{Duration, SystemTime};

use crate::matrix::matrix::{pad_emoji, Matrix};
use crate::pretty_list;
use crate::spawn::view_text;
use chrono::offset::Local;
use matrix_sdk::room::RoomMember;
use once_cell::unsync::OnceCell;
use ruma::events::relation::Replacement;
Expand Down Expand Up @@ -32,19 +34,73 @@ pub struct Message {
pub body: MessageType,
pub history: Vec<MessageType>,
pub sender: String,
pub sender_id: String,
pub reactions: Vec<Reaction>,
}

impl Message {
pub fn display(&self) -> &str {
match &self.body {
pub fn display_body(body: &MessageType) -> &str {
match body {
Text(TextMessageEventContent { body, .. }) => body,
Image(ImageMessageEventContent { body, .. }) => body,
Video(VideoMessageEventContent { body, .. }) => body,
_ => "unknown",
}
}

pub fn display(&self) -> &str {
Message::display_body(&self.body)
}

pub fn display_full(&self) -> String {
let date = Local.timestamp_opt(self.sent.as_secs().into(), 0).unwrap();

let mut ret = format!(
"Sent {} by {} ({})\n\n",
date.format("%Y-%m-%d at %I:%M:%S %p"),
self.sender_id,
self.sender_id
);

ret.push_str(self.display());
ret.push_str("\n");

if !self.reactions.is_empty() {
ret.push_str("### Reactions\n\n");

for r in &self.reactions {
for re in &r.events {
ret.push_str(
format!(
"* {} by {} ({})\n",
r.display(),
re.sender_name,
re.sender_id
)
.as_str(),
);
}
}

ret.push_str("\n");
}

if !self.history.is_empty() {
let mut reversed_history = self.history.clone();
reversed_history.reverse();

ret.push_str("### History\n\n");

for h in reversed_history.into_iter() {
ret.push_str("* ");
ret.push_str(Message::display_body(&h));
ret.push_str("\n");
}
}

ret
}

pub fn style(&self) -> Style {
match &self.body {
Text(_) => Style::default(),
Expand Down Expand Up @@ -88,6 +144,7 @@ impl Message {
body,
history: vec![],
sender: c.sender.to_string(),
sender_id: c.sender.to_string(),
reactions: Vec::new(),
});
}
Expand Down Expand Up @@ -214,24 +271,21 @@ impl Message {

let wrapped = textwrap::wrap(&self.display(), width);

for l in wrapped {
lines.extend(Text::styled(l, self.style()))
for l in wrapped.iter().take(10) {
lines.extend(Text::styled(l.to_string(), self.style()))
}

// reactions
for r in &self.reactions {
let line = if let Some(emoji) = emojis::get(&r.body) {
if let Some(shortcode) = emoji.shortcode() {
format!("{} ({})", pad_emoji(&r.body), shortcode)
} else {
pad_emoji(&r.body)
}
} else {
pad_emoji(&r.body)
};

let line = format!("{} {}", line, r.pretty_senders());
// overflow warning
if wrapped.len() > 10 || self.reactions.len() > 5 {
lines.extend(Text::styled(
"* overflow: type \"O\" to view entire message",
Style::default().fg(Color::Red),
))
}

// reactions
for r in self.reactions.iter().take(5) {
let line = format!("{} {}", r.display(), r.pretty_senders());
lines.extend(Text::styled(line, Style::default().fg(Color::DarkGray)))
}

Expand Down Expand Up @@ -273,6 +327,18 @@ impl Reaction {
merged
}

pub fn display(&self) -> String {
if let Some(emoji) = emojis::get(&self.body) {
if let Some(shortcode) = emoji.shortcode() {
format!("{} ({})", pad_emoji(&self.body), shortcode)
} else {
pad_emoji(&self.body)
}
} else {
pad_emoji(&self.body)
}
}

pub fn pretty_senders(&self) -> &str {
self.pretty_senders.get_or_init(|| {
let all: Vec<&str> = self
Expand Down

0 comments on commit 76646b3

Please sign in to comment.