});
}
+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>,
}
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(
});
}
+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(¶.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