chiark / gitweb /
Media entries.
authorSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 09:20:45 +0000 (09:20 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 09:27:05 +0000 (09:27 +0000)
src/text.rs

index 9e2dd93c4856cd487fe4e646e2d361bfa8daf1b3..4b87b63c739bfe60d85e7e1b137a8d1fdef262bc 100644 (file)
@@ -515,6 +515,19 @@ fn test_fileheader() {
         });
 }
 
+fn trim_para_list(paras: &mut Vec<Paragraph>) {
+    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<Paragraph>,
     colourstack: Vec<char>,
@@ -535,16 +548,7 @@ impl HTMLFormatter {
     }
 
     fn finish(mut self) -> Vec<Paragraph> {
-        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<Paragraph>,
+}
+
+pub fn media(url: &str, description: &str)
+    -> Box<dyn TextFragment> {
+    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<ColouredString> {
+        let mut lines = vec! { ColouredString::uniform(&self.url, 'M') };
+        for para in &self.description {
+            lines.extend_from_slice(&para.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