From a7356c92152ab918f0dd7ec3da122ecabe8d8c5a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 26 Dec 2023 09:20:45 +0000 Subject: [PATCH] Media entries. --- src/text.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/src/text.rs b/src/text.rs index 9e2dd93..4b87b63 100644 --- a/src/text.rs +++ b/src/text.rs @@ -515,6 +515,19 @@ fn test_fileheader() { }); } +fn trim_para_list(paras: &mut Vec) { + let first_nonempty = paras.iter() + .position(|p| !p.is_empty()).unwrap_or(paras.len()); + paras.splice(..first_nonempty, vec!{}); + + while match paras.last() { + Some(p) => p.is_empty(), + None => false + } { + paras.pop(); + } +} + struct HTMLFormatter { paras: Vec, colourstack: Vec, @@ -535,16 +548,7 @@ impl HTMLFormatter { } fn finish(mut self) -> Vec { - let first_nonempty = self.paras.iter() - .position(|p| !p.is_empty()).unwrap_or(self.paras.len()); - self.paras.splice(..first_nonempty, vec!{}); - - while match self.paras.last() { - Some(p) => p.is_empty(), - None => false - } { - self.paras.pop(); - } + trim_para_list(&mut self.paras); if !self.bad_tags.is_empty() { let mut para = Paragraph::new().add( @@ -978,7 +982,53 @@ fn test_user_list_entry() { }); } +pub struct Media { + url: String, + description: Vec, +} + +pub fn media(url: &str, description: &str) + -> Box { + let mut paras = description + .split('\n') + .map(|x| Paragraph::new() + .set_indent(2, 2) + .add(&ColouredString::uniform(x, 'm'))) + .collect(); + trim_para_list(&mut paras); + Box::new(Media { + url: url.to_string(), + description: paras, + }) +} + +impl TextFragment for Media { + fn render(&self, width: usize) -> Vec { + let mut lines = vec! { ColouredString::uniform(&self.url, 'M') }; + for para in &self.description { + lines.extend_from_slice(¶.render(width)); + } + lines + } +} + +#[test] +fn test_media() { + assert_eq!(media("https://example.com/example.png", + "A picture of an example, sitting on an example, with examples dotted around the example landscape.\n\nThis doesn't really make sense, but whatever.").render(50), vec! { + ColouredString::uniform("https://example.com/example.png", 'M'), + ColouredString::general(" A picture of an example, sitting on an example,", + " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"), + ColouredString::general(" with examples dotted around the example", + " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"), + ColouredString::general(" landscape.", + " mmmmmmmmmm"), + ColouredString::plain(" "), + ColouredString::general(" This doesn't really make sense, but whatever.", + " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"), + }); +} + // TODO: -// Media // FileStatusLine, with priorities // MenuKeypressLine -- 2.30.2