chiark / gitweb /
ExtendableIndicator, without the hard part
authorSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 20:43:55 +0000 (20:43 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 20:43:55 +0000 (20:43 +0000)
src/text.rs

index b61efa9ac2c2169c2e5662bafe9331652aba7e12..9b59666aa29b5c020d52647d9d6fffbfaea92f5d 100644 (file)
@@ -1,5 +1,5 @@
-use chrono::{DateTime,Utc,Local};
-use core::cmp::max;
+use chrono::{DateTime, Utc, Local};
+use core::cmp::{max, min};
 use std::collections::{HashMap, BTreeSet};
 
 use super::html;
@@ -11,6 +11,8 @@ pub trait TextFragment {
     // 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>;
+
+    fn as_extendable_indicator(&self) -> Option<&ExtendableIndicator> { None }
 }
 
 pub struct BlankLine {}
@@ -733,8 +735,47 @@ fn test_html() {
         });
 }
 
+pub struct ExtendableIndicator {}
+
+pub fn extendable_indicator() -> Box<dyn TextFragment> {
+    Box::new(ExtendableIndicator{})
+}
+
+impl TextFragment for ExtendableIndicator {
+    fn render(&self, width: usize) -> Vec<ColouredString> {
+        // FIXME: decide how to make this message change when it's primed
+        let message = ColouredString::general(
+            "Press [0] at top of file to extend",
+            "HHHHHHHKHHHHHHHHHHHHHHHHHHHHHHHHHH");
+        let msgtrunc = message.truncate(width);
+        let space = width - min(msgtrunc.width() + 1, width);
+        let left = space / 2;
+        let msgpad = ColouredString::plain(" ").repeat(left) + msgtrunc;
+        vec! {
+            ColouredString::plain(""),
+            msgpad,
+            ColouredString::plain(""),
+        }
+    }
+
+    // FIXME: get rid of this silly trait method if I turn out not to need it
+    fn as_extendable_indicator(&self) -> Option<&ExtendableIndicator> {
+        Some(self)
+    }
+}
+
+#[test]
+fn test_extendable() {
+    assert_eq!(extendable_indicator().render(40), vec! {
+            ColouredString::plain(""),
+            ColouredString::general(
+                "  Press [0] at top of file to extend",
+                "  HHHHHHHKHHHHHHHHHHHHHHHHHHHHHHHHHH"),
+            ColouredString::plain(""),
+        });
+}
+
 // TODO:
-// ExtendableIndicator at file header (with an 'active' flag)
 // InReplyToLine, with first line of included paragraph
 // NotificationLog, also with included toot
 // UserListEntry