chiark / gitweb /
Username headers
authorSimon Tatham <anakin@pobox.com>
Sun, 24 Dec 2023 22:13:47 +0000 (22:13 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 24 Dec 2023 22:13:47 +0000 (22:13 +0000)
src/text.rs

index e8970e8ac49de90fc8296ef9958c975391b5c595..43f66f208a0487001f309ff26e0dc88b408d50ca 100644 (file)
@@ -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<ColouredString>;
 }
 
 pub struct BlankLine {}
 
-impl BlankLine {
-    pub fn newbox() -> Box<dyn TextFragment> {
-        Box::new(BlankLine{})
-    }
+pub fn blank() -> Box<dyn TextFragment> {
+    Box::new(BlankLine{})
 }
 
 impl TextFragment for BlankLine {
@@ -29,18 +31,16 @@ pub struct SeparatorLine {
     boosted: bool,
 }
 
-impl SeparatorLine {
-    pub fn newbox(
-        timestamp: Option<DateTime<Utc>>,
-        favourited: bool,
-        boosted: bool,
+pub fn separator(
+    timestamp: Option<DateTime<Utc>>,
+    favourited: bool,
+    boosted: bool,
     ) -> Box<dyn TextFragment> {
-        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<dyn TextFragment> {
-        Box::new(EditorHeaderSeparator{})
-    }
+pub fn editorsep() -> Box<dyn TextFragment> {
+    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<dyn TextFragment> {
+    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<dyn TextFragment> {
+    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<ColouredString> {
+        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",
+                )
+        });
+}