chiark / gitweb /
And prime the ExtendableIndicator.
authorSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 14:09:50 +0000 (14:09 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 14:09:50 +0000 (14:09 +0000)
Perhaps we're ready to start on another module now!

src/text.rs

index dc5b5c8b9626a296894125fe2ca845296abbe497..d3bed53e3f9ed73a190609b6fb9439a2c383abd7 100644 (file)
@@ -16,7 +16,10 @@ pub trait TextFragment {
     // Some means of having ExtendableIndicator flag itself as active
     fn render(&self, width: usize) -> Vec<ColouredString>;
 
-    fn as_extendable_indicator(&self) -> Option<&ExtendableIndicator> { None }
+    fn as_extendable_indicator_mut(&mut self)
+                                   -> Option<&mut ExtendableIndicator> {
+        None
+    }
     fn as_menu_keypress_mut(&mut self) -> Option<&mut MenuKeypressLine> {
         None
     }
@@ -769,20 +772,32 @@ fn test_html() {
         });
 }
 
-pub struct ExtendableIndicator {}
+pub struct ExtendableIndicator {
+    primed: bool,
+}
 
 impl ExtendableIndicator {
     pub fn new() -> Box<dyn TextFragment> {
-        Box::new(ExtendableIndicator{})
+        Box::new(ExtendableIndicator{
+                primed: false
+            })
     }
+
+    pub fn set_primed(&mut self, primed: bool) { self.primed = primed; }
 }
 
 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 message = if self.primed {
+            ColouredString::general(
+                "Press [0] to extend",
+                "HHHHHHHKHHHHHHHHHHH")
+        } else {
+            ColouredString::general(
+                "Press [0] twice to extend",
+                "HHHHHHHKHHHHHHHHHHHHHHHHH")
+        };
         let msgtrunc = message.truncate(width);
         let space = width - min(msgtrunc.width() + 1, width);
         let left = space / 2;
@@ -794,18 +809,31 @@ impl TextFragment for ExtendableIndicator {
         }
     }
 
-    fn as_extendable_indicator_mut(&self) -> Option<&ExtendableIndicator> {
+    fn as_extendable_indicator_mut(&mut self)
+                                   -> Option<&mut ExtendableIndicator> {
         Some(self)
     }
 }
 
 #[test]
 fn test_extendable() {
-    assert_eq!(ExtendableIndicator::new().render(40), vec! {
+    let mut ei = ExtendableIndicator::new();
+
+    assert_eq!(ei.render(40), vec! {
+            ColouredString::plain(""),
+            ColouredString::general(
+                "       Press [0] twice to extend",
+                "       HHHHHHHKHHHHHHHHHHHHHHHHH"),
+            ColouredString::plain(""),
+        });
+
+    ei.as_extendable_indicator_mut().unwrap().set_primed(true);
+
+    assert_eq!(ei.render(40), vec! {
             ColouredString::plain(""),
             ColouredString::general(
-                "  Press [0] at top of file to extend",
-                "  HHHHHHHKHHHHHHHHHHHHHHHHHHHHHHHHHH"),
+                "          Press [0] to extend",
+                "          HHHHHHHKHHHHHHHHHHH"),
             ColouredString::plain(""),
         });
 }