58 lines
1.9 KiB
Rust
58 lines
1.9 KiB
Rust
|
|
use std::fmt::Display;
|
||
|
|
|
||
|
|
use pretty::RcDoc;
|
||
|
|
use kordophone::model::Conversation;
|
||
|
|
|
||
|
|
pub struct ConversationPrinter<'a> {
|
||
|
|
doc: RcDoc<'a, Conversation>
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'a> ConversationPrinter<'a> {
|
||
|
|
pub fn new(conversation: &'a Conversation) -> Self {
|
||
|
|
let preview = conversation.last_message_preview
|
||
|
|
.as_deref()
|
||
|
|
.unwrap_or("<null>")
|
||
|
|
.replace('\n', " ");
|
||
|
|
|
||
|
|
let doc = RcDoc::text(format!("<Conversation: \"{}\"", &conversation.guid))
|
||
|
|
.append(
|
||
|
|
RcDoc::line()
|
||
|
|
.append("Display Name: ")
|
||
|
|
.append(conversation.display_name.as_deref().unwrap_or("<null>"))
|
||
|
|
.append(RcDoc::line())
|
||
|
|
.append("Date: ")
|
||
|
|
.append(conversation.date.to_string())
|
||
|
|
.append(RcDoc::line())
|
||
|
|
.append("Participants: ")
|
||
|
|
.append("[")
|
||
|
|
.append(RcDoc::line()
|
||
|
|
.append(
|
||
|
|
conversation.participant_display_names
|
||
|
|
.iter()
|
||
|
|
.map(|name|
|
||
|
|
RcDoc::text(name)
|
||
|
|
.append(",")
|
||
|
|
.append(RcDoc::line())
|
||
|
|
)
|
||
|
|
.fold(RcDoc::nil(), |acc, x| acc.append(x))
|
||
|
|
)
|
||
|
|
.nest(4)
|
||
|
|
)
|
||
|
|
.append("]")
|
||
|
|
.append(RcDoc::line())
|
||
|
|
.append("Last Message Preview: ")
|
||
|
|
.append(preview)
|
||
|
|
.nest(4)
|
||
|
|
)
|
||
|
|
.append(RcDoc::line())
|
||
|
|
.append(">");
|
||
|
|
|
||
|
|
ConversationPrinter { doc }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'a> Display for ConversationPrinter<'a> {
|
||
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
|
|
self.doc.render_fmt(180, f)
|
||
|
|
}
|
||
|
|
}
|