chiark / gitweb /
FileHeader text implementation
authorSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 15:55:47 +0000 (15:55 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 15:55:47 +0000 (15:55 +0000)
src/text.rs

index 54e3aaae3d5af394e6ac673da1a0b6b17bad021e..20e7399fb14c240ecbb675b59e020dfaae4ee304 100644 (file)
@@ -399,5 +399,78 @@ fn test_para_wrap() {
             ColouredString::plain("  jumps over"),
             ColouredString::plain("  the lazy dog"),
         });
+}
+
+pub struct FileHeader {
+    text: ColouredString,
+}
+
+pub fn fileheader(text: ColouredString) -> Box<dyn TextFragment> {
+    Box::new(FileHeader{
+            text: text,
+        })
+}
+
+impl TextFragment for FileHeader {
+    fn render(&self, width: usize) -> Vec<ColouredString> {
+        let elephants = width >= 16;
+        let twidth = if elephants { width - 9 } else { width - 1 };
+        let title = self.text.truncate(twidth);
+        let tspace = twidth - title.width();
+        let tleft = tspace / 2;
+        let tright = tspace - tleft;
+        let titlepad = ColouredString::plain(" ").repeat(tleft) +
+            title + ColouredString::plain(" ").repeat(tright);
+        let underline = ColouredString::uniform("~", '~').repeat(twidth);
+        if elephants {
+            vec! {
+                (ColouredString::general("(o) ", "JJJ ") + titlepad +
+                 ColouredString::general(" (o)", " JJJ")),
+                (ColouredString::general("/J\\ ", "JJJ ") + underline +
+                 ColouredString::general(" /J\\", " JJJ")),
+            }
+        } else {
+            vec! { titlepad, underline }
+        }
+    }
+}
+
+#[test]
+fn test_fileheader() {
+    assert_eq!(fileheader(ColouredString::uniform("hello, world", 'H'))
+               .render(40),
+               vec! {
+            ColouredString::general("(o)          hello, world           (o)",
+                                    "JJJ          HHHHHHHHHHHH           JJJ"),
+            ColouredString::general("/J\\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /J\\",
+                                    "JJJ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JJJ"),
+        });
 
+    assert_eq!(fileheader(ColouredString::uniform("hello, world", 'H'))
+               .render(21),
+               vec! {
+            ColouredString::general("(o) hello, world (o)",
+                                    "JJJ HHHHHHHHHHHH JJJ"),
+            ColouredString::general("/J\\ ~~~~~~~~~~~~ /J\\",
+                                    "JJJ ~~~~~~~~~~~~ JJJ"),
+        });
+
+    assert_eq!(fileheader(ColouredString::uniform("hello, world", 'H'))
+               .render(20),
+               vec! {
+            ColouredString::general("(o) hello, worl (o)",
+                                    "JJJ HHHHHHHHHHH JJJ"),
+            ColouredString::general("/J\\ ~~~~~~~~~~~ /J\\",
+                                    "JJJ ~~~~~~~~~~~ JJJ"),
+        });
+
+    assert_eq!(fileheader(ColouredString::uniform("hello, world", 'H'))
+               .render(10),
+               vec! {
+            ColouredString::general("hello, wo",
+                                    "HHHHHHHHH"),
+            ColouredString::general("~~~~~~~~~",
+                                    "~~~~~~~~~"),
+        });
 }
+