From: Simon Tatham Date: Sun, 24 Dec 2023 21:26:32 +0000 (+0000) Subject: SeparatorLine (with proper date formatting!) X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=4b3e2030dbbee2aef423b4fc4993721c49f27d2e;p=mastodonochrome.git SeparatorLine (with proper date formatting!) --- diff --git a/src/text.rs b/src/text.rs index a5c62bd..386b917 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,3 +1,5 @@ +use chrono::{DateTime,Utc,Local}; + use super::coloured_string::ColouredString; pub trait TextFragment { @@ -19,3 +21,77 @@ impl TextFragment for BlankLine { } } } + +pub struct SeparatorLine { + timestamp: Option>, + favourited: bool, + boosted: bool, +} + +impl SeparatorLine { + pub fn newbox( + timestamp: Option>, + favourited: bool, + boosted: bool, + ) -> Box { + Box::new(SeparatorLine { + timestamp, + favourited, + boosted, + }) + } +} + +impl TextFragment for SeparatorLine { + fn render(&self, width: usize) -> Vec { + let mut suffix = ColouredString::plain(""); + let display_pre = ColouredString::uniform("[", 'S'); + let display_post = ColouredString::uniform("]--", 'S'); + match self.timestamp { + Some(date) => { + let datestr = date.with_timezone(&Local) + .format("%a %b %e %H:%M:%S %Y").to_string(); + suffix = &display_pre + + ColouredString::uniform(&datestr, 'D') + + &display_post + suffix; + }, + _ => (), + } + if self.boosted { + suffix = &display_pre + ColouredString::uniform("B", 'D') + + &display_post + suffix; + } + if self.favourited { + suffix = &display_pre + ColouredString::uniform("F", 'D') + + &display_post + suffix; + } + let w = suffix.width(); + if w < width - 1 { + suffix = ColouredString::uniform("-", 'S').repeat(width - 1 - w) + + suffix; + } + vec! { + suffix.split(width).next().unwrap().to_owned() + } + } +} + +#[test] +fn blank() { + assert_eq!(BlankLine::newbox().render(40), vec! { + ColouredString::plain("") + }); +} + +#[test] +fn separator() { + // Would be nice to test time formatting here, but I'd have to + // think of a way to test it independently of TZ + assert_eq!(SeparatorLine::newbox(None, true, false) + .render(60), vec! { + ColouredString::general( + "------------------------------------------------------[F]--", + "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSDSSS", + ) + }); +}