mirror of
https://github.com/youwen5/iamb.git
synced 2025-06-20 13:49:52 -07:00
Support sending and displaying formatted messages (#10)
This commit is contained in:
parent
8966644f6e
commit
4f2261e66f
11 changed files with 1770 additions and 233 deletions
942
src/message/html.rs
Normal file
942
src/message/html.rs
Normal file
|
@ -0,0 +1,942 @@
|
|||
//! # Rendering for formatted bodies
|
||||
//!
|
||||
//! This module contains the code for rendering messages that contained an
|
||||
//! "org.matrix.custom.html"-formatted body.
|
||||
//!
|
||||
//! The Matrix specification recommends limiting rendered tags and attributes to a safe subset of
|
||||
//! HTML. You can read more in section 11.2.1.1, "m.room.message msgtypes":
|
||||
//!
|
||||
//! https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes
|
||||
//!
|
||||
//! This isn't as important for iamb, since it isn't a browser environment, but we do still map
|
||||
//! input onto an enum of the safe list of tags to keep it easy to understand and process.
|
||||
use std::ops::Deref;
|
||||
|
||||
use css_color_parser::Color as CssColor;
|
||||
use markup5ever_rcdom::{Handle, NodeData, RcDom};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use html5ever::{
|
||||
driver::{parse_fragment, ParseOpts},
|
||||
interface::{Attribute, QualName},
|
||||
local_name,
|
||||
namespace_url,
|
||||
ns,
|
||||
tendril::{StrTendril, TendrilSink},
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
layout::Alignment,
|
||||
style::{Color, Modifier as StyleModifier, Style},
|
||||
symbols::line,
|
||||
text::{Span, Spans, Text},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
message::printer::TextPrinter,
|
||||
util::{join_cell_text, space_text},
|
||||
};
|
||||
|
||||
struct BulletIterator {
|
||||
style: ListStyle,
|
||||
pos: usize,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl BulletIterator {
|
||||
fn width(&self) -> usize {
|
||||
match self.style {
|
||||
ListStyle::Unordered => 2,
|
||||
ListStyle::Ordered => self.len.to_string().len() + 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for BulletIterator {
|
||||
type Item = String;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.pos == self.len {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.pos += 1;
|
||||
|
||||
let bullet = match self.style {
|
||||
ListStyle::Unordered => "- ".to_string(),
|
||||
ListStyle::Ordered => {
|
||||
let w = self.len.to_string().len();
|
||||
format!("{: >w$}. ", self.pos, w = w)
|
||||
},
|
||||
};
|
||||
|
||||
return Some(bullet);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ListStyle {
|
||||
Ordered,
|
||||
Unordered,
|
||||
}
|
||||
|
||||
impl ListStyle {
|
||||
fn bullets(&self, len: usize) -> BulletIterator {
|
||||
BulletIterator { style: *self, pos: 0, len }
|
||||
}
|
||||
}
|
||||
|
||||
pub type StyleTreeChildren = Vec<StyleTreeNode>;
|
||||
|
||||
pub enum CellType {
|
||||
Data,
|
||||
Header,
|
||||
}
|
||||
|
||||
pub struct TableRow {
|
||||
cells: Vec<(CellType, StyleTreeNode)>,
|
||||
}
|
||||
|
||||
impl TableRow {
|
||||
fn columns(&self) -> usize {
|
||||
self.cells.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TableSection {
|
||||
rows: Vec<TableRow>,
|
||||
}
|
||||
|
||||
impl TableSection {
|
||||
fn columns(&self) -> usize {
|
||||
self.rows.iter().map(TableRow::columns).max().unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Table {
|
||||
caption: Option<Box<StyleTreeNode>>,
|
||||
sections: Vec<TableSection>,
|
||||
}
|
||||
|
||||
impl Table {
|
||||
fn columns(&self) -> usize {
|
||||
self.sections.iter().map(TableSection::columns).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
fn to_text(&self, width: usize, style: Style) -> Text {
|
||||
let mut text = Text::default();
|
||||
let columns = self.columns();
|
||||
let cell_total = width.saturating_sub(columns).saturating_sub(1);
|
||||
let cell_min = cell_total / columns;
|
||||
let mut cell_slop = cell_total - cell_min * columns;
|
||||
let cell_widths = (0..columns)
|
||||
.into_iter()
|
||||
.map(|_| {
|
||||
let slopped = cell_slop.min(1);
|
||||
cell_slop -= slopped;
|
||||
cell_min + slopped
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut nrows = 0;
|
||||
|
||||
if let Some(caption) = &self.caption {
|
||||
let subw = width.saturating_sub(6);
|
||||
let mut printer = TextPrinter::new(subw, style, true).align(Alignment::Center);
|
||||
caption.print(&mut printer, style);
|
||||
|
||||
for mut line in printer.finish().lines {
|
||||
line.0.insert(0, Span::styled(" ", style));
|
||||
line.0.push(Span::styled(" ", style));
|
||||
text.lines.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
for section in self.sections.iter() {
|
||||
for row in section.rows.iter() {
|
||||
let mut ruler = String::new();
|
||||
|
||||
for (i, w) in cell_widths.iter().enumerate() {
|
||||
let cross = match (nrows, i) {
|
||||
(0, 0) => line::TOP_LEFT,
|
||||
(0, _) => line::HORIZONTAL_DOWN,
|
||||
(_, 0) => line::VERTICAL_RIGHT,
|
||||
(_, _) => line::CROSS,
|
||||
};
|
||||
|
||||
ruler.push_str(cross);
|
||||
|
||||
for _ in 0..*w {
|
||||
ruler.push_str(line::HORIZONTAL);
|
||||
}
|
||||
}
|
||||
|
||||
if nrows == 0 {
|
||||
ruler.push_str(line::TOP_RIGHT);
|
||||
} else {
|
||||
ruler.push_str(line::VERTICAL_LEFT);
|
||||
}
|
||||
|
||||
text.lines.push(Spans(vec![Span::styled(ruler, style)]));
|
||||
|
||||
let cells = cell_widths
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, w)| {
|
||||
let text = if let Some((kind, cell)) = row.cells.get(i) {
|
||||
let style = match kind {
|
||||
CellType::Header => style.add_modifier(StyleModifier::BOLD),
|
||||
CellType::Data => style,
|
||||
};
|
||||
|
||||
cell.to_text(*w, style)
|
||||
} else {
|
||||
space_text(*w, style)
|
||||
};
|
||||
|
||||
(text, *w)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let joined = join_cell_text(cells, Span::styled(line::VERTICAL, style));
|
||||
text.lines.extend(joined.lines);
|
||||
|
||||
nrows += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if nrows > 0 {
|
||||
let mut ruler = String::new();
|
||||
|
||||
for (i, w) in cell_widths.iter().enumerate() {
|
||||
let cross = if i == 0 {
|
||||
line::BOTTOM_LEFT
|
||||
} else {
|
||||
line::HORIZONTAL_UP
|
||||
};
|
||||
|
||||
ruler.push_str(cross);
|
||||
|
||||
for _ in 0..*w {
|
||||
ruler.push_str(line::HORIZONTAL);
|
||||
}
|
||||
}
|
||||
|
||||
ruler.push_str(line::BOTTOM_RIGHT);
|
||||
text.lines.push(Spans(vec![Span::styled(ruler, style)]));
|
||||
}
|
||||
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
pub enum StyleTreeNode {
|
||||
Blockquote(Box<StyleTreeNode>),
|
||||
Break,
|
||||
Code(Box<StyleTreeNode>, Option<String>),
|
||||
Header(Box<StyleTreeNode>, usize),
|
||||
Image(Option<String>),
|
||||
List(StyleTreeChildren, ListStyle),
|
||||
Paragraph(Box<StyleTreeNode>),
|
||||
Reply(Box<StyleTreeNode>),
|
||||
Ruler,
|
||||
Style(Box<StyleTreeNode>, Style),
|
||||
Table(Table),
|
||||
Text(String),
|
||||
Sequence(StyleTreeChildren),
|
||||
}
|
||||
|
||||
impl StyleTreeNode {
|
||||
pub fn to_text(&self, width: usize, style: Style) -> Text {
|
||||
let mut printer = TextPrinter::new(width, style, true);
|
||||
self.print(&mut printer, style);
|
||||
printer.finish()
|
||||
}
|
||||
|
||||
pub fn print<'a>(&'a self, printer: &mut TextPrinter<'a>, style: Style) {
|
||||
let width = printer.width();
|
||||
|
||||
match self {
|
||||
StyleTreeNode::Blockquote(child) => {
|
||||
let mut subp = printer.sub(4);
|
||||
child.print(&mut subp, style);
|
||||
|
||||
for mut line in subp.finish() {
|
||||
line.0.insert(0, Span::styled(" ", style));
|
||||
printer.push_line(line);
|
||||
}
|
||||
},
|
||||
StyleTreeNode::Code(child, _) => {
|
||||
child.print(printer, style);
|
||||
},
|
||||
StyleTreeNode::Header(child, level) => {
|
||||
let style = style.add_modifier(StyleModifier::BOLD);
|
||||
let mut hashes = "#".repeat(*level);
|
||||
hashes.push(' ');
|
||||
|
||||
printer.push_str(hashes, style);
|
||||
child.print(printer, style);
|
||||
},
|
||||
StyleTreeNode::Image(None) => {},
|
||||
StyleTreeNode::Image(Some(alt)) => {
|
||||
printer.commit();
|
||||
printer.push_str("Image Alt: ", Style::default());
|
||||
printer.push_str(alt, Style::default());
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::List(children, lt) => {
|
||||
let mut bullets = lt.bullets(children.len());
|
||||
let liw = bullets.width();
|
||||
|
||||
for child in children {
|
||||
let mut subp = printer.sub(liw);
|
||||
let mut bullet = bullets.next();
|
||||
child.print(&mut subp, style);
|
||||
|
||||
for mut line in subp.finish() {
|
||||
let leading = if let Some(bullet) = bullet.take() {
|
||||
Span::styled(bullet, style)
|
||||
} else {
|
||||
Span::styled(" ".repeat(liw), style)
|
||||
};
|
||||
|
||||
line.0.insert(0, leading);
|
||||
printer.push_line(line);
|
||||
}
|
||||
}
|
||||
},
|
||||
StyleTreeNode::Paragraph(child) => {
|
||||
printer.push_break();
|
||||
child.print(printer, style);
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Reply(child) => {
|
||||
if printer.hide_reply() {
|
||||
return;
|
||||
}
|
||||
|
||||
printer.push_break();
|
||||
child.print(printer, style);
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Ruler => {
|
||||
printer.push_str(line::HORIZONTAL.repeat(width), style);
|
||||
},
|
||||
StyleTreeNode::Table(table) => {
|
||||
let text = table.to_text(width, style);
|
||||
printer.push_text(text);
|
||||
},
|
||||
StyleTreeNode::Break => {
|
||||
printer.push_break();
|
||||
},
|
||||
StyleTreeNode::Text(s) => {
|
||||
printer.push_str(s.as_str(), style);
|
||||
},
|
||||
|
||||
StyleTreeNode::Style(child, patch) => child.print(printer, style.patch(*patch)),
|
||||
StyleTreeNode::Sequence(children) => {
|
||||
for child in children {
|
||||
child.print(printer, style);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StyleTree {
|
||||
children: StyleTreeChildren,
|
||||
}
|
||||
|
||||
impl StyleTree {
|
||||
pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> {
|
||||
let mut printer = TextPrinter::new(width, style, hide_reply);
|
||||
|
||||
for child in self.children.iter() {
|
||||
child.print(&mut printer, style);
|
||||
}
|
||||
|
||||
printer.finish()
|
||||
}
|
||||
}
|
||||
|
||||
fn c2c(handles: &[Handle]) -> Vec<StyleTreeNode> {
|
||||
handles.iter().flat_map(h2t).collect()
|
||||
}
|
||||
|
||||
fn c2t(handles: &[Handle]) -> Box<StyleTreeNode> {
|
||||
let node = StyleTreeNode::Sequence(c2c(handles));
|
||||
|
||||
Box::new(node)
|
||||
}
|
||||
|
||||
fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> {
|
||||
let node = hdl.deref();
|
||||
|
||||
if let NodeData::Element { name, .. } = &node.data {
|
||||
if name.local.as_ref() != want {
|
||||
return None;
|
||||
}
|
||||
|
||||
let c = c2c(&node.children.borrow());
|
||||
return Some(StyleTreeNode::Sequence(c));
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
fn li2t(hdl: &Handle) -> Option<StyleTreeNode> {
|
||||
get_node(hdl, "li")
|
||||
}
|
||||
|
||||
fn table_cell(hdl: &Handle) -> Option<(CellType, StyleTreeNode)> {
|
||||
if let Some(node) = get_node(hdl, "th") {
|
||||
return Some((CellType::Header, node));
|
||||
}
|
||||
|
||||
Some((CellType::Data, get_node(hdl, "td")?))
|
||||
}
|
||||
|
||||
fn table_row(hdl: &Handle) -> Option<TableRow> {
|
||||
let node = hdl.deref();
|
||||
|
||||
if let NodeData::Element { name, .. } = &node.data {
|
||||
if name.local.as_ref() != "tr" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let cells = table_cells(&node.children.borrow());
|
||||
return Some(TableRow { cells });
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
fn table_section(hdl: &Handle) -> Option<TableSection> {
|
||||
let node = hdl.deref();
|
||||
|
||||
if let NodeData::Element { name, .. } = &node.data {
|
||||
match name.local.as_ref() {
|
||||
"thead" | "tbody" => {
|
||||
let rows = table_rows(&node.children.borrow());
|
||||
|
||||
Some(TableSection { rows })
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
fn table_cells(handles: &[Handle]) -> Vec<(CellType, StyleTreeNode)> {
|
||||
handles.iter().filter_map(table_cell).collect()
|
||||
}
|
||||
|
||||
fn table_rows(handles: &[Handle]) -> Vec<TableRow> {
|
||||
handles.iter().filter_map(table_row).collect()
|
||||
}
|
||||
|
||||
fn table_sections(handles: &[Handle]) -> Vec<TableSection> {
|
||||
handles.iter().filter_map(table_section).collect()
|
||||
}
|
||||
|
||||
fn lic2t(handles: &[Handle]) -> StyleTreeChildren {
|
||||
handles.iter().filter_map(li2t).collect()
|
||||
}
|
||||
|
||||
fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> {
|
||||
for attr in attrs {
|
||||
if attr.name.local.as_ref() != "alt" {
|
||||
continue;
|
||||
}
|
||||
|
||||
return Some(attr.value.to_string());
|
||||
}
|
||||
|
||||
return None;
|
||||
}
|
||||
|
||||
fn attrs_to_language(attrs: &[Attribute]) -> Option<String> {
|
||||
for attr in attrs {
|
||||
if attr.name.local.as_ref() != "class" {
|
||||
continue;
|
||||
}
|
||||
|
||||
for class in attr.value.as_ref().unicode_words() {
|
||||
if class.len() > 9 && class.starts_with("language-") {
|
||||
return Some(class[9..].to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None;
|
||||
}
|
||||
|
||||
fn attrs_to_style(attrs: &[Attribute]) -> Style {
|
||||
let mut style = Style::default();
|
||||
|
||||
for attr in attrs {
|
||||
match attr.name.local.as_ref() {
|
||||
"data-mx-bg-color" => {
|
||||
if let Ok(rgb) = attr.value.as_ref().parse::<CssColor>() {
|
||||
let color = Color::Rgb(rgb.r, rgb.g, rgb.b);
|
||||
style = style.bg(color);
|
||||
}
|
||||
},
|
||||
"data-mx-color" | "color" => {
|
||||
if let Ok(rgb) = attr.value.as_ref().parse::<CssColor>() {
|
||||
let color = Color::Rgb(rgb.r, rgb.g, rgb.b);
|
||||
style = style.fg(color);
|
||||
}
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
return style;
|
||||
}
|
||||
|
||||
fn h2t(hdl: &Handle) -> StyleTreeChildren {
|
||||
let node = hdl.deref();
|
||||
|
||||
let tree = match &node.data {
|
||||
NodeData::Document => *c2t(node.children.borrow().as_slice()),
|
||||
NodeData::Text { contents } => StyleTreeNode::Text(contents.borrow().to_string()),
|
||||
NodeData::Element { name, attrs, .. } => {
|
||||
match name.local.as_ref() {
|
||||
// Message that this one replies to.
|
||||
"mx-reply" => StyleTreeNode::Reply(c2t(&node.children.borrow())),
|
||||
|
||||
// Style change
|
||||
"b" | "strong" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = Style::default().add_modifier(StyleModifier::BOLD);
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
"font" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = attrs_to_style(&attrs.borrow());
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
"em" | "i" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = Style::default().add_modifier(StyleModifier::ITALIC);
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
"span" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = attrs_to_style(&attrs.borrow());
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
"del" | "strike" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = Style::default().add_modifier(StyleModifier::CROSSED_OUT);
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
"u" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let s = Style::default().add_modifier(StyleModifier::UNDERLINED);
|
||||
|
||||
StyleTreeNode::Style(c, s)
|
||||
},
|
||||
|
||||
// Lists
|
||||
"ol" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Ordered),
|
||||
"ul" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Unordered),
|
||||
|
||||
// Headers
|
||||
"h1" => StyleTreeNode::Header(c2t(&node.children.borrow()), 1),
|
||||
"h2" => StyleTreeNode::Header(c2t(&node.children.borrow()), 2),
|
||||
"h3" => StyleTreeNode::Header(c2t(&node.children.borrow()), 3),
|
||||
"h4" => StyleTreeNode::Header(c2t(&node.children.borrow()), 4),
|
||||
"h5" => StyleTreeNode::Header(c2t(&node.children.borrow()), 5),
|
||||
"h6" => StyleTreeNode::Header(c2t(&node.children.borrow()), 6),
|
||||
|
||||
// Table
|
||||
"table" => {
|
||||
let sections = table_sections(&node.children.borrow());
|
||||
let caption = node
|
||||
.children
|
||||
.borrow()
|
||||
.iter()
|
||||
.find_map(|hdl| get_node(hdl, "caption"))
|
||||
.map(Box::new);
|
||||
let table = Table { caption, sections };
|
||||
|
||||
StyleTreeNode::Table(table)
|
||||
},
|
||||
|
||||
// Code blocks.
|
||||
"code" => {
|
||||
let c = c2t(&node.children.borrow());
|
||||
let l = attrs_to_language(&attrs.borrow());
|
||||
|
||||
StyleTreeNode::Code(c, l)
|
||||
},
|
||||
|
||||
// Other text blocks.
|
||||
"blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())),
|
||||
"div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())),
|
||||
|
||||
// No children.
|
||||
"hr" => StyleTreeNode::Ruler,
|
||||
"br" => StyleTreeNode::Break,
|
||||
|
||||
"img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())),
|
||||
|
||||
// These don't render in any special way.
|
||||
"a" | "details" | "html" | "pre" | "summary" | "sub" | "sup" => {
|
||||
*c2t(&node.children.borrow())
|
||||
},
|
||||
|
||||
_ => return vec![],
|
||||
}
|
||||
},
|
||||
|
||||
// These don't render as anything.
|
||||
NodeData::Doctype { .. } => return vec![],
|
||||
NodeData::Comment { .. } => return vec![],
|
||||
NodeData::ProcessingInstruction { .. } => return vec![],
|
||||
};
|
||||
|
||||
vec![tree]
|
||||
}
|
||||
|
||||
fn dom_to_style_tree(dom: RcDom) -> StyleTree {
|
||||
StyleTree { children: h2t(&dom.document) }
|
||||
}
|
||||
|
||||
pub fn parse_matrix_html(s: &str) -> StyleTree {
|
||||
let dom = parse_fragment(
|
||||
RcDom::default(),
|
||||
ParseOpts::default(),
|
||||
QualName::new(None, ns!(), local_name!("div")),
|
||||
vec![],
|
||||
)
|
||||
.one(StrTendril::from(s));
|
||||
|
||||
dom_to_style_tree(dom)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::util::space_span;
|
||||
|
||||
#[test]
|
||||
fn test_header() {
|
||||
let bold = Style::default().add_modifier(StyleModifier::BOLD);
|
||||
|
||||
let s = "<h1>Header 1</h1>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("# ", bold),
|
||||
Span::styled("Header 1", bold),
|
||||
space_span(10, Style::default())
|
||||
])]);
|
||||
|
||||
let s = "<h2>Header 2</h2>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("## ", bold),
|
||||
Span::styled("Header 2", bold),
|
||||
space_span(9, Style::default())
|
||||
])]);
|
||||
|
||||
let s = "<h3>Header 3</h3>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("### ", bold),
|
||||
Span::styled("Header 3", bold),
|
||||
space_span(8, Style::default())
|
||||
])]);
|
||||
|
||||
let s = "<h4>Header 4</h4>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("#### ", bold),
|
||||
Span::styled("Header 4", bold),
|
||||
space_span(7, Style::default())
|
||||
])]);
|
||||
|
||||
let s = "<h5>Header 5</h5>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("##### ", bold),
|
||||
Span::styled("Header 5", bold),
|
||||
space_span(6, Style::default())
|
||||
])]);
|
||||
|
||||
let s = "<h6>Header 6</h6>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("###### ", bold),
|
||||
Span::styled("Header 6", bold),
|
||||
space_span(5, Style::default())
|
||||
])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style() {
|
||||
let def = Style::default();
|
||||
let bold = def.add_modifier(StyleModifier::BOLD);
|
||||
let italic = def.add_modifier(StyleModifier::ITALIC);
|
||||
let strike = def.add_modifier(StyleModifier::CROSSED_OUT);
|
||||
let underl = def.add_modifier(StyleModifier::UNDERLINED);
|
||||
let red = def.fg(Color::Rgb(0xff, 0x00, 0x00));
|
||||
|
||||
let s = "<b>Bold!</b>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Bold!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
let s = "<strong>Bold!</strong>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Bold!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
let s = "<i>Italic!</i>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Italic!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
let s = "<em>Italic!</em>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Italic!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
let s = "<del>Strikethrough!</del>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Strikethrough!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
let s = "<strike>Strikethrough!</strike>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Strikethrough!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
let s = "<u>Underline!</u>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Underline!", underl),
|
||||
space_span(10, def)
|
||||
])]);
|
||||
|
||||
let s = "<font color=\"#ff0000\">Red!</u>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
|
||||
|
||||
let s = "<font color=\"red\">Red!</u>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_paragraph() {
|
||||
let s = "<p>Hello world!</p><p>Content</p><p>Goodbye world!</p>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 7);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("Hello worl")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("d!"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw(" ")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw("Content"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw(" ")]));
|
||||
assert_eq!(text.lines[5], Spans(vec![Span::raw("Goodbye wo")]));
|
||||
assert_eq!(text.lines[6], Spans(vec![Span::raw("rld!"), Span::raw(" ")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blockquote() {
|
||||
let s = "<blockquote>Hello world!</blockquote>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 2);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw(" "), Span::raw("Hello ")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("world!")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_unordered() {
|
||||
let s = "<ul><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ul>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(8, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 6);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[5], Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_ordered() {
|
||||
let s = "<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li></ol>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(9, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 6);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("1. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("2. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw("3. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table() {
|
||||
let s = "<table>\
|
||||
<thead>\
|
||||
<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>
|
||||
</thead>\
|
||||
<tbody>\
|
||||
<tr><td>a</td><td>b</td><td>c</td></tr>\
|
||||
<tr><td>a</td><td>b</td><td>c</td></tr>\
|
||||
<tr><td>a</td><td>b</td><td>c</td></tr>\
|
||||
</tbody></table>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(15, Style::default(), false);
|
||||
let bold = Style::default().add_modifier(StyleModifier::BOLD);
|
||||
assert_eq!(text.lines.len(), 11);
|
||||
|
||||
// Table header
|
||||
assert_eq!(text.lines[0].0, vec![Span::raw("┌────┬────┬───┐")]);
|
||||
assert_eq!(text.lines[1].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::styled("Colu", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("Colu", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("Col", bold),
|
||||
Span::raw("│")
|
||||
]);
|
||||
assert_eq!(text.lines[2].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 1", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 2", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("umn", bold),
|
||||
Span::raw("│")
|
||||
]);
|
||||
assert_eq!(text.lines[3].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::styled(" 3", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::raw("│")
|
||||
]);
|
||||
|
||||
// First row
|
||||
assert_eq!(text.lines[4].0, vec![Span::raw("├────┼────┼───┤")]);
|
||||
assert_eq!(text.lines[5].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::raw("a"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("b"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("c"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│")
|
||||
]);
|
||||
|
||||
// Second row
|
||||
assert_eq!(text.lines[6].0, vec![Span::raw("├────┼────┼───┤")]);
|
||||
assert_eq!(text.lines[7].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::raw("a"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("b"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("c"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│")
|
||||
]);
|
||||
|
||||
// Third row
|
||||
assert_eq!(text.lines[8].0, vec![Span::raw("├────┼────┼───┤")]);
|
||||
assert_eq!(text.lines[9].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::raw("a"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("b"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::raw("c"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│")
|
||||
]);
|
||||
|
||||
// Bottom ruler
|
||||
assert_eq!(text.lines[10].0, vec![Span::raw("└────┴────┴───┘")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matrix_reply() {
|
||||
let s = "<mx-reply>This was replied to</mx-reply>This is the reply";
|
||||
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 4);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("This was r")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("eplied to"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("This is th")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
|
||||
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), true);
|
||||
assert_eq!(text.lines.len(), 2);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("This is th")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
|
||||
}
|
||||
}
|
729
src/message/mod.rs
Normal file
729
src/message/mod.rs
Normal file
|
@ -0,0 +1,729 @@
|
|||
use std::borrow::Cow;
|
||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use matrix_sdk::ruma::{
|
||||
events::{
|
||||
room::{
|
||||
message::{
|
||||
FormattedBody,
|
||||
MessageFormat,
|
||||
MessageType,
|
||||
OriginalRoomMessageEvent,
|
||||
RedactedRoomMessageEvent,
|
||||
Relation,
|
||||
RoomMessageEvent,
|
||||
RoomMessageEventContent,
|
||||
},
|
||||
redaction::SyncRoomRedactionEvent,
|
||||
},
|
||||
Redact,
|
||||
},
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
OwnedEventId,
|
||||
OwnedUserId,
|
||||
RoomVersionId,
|
||||
UInt,
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
style::{Modifier as StyleModifier, Style},
|
||||
symbols::line::THICK_VERTICAL,
|
||||
text::{Span, Spans, Text},
|
||||
};
|
||||
|
||||
use modalkit::editing::{base::ViewportContext, cursor::Cursor};
|
||||
|
||||
use crate::{
|
||||
base::{IambResult, RoomInfo},
|
||||
config::ApplicationSettings,
|
||||
message::html::{parse_matrix_html, StyleTree},
|
||||
util::{space_span, wrapped_text},
|
||||
};
|
||||
|
||||
mod html;
|
||||
mod printer;
|
||||
|
||||
pub type MessageFetchResult = IambResult<(Option<String>, Vec<RoomMessageEvent>)>;
|
||||
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
|
||||
pub type Messages = BTreeMap<MessageKey, Message>;
|
||||
|
||||
const USER_GUTTER: usize = 30;
|
||||
const TIME_GUTTER: usize = 12;
|
||||
const MIN_MSG_LEN: usize = 30;
|
||||
|
||||
const USER_GUTTER_EMPTY: &str = " ";
|
||||
const USER_GUTTER_EMPTY_SPAN: Span<'static> = Span {
|
||||
content: Cow::Borrowed(USER_GUTTER_EMPTY),
|
||||
style: Style {
|
||||
fg: None,
|
||||
bg: None,
|
||||
add_modifier: StyleModifier::empty(),
|
||||
sub_modifier: StyleModifier::empty(),
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum TimeStampIntError {
|
||||
#[error("Integer conversion error: {0}")]
|
||||
IntError(#[from] std::num::TryFromIntError),
|
||||
|
||||
#[error("UInt conversion error: {0}")]
|
||||
UIntError(<UInt as TryFrom<u64>>::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum MessageTimeStamp {
|
||||
OriginServer(UInt),
|
||||
LocalEcho,
|
||||
}
|
||||
|
||||
impl MessageTimeStamp {
|
||||
fn show(&self) -> Option<Span> {
|
||||
match self {
|
||||
MessageTimeStamp::OriginServer(ts) => {
|
||||
let time = i64::from(*ts) / 1000;
|
||||
let time = NaiveDateTime::from_timestamp_opt(time, 0)?;
|
||||
let time = DateTime::<Utc>::from_utc(time, Utc);
|
||||
let time = time.format("%T");
|
||||
let time = format!(" [{}]", time);
|
||||
|
||||
Span::raw(time).into()
|
||||
},
|
||||
MessageTimeStamp::LocalEcho => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_local_echo(&self) -> bool {
|
||||
matches!(self, MessageTimeStamp::LocalEcho)
|
||||
}
|
||||
|
||||
pub fn as_millis(&self) -> Option<MilliSecondsSinceUnixEpoch> {
|
||||
match self {
|
||||
MessageTimeStamp::OriginServer(ms) => MilliSecondsSinceUnixEpoch(*ms).into(),
|
||||
MessageTimeStamp::LocalEcho => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for MessageTimeStamp {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match (self, other) {
|
||||
(MessageTimeStamp::OriginServer(_), MessageTimeStamp::LocalEcho) => Ordering::Less,
|
||||
(MessageTimeStamp::OriginServer(a), MessageTimeStamp::OriginServer(b)) => a.cmp(b),
|
||||
(MessageTimeStamp::LocalEcho, MessageTimeStamp::OriginServer(_)) => Ordering::Greater,
|
||||
(MessageTimeStamp::LocalEcho, MessageTimeStamp::LocalEcho) => Ordering::Equal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for MessageTimeStamp {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
|
||||
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
|
||||
MessageTimeStamp::OriginServer(millis.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&MessageTimeStamp> for usize {
|
||||
type Error = TimeStampIntError;
|
||||
|
||||
fn try_from(ts: &MessageTimeStamp) -> Result<Self, Self::Error> {
|
||||
let n = match ts {
|
||||
MessageTimeStamp::LocalEcho => 0,
|
||||
MessageTimeStamp::OriginServer(u) => usize::try_from(u64::from(*u))?,
|
||||
};
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<usize> for MessageTimeStamp {
|
||||
type Error = TimeStampIntError;
|
||||
|
||||
fn try_from(u: usize) -> Result<Self, Self::Error> {
|
||||
if u == 0 {
|
||||
Ok(MessageTimeStamp::LocalEcho)
|
||||
} else {
|
||||
let n = u64::try_from(u)?;
|
||||
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
|
||||
|
||||
Ok(MessageTimeStamp::OriginServer(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct MessageCursor {
|
||||
/// When timestamp is None, the corner is determined by moving backwards from
|
||||
/// the most recently received message.
|
||||
pub timestamp: Option<MessageKey>,
|
||||
|
||||
/// A row within the [Text] representation of a [Message].
|
||||
pub text_row: usize,
|
||||
}
|
||||
|
||||
impl MessageCursor {
|
||||
pub fn new(timestamp: MessageKey, text_row: usize) -> Self {
|
||||
MessageCursor { timestamp: Some(timestamp), text_row }
|
||||
}
|
||||
|
||||
/// Get a cursor that refers to the most recent message.
|
||||
pub fn latest() -> Self {
|
||||
MessageCursor::default()
|
||||
}
|
||||
|
||||
pub fn to_key<'a>(&'a self, info: &'a RoomInfo) -> Option<&'a MessageKey> {
|
||||
if let Some(ref key) = self.timestamp {
|
||||
Some(key)
|
||||
} else {
|
||||
Some(info.messages.last_key_value()?.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_cursor(cursor: &Cursor, info: &RoomInfo) -> Option<Self> {
|
||||
let ev_hash = u64::try_from(cursor.get_x()).ok()?;
|
||||
let ev_term = OwnedEventId::try_from("$").ok()?;
|
||||
|
||||
let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
|
||||
let start = (ts_start, ev_term);
|
||||
let mut mc = None;
|
||||
|
||||
for ((ts, event_id), _) in info.messages.range(start..) {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
event_id.hash(&mut hasher);
|
||||
|
||||
if hasher.finish() == ev_hash {
|
||||
mc = Self::from((*ts, event_id.clone())).into();
|
||||
break;
|
||||
}
|
||||
|
||||
if mc.is_none() {
|
||||
mc = Self::from((*ts, event_id.clone())).into();
|
||||
}
|
||||
|
||||
if ts > &ts_start {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return mc;
|
||||
}
|
||||
|
||||
pub fn to_cursor(&self, info: &RoomInfo) -> Option<Cursor> {
|
||||
let (ts, event_id) = self.to_key(info)?;
|
||||
|
||||
let y: usize = usize::try_from(ts).ok()?;
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
event_id.hash(&mut hasher);
|
||||
let x = usize::try_from(hasher.finish()).ok()?;
|
||||
|
||||
Cursor::new(y, x).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<MessageKey>> for MessageCursor {
|
||||
fn from(key: Option<MessageKey>) -> Self {
|
||||
MessageCursor { timestamp: key, text_row: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MessageKey> for MessageCursor {
|
||||
fn from(key: MessageKey) -> Self {
|
||||
MessageCursor { timestamp: Some(key), text_row: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for MessageCursor {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match (&self.timestamp, &other.timestamp) {
|
||||
(None, None) => self.text_row.cmp(&other.text_row),
|
||||
(None, Some(_)) => Ordering::Greater,
|
||||
(Some(_), None) => Ordering::Less,
|
||||
(Some(st), Some(ot)) => {
|
||||
let pcmp = st.cmp(ot);
|
||||
let tcmp = self.text_row.cmp(&other.text_row);
|
||||
|
||||
pcmp.then(tcmp)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for MessageCursor {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum MessageEvent {
|
||||
Original(Box<OriginalRoomMessageEvent>),
|
||||
Redacted(Box<RedactedRoomMessageEvent>),
|
||||
Local(Box<RoomMessageEventContent>),
|
||||
}
|
||||
|
||||
impl MessageEvent {
|
||||
pub fn body(&self) -> Cow<'_, str> {
|
||||
match self {
|
||||
MessageEvent::Original(ev) => body_cow_content(&ev.content),
|
||||
MessageEvent::Redacted(ev) => {
|
||||
let reason = ev
|
||||
.unsigned
|
||||
.redacted_because
|
||||
.as_ref()
|
||||
.and_then(|e| e.as_original())
|
||||
.and_then(|r| r.content.reason.as_ref());
|
||||
|
||||
if let Some(r) = reason {
|
||||
Cow::Owned(format!("[Redacted: {:?}]", r))
|
||||
} else {
|
||||
Cow::Borrowed("[Redacted]")
|
||||
}
|
||||
},
|
||||
MessageEvent::Local(content) => body_cow_content(content),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn html(&self) -> Option<StyleTree> {
|
||||
let content = match self {
|
||||
MessageEvent::Original(ev) => &ev.content,
|
||||
MessageEvent::Redacted(_) => return None,
|
||||
MessageEvent::Local(content) => content,
|
||||
};
|
||||
|
||||
if let MessageType::Text(content) = &content.msgtype {
|
||||
if let Some(FormattedBody { format: MessageFormat::Html, body }) = &content.formatted {
|
||||
Some(parse_matrix_html(body.as_str()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
||||
match self {
|
||||
MessageEvent::Redacted(_) => return,
|
||||
MessageEvent::Local(_) => return,
|
||||
MessageEvent::Original(ev) => {
|
||||
let redacted = ev.clone().redact(redaction, version);
|
||||
*self = MessageEvent::Redacted(Box::new(redacted));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
||||
let s = match &content.msgtype {
|
||||
MessageType::Text(content) => content.body.as_str(),
|
||||
MessageType::VerificationRequest(_) => "[Verification Request]",
|
||||
MessageType::Emote(content) => content.body.as_ref(),
|
||||
MessageType::Notice(content) => content.body.as_str(),
|
||||
MessageType::ServerNotice(content) => content.body.as_str(),
|
||||
|
||||
MessageType::Audio(content) => {
|
||||
return Cow::Owned(format!("[Attached Audio: {}]", content.body));
|
||||
},
|
||||
MessageType::File(content) => {
|
||||
return Cow::Owned(format!("[Attached File: {}]", content.body));
|
||||
},
|
||||
MessageType::Image(content) => {
|
||||
return Cow::Owned(format!("[Attached Image: {}]", content.body));
|
||||
},
|
||||
MessageType::Video(content) => {
|
||||
return Cow::Owned(format!("[Attached Video: {}]", content.body));
|
||||
},
|
||||
_ => {
|
||||
return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype()));
|
||||
},
|
||||
};
|
||||
|
||||
Cow::Borrowed(s)
|
||||
}
|
||||
|
||||
enum MessageColumns<'a> {
|
||||
Three(usize, Option<Span<'a>>, Option<Span<'a>>),
|
||||
Two(usize, Option<Span<'a>>),
|
||||
One(usize, Option<Span<'a>>),
|
||||
}
|
||||
|
||||
impl<'a> MessageColumns<'a> {
|
||||
fn width(&self) -> usize {
|
||||
match self {
|
||||
MessageColumns::Three(fill, _, _) => *fill,
|
||||
MessageColumns::Two(fill, _) => *fill,
|
||||
MessageColumns::One(fill, _) => *fill,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn push_spans(&mut self, spans: Spans<'a>, style: Style, text: &mut Text<'a>) {
|
||||
match self {
|
||||
MessageColumns::Three(_, user, time) => {
|
||||
let user = user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
|
||||
let time = time.take().unwrap_or_else(|| Span::from(""));
|
||||
|
||||
let mut line = vec![user];
|
||||
line.extend(spans.0);
|
||||
line.push(time);
|
||||
|
||||
text.lines.push(Spans(line))
|
||||
},
|
||||
MessageColumns::Two(_, opt) => {
|
||||
let user = opt.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
|
||||
let mut line = vec![user];
|
||||
line.extend(spans.0);
|
||||
|
||||
text.lines.push(Spans(line));
|
||||
},
|
||||
MessageColumns::One(_, opt) => {
|
||||
if let Some(user) = opt.take() {
|
||||
text.lines.push(Spans(vec![user]));
|
||||
}
|
||||
|
||||
let leading = space_span(2, style);
|
||||
let mut line = vec![leading];
|
||||
line.extend(spans.0);
|
||||
|
||||
text.lines.push(Spans(line));
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn push_text(&mut self, append: Text<'a>, style: Style, text: &mut Text<'a>) {
|
||||
for line in append.lines.into_iter() {
|
||||
self.push_spans(line, style, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Message {
|
||||
pub event: MessageEvent,
|
||||
pub sender: OwnedUserId,
|
||||
pub timestamp: MessageTimeStamp,
|
||||
pub downloaded: bool,
|
||||
pub html: Option<StyleTree>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
|
||||
let html = event.html();
|
||||
let downloaded = false;
|
||||
|
||||
Message { event, sender, timestamp, downloaded, html }
|
||||
}
|
||||
|
||||
pub fn reply_to(&self) -> Option<OwnedEventId> {
|
||||
let content = match &self.event {
|
||||
MessageEvent::Local(content) => content,
|
||||
MessageEvent::Original(ev) => &ev.content,
|
||||
MessageEvent::Redacted(_) => return None,
|
||||
};
|
||||
|
||||
if let Some(Relation::Reply { in_reply_to }) = &content.relates_to {
|
||||
Some(in_reply_to.event_id.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_render_style(&self, selected: bool) -> Style {
|
||||
let mut style = Style::default();
|
||||
|
||||
if selected {
|
||||
style = style.add_modifier(StyleModifier::REVERSED)
|
||||
}
|
||||
|
||||
if self.timestamp.is_local_echo() {
|
||||
style = style.add_modifier(StyleModifier::ITALIC);
|
||||
}
|
||||
|
||||
return style;
|
||||
}
|
||||
|
||||
fn get_render_format(
|
||||
&self,
|
||||
prev: Option<&Message>,
|
||||
width: usize,
|
||||
settings: &ApplicationSettings,
|
||||
) -> MessageColumns {
|
||||
if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
|
||||
let lw = width - USER_GUTTER - TIME_GUTTER;
|
||||
let user = self.show_sender(prev, true, settings);
|
||||
let time = self.timestamp.show();
|
||||
|
||||
MessageColumns::Three(lw, user, time)
|
||||
} else if USER_GUTTER + MIN_MSG_LEN <= width {
|
||||
let lw = width - USER_GUTTER;
|
||||
let user = self.show_sender(prev, true, settings);
|
||||
|
||||
MessageColumns::Two(lw, user)
|
||||
} else {
|
||||
let lw = width.saturating_sub(2);
|
||||
let user = self.show_sender(prev, false, settings);
|
||||
|
||||
MessageColumns::One(lw, user)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show<'a>(
|
||||
&'a self,
|
||||
prev: Option<&Message>,
|
||||
selected: bool,
|
||||
vwctx: &ViewportContext<MessageCursor>,
|
||||
info: &'a RoomInfo,
|
||||
settings: &ApplicationSettings,
|
||||
) -> Text<'a> {
|
||||
let width = vwctx.get_width();
|
||||
|
||||
let style = self.get_render_style(selected);
|
||||
let mut fmt = self.get_render_format(prev, width, settings);
|
||||
let mut text = Text { lines: vec![] };
|
||||
let width = fmt.width();
|
||||
|
||||
// Show the message that this one replied to, if any.
|
||||
let reply = self.reply_to().and_then(|e| info.get_event(&e));
|
||||
|
||||
if let Some(r) = &reply {
|
||||
let w = width.saturating_sub(2);
|
||||
let mut replied = r.show_msg(w, style, true);
|
||||
let mut sender = r.sender_span(settings);
|
||||
let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
|
||||
let trailing = w.saturating_sub(sender_width + 1);
|
||||
|
||||
sender.style = sender.style.patch(style);
|
||||
|
||||
fmt.push_spans(
|
||||
Spans(vec![
|
||||
Span::styled(" ", style),
|
||||
Span::styled(THICK_VERTICAL, style),
|
||||
sender,
|
||||
Span::styled(":", style),
|
||||
space_span(trailing, style),
|
||||
]),
|
||||
style,
|
||||
&mut text,
|
||||
);
|
||||
|
||||
for line in replied.lines.iter_mut() {
|
||||
line.0.insert(0, Span::styled(THICK_VERTICAL, style));
|
||||
line.0.insert(0, Span::styled(" ", style));
|
||||
}
|
||||
|
||||
fmt.push_text(replied, style, &mut text);
|
||||
}
|
||||
|
||||
// Now show the message contents, and the inlined reply if we couldn't find it above.
|
||||
let msg = self.show_msg(width, style, reply.is_some());
|
||||
fmt.push_text(msg, style, &mut text);
|
||||
|
||||
if text.lines.is_empty() {
|
||||
// If there was nothing in the body, just show an empty message.
|
||||
fmt.push_spans(space_span(width, style).into(), style, &mut text);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
pub fn show_msg(&self, width: usize, style: Style, hide_reply: bool) -> Text {
|
||||
if let Some(html) = &self.html {
|
||||
html.to_text(width, style, hide_reply)
|
||||
} else {
|
||||
let mut msg = self.event.body();
|
||||
|
||||
if self.downloaded {
|
||||
msg.to_mut().push_str(" \u{2705}");
|
||||
}
|
||||
|
||||
wrapped_text(msg, width, style)
|
||||
}
|
||||
}
|
||||
|
||||
fn sender_span(&self, settings: &ApplicationSettings) -> Span {
|
||||
settings.get_user_span(self.sender.as_ref())
|
||||
}
|
||||
|
||||
fn show_sender(
|
||||
&self,
|
||||
prev: Option<&Message>,
|
||||
align_right: bool,
|
||||
settings: &ApplicationSettings,
|
||||
) -> Option<Span> {
|
||||
let user = if matches!(prev, Some(prev) if self.sender == prev.sender) {
|
||||
return None;
|
||||
} else {
|
||||
self.sender_span(settings)
|
||||
};
|
||||
|
||||
let Span { content, style } = user;
|
||||
let stop = content.len().min(28);
|
||||
let s = &content[..stop];
|
||||
|
||||
let sender = if align_right {
|
||||
format!("{: >width$} ", s, width = 28)
|
||||
} else {
|
||||
format!("{: <width$} ", s, width = 28)
|
||||
};
|
||||
|
||||
Span::styled(sender, style).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OriginalRoomMessageEvent> for Message {
|
||||
fn from(event: OriginalRoomMessageEvent) -> Self {
|
||||
let timestamp = event.origin_server_ts.into();
|
||||
let user_id = event.sender.clone();
|
||||
let content = MessageEvent::Original(event.into());
|
||||
|
||||
Message::new(content, user_id, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RedactedRoomMessageEvent> for Message {
|
||||
fn from(event: RedactedRoomMessageEvent) -> Self {
|
||||
let timestamp = event.origin_server_ts.into();
|
||||
let user_id = event.sender.clone();
|
||||
let content = MessageEvent::Redacted(event.into());
|
||||
|
||||
Message::new(content, user_id, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RoomMessageEvent> for Message {
|
||||
fn from(event: RoomMessageEvent) -> Self {
|
||||
match event {
|
||||
RoomMessageEvent::Original(ev) => ev.into(),
|
||||
RoomMessageEvent::Redacted(ev) => ev.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Message {
|
||||
fn to_string(&self) -> String {
|
||||
self.event.body().into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::tests::*;
|
||||
|
||||
#[test]
|
||||
fn test_mc_cmp() {
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
|
||||
// Everything is equal to itself.
|
||||
assert_eq!(mc1.cmp(&mc1), Ordering::Equal);
|
||||
assert_eq!(mc2.cmp(&mc2), Ordering::Equal);
|
||||
assert_eq!(mc3.cmp(&mc3), Ordering::Equal);
|
||||
assert_eq!(mc4.cmp(&mc4), Ordering::Equal);
|
||||
assert_eq!(mc5.cmp(&mc5), Ordering::Equal);
|
||||
|
||||
// Local echo is always greater than an origin server timestamp.
|
||||
assert_eq!(mc1.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc4), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc5), Ordering::Greater);
|
||||
|
||||
// mc2 is the smallest timestamp.
|
||||
assert_eq!(mc2.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc3), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc4), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc3 should be less than mc4 because of its event ID.
|
||||
assert_eq!(mc3.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc3.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc3.cmp(&mc4), Ordering::Less);
|
||||
assert_eq!(mc3.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc4 should be greater than mc3 because of its event ID.
|
||||
assert_eq!(mc4.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc4.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc4.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc4.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc5 is the greatest OriginServer timestamp.
|
||||
assert_eq!(mc5.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc5.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc5.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc5.cmp(&mc4), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mc_to_key() {
|
||||
let info = mock_room();
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
let mc6 = MessageCursor::latest();
|
||||
|
||||
let k1 = mc1.to_key(&info).unwrap();
|
||||
let k2 = mc2.to_key(&info).unwrap();
|
||||
let k3 = mc3.to_key(&info).unwrap();
|
||||
let k4 = mc4.to_key(&info).unwrap();
|
||||
let k5 = mc5.to_key(&info).unwrap();
|
||||
let k6 = mc6.to_key(&info).unwrap();
|
||||
|
||||
// These should all be equal to their MSGN_KEYs.
|
||||
assert_eq!(k1, &MSG1_KEY.clone());
|
||||
assert_eq!(k2, &MSG2_KEY.clone());
|
||||
assert_eq!(k3, &MSG3_KEY.clone());
|
||||
assert_eq!(k4, &MSG4_KEY.clone());
|
||||
assert_eq!(k5, &MSG5_KEY.clone());
|
||||
|
||||
// MessageCursor::latest() turns into the largest key (our local echo message).
|
||||
assert_eq!(k6, &MSG1_KEY.clone());
|
||||
|
||||
// MessageCursor::latest() fails to convert for a room w/o messages.
|
||||
let info_empty = RoomInfo::default();
|
||||
assert_eq!(mc6.to_key(&info_empty), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mc_to_from_cursor() {
|
||||
let info = mock_room();
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
let mc6 = MessageCursor::latest();
|
||||
|
||||
let identity = |mc: &MessageCursor| {
|
||||
let c = mc.to_cursor(&info).unwrap();
|
||||
|
||||
MessageCursor::from_cursor(&c, &info).unwrap()
|
||||
};
|
||||
|
||||
// These should all convert to a Cursor and back to the original value.
|
||||
assert_eq!(identity(&mc1), mc1);
|
||||
assert_eq!(identity(&mc2), mc2);
|
||||
assert_eq!(identity(&mc3), mc3);
|
||||
assert_eq!(identity(&mc4), mc4);
|
||||
assert_eq!(identity(&mc5), mc5);
|
||||
|
||||
// MessageCursor::latest() should point at the most recent message after conversion.
|
||||
assert_eq!(identity(&mc6), mc1);
|
||||
}
|
||||
}
|
157
src/message/printer.rs
Normal file
157
src/message/printer.rs
Normal file
|
@ -0,0 +1,157 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use modalkit::tui::layout::Alignment;
|
||||
use modalkit::tui::style::Style;
|
||||
use modalkit::tui::text::{Span, Spans, Text};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::util::{space_span, take_width};
|
||||
|
||||
pub struct TextPrinter<'a> {
|
||||
text: Text<'a>,
|
||||
width: usize,
|
||||
base_style: Style,
|
||||
hide_reply: bool,
|
||||
|
||||
alignment: Alignment,
|
||||
curr_spans: Vec<Span<'a>>,
|
||||
curr_width: usize,
|
||||
}
|
||||
|
||||
impl<'a> TextPrinter<'a> {
|
||||
pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
|
||||
TextPrinter {
|
||||
text: Text::default(),
|
||||
width,
|
||||
base_style,
|
||||
hide_reply,
|
||||
|
||||
alignment: Alignment::Left,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn align(mut self, alignment: Alignment) -> Self {
|
||||
self.alignment = alignment;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn hide_reply(&self) -> bool {
|
||||
self.hide_reply
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn sub(&self, indent: usize) -> Self {
|
||||
TextPrinter {
|
||||
text: Text::default(),
|
||||
width: self.width.saturating_sub(indent),
|
||||
base_style: self.base_style,
|
||||
hide_reply: self.hide_reply,
|
||||
|
||||
alignment: self.alignment,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn remaining(&self) -> usize {
|
||||
self.width - self.curr_width
|
||||
}
|
||||
|
||||
pub fn commit(&mut self) {
|
||||
if self.curr_width > 0 {
|
||||
self.push_break();
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self) {
|
||||
self.curr_width = 0;
|
||||
self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
|
||||
}
|
||||
|
||||
pub fn push_break(&mut self) {
|
||||
if self.curr_width == 0 && self.text.lines.is_empty() {
|
||||
// Disallow leading breaks.
|
||||
return;
|
||||
}
|
||||
|
||||
let remaining = self.remaining();
|
||||
|
||||
if remaining > 0 {
|
||||
match self.alignment {
|
||||
Alignment::Left => {
|
||||
let tspan = space_span(remaining, self.base_style);
|
||||
self.curr_spans.push(tspan);
|
||||
},
|
||||
Alignment::Center => {
|
||||
let trailing = remaining / 2;
|
||||
let leading = remaining - trailing;
|
||||
|
||||
let tspan = space_span(trailing, self.base_style);
|
||||
let lspan = space_span(leading, self.base_style);
|
||||
|
||||
self.curr_spans.push(tspan);
|
||||
self.curr_spans.insert(0, lspan);
|
||||
},
|
||||
Alignment::Right => {
|
||||
let lspan = space_span(remaining, self.base_style);
|
||||
self.curr_spans.insert(0, lspan);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
self.push();
|
||||
}
|
||||
|
||||
pub fn push_str<T>(&mut self, s: T, style: Style)
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
let style = self.base_style.patch(style);
|
||||
let mut cow = s.into();
|
||||
|
||||
loop {
|
||||
let sw = UnicodeWidthStr::width(cow.as_ref());
|
||||
|
||||
if self.curr_width + sw <= self.width {
|
||||
// The text fits within the current line.
|
||||
self.curr_spans.push(Span::styled(cow, style));
|
||||
self.curr_width += sw;
|
||||
break;
|
||||
}
|
||||
|
||||
// Take a leading portion of the text that fits in the line.
|
||||
let ((s0, w), s1) = take_width(cow, self.remaining());
|
||||
cow = s1;
|
||||
|
||||
self.curr_spans.push(Span::styled(s0, style));
|
||||
self.curr_width += w;
|
||||
|
||||
self.commit();
|
||||
}
|
||||
|
||||
if self.curr_width == self.width {
|
||||
// If the last bit fills the full line, start a new one.
|
||||
self.push();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_line(&mut self, spans: Spans<'a>) {
|
||||
self.commit();
|
||||
self.text.lines.push(spans);
|
||||
}
|
||||
|
||||
pub fn push_text(&mut self, text: Text<'a>) {
|
||||
self.commit();
|
||||
self.text.lines.extend(text.lines);
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> Text<'a> {
|
||||
self.commit();
|
||||
self.text
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue