From: Simon Tatham Date: Sun, 24 Dec 2023 22:13:47 +0000 (+0000) Subject: Username headers X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=818aef395d6663c3d4307d6e6eec110847929c29;p=mastodonochrome.git Username headers --- diff --git a/src/text.rs b/src/text.rs index e8970e8..43f66f2 100644 --- a/src/text.rs +++ b/src/text.rs @@ -4,15 +4,17 @@ use core::cmp::max; use super::coloured_string::ColouredString; pub trait TextFragment { + // FIXME: we will also need ... + // Some indication of how many usernames are in here and how many statuses + // Some means of passing a flag to render() that says which username or + // status is to be highlighted fn render(&self, width: usize) -> Vec; } pub struct BlankLine {} -impl BlankLine { - pub fn newbox() -> Box { - Box::new(BlankLine{}) - } +pub fn blank() -> Box { + Box::new(BlankLine{}) } impl TextFragment for BlankLine { @@ -29,18 +31,16 @@ pub struct SeparatorLine { boosted: bool, } -impl SeparatorLine { - pub fn newbox( - timestamp: Option>, - favourited: bool, - boosted: bool, +pub fn separator( + timestamp: Option>, + favourited: bool, + boosted: bool, ) -> Box { - Box::new(SeparatorLine { + Box::new(SeparatorLine { timestamp, favourited, boosted, }) - } } impl TextFragment for SeparatorLine { @@ -79,10 +79,8 @@ impl TextFragment for SeparatorLine { pub struct EditorHeaderSeparator {} -impl EditorHeaderSeparator { - pub fn newbox() -> Box { - Box::new(EditorHeaderSeparator{}) - } +pub fn editorsep() -> Box { + Box::new(EditorHeaderSeparator{}) } impl TextFragment for EditorHeaderSeparator { @@ -96,18 +94,54 @@ impl TextFragment for EditorHeaderSeparator { } } +pub struct UsernameHeader { + header: String, + colour: char, + account: String, + nameline: String, +} + +pub fn fromline(account: &str, nameline: &str) -> Box { + Box::new(UsernameHeader{ + header: "From".to_string(), + colour: 'F', + account: account.to_string(), + nameline: nameline.to_string(), + }) +} + +pub fn boosterline(account: &str, nameline: &str) -> Box { + Box::new(UsernameHeader{ + header: "Via".to_string(), + colour: 'f', + account: account.to_string(), + nameline: nameline.to_string(), + }) +} + +impl TextFragment for UsernameHeader { + fn render(&self, _width: usize) -> Vec { + let header = ColouredString::plain(&format!("{}: ", self.header)); + let body = ColouredString::uniform( + &format!("{} ({})", self.nameline, self.account), self.colour); + vec! { + header + body, + } + } +} + #[test] -fn blank() { - assert_eq!(BlankLine::newbox().render(40), vec! { +fn test_blank() { + assert_eq!(blank().render(40), vec! { ColouredString::plain("") }); } #[test] -fn separator() { +fn test_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) + assert_eq!(separator(None, true, false) .render(60), vec! { ColouredString::general( "------------------------------------------------------[F]--", @@ -117,11 +151,28 @@ fn separator() { } #[test] -fn editorsep() { - assert_eq!(EditorHeaderSeparator::newbox().render(5), vec! { +fn test_editorsep() { + assert_eq!(editorsep().render(5), vec! { ColouredString::general( "---|", "----", ) }); } + +#[test] +fn test_userheader() { + assert_eq!(fromline("stoat@example.com", "Some Person").render(80), vec! { + ColouredString::general( + "From: Some Person (stoat@example.com)", + " FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + ) + }); + assert_eq!(boosterline("stoat@example.com", "Some Person") + .render(80), vec! { + ColouredString::general( + "Via: Some Person (stoat@example.com)", + " fffffffffffffffffffffffffffffff", + ) + }); +}