]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Explicitly specify a font for tally marks
[tablify] / src / lib.rs
index 11ee0c38912872503178c90e3efab5db0cf98e3a..bfcfdec72aa9d9186cb017c6cba14b6a75bfcf62 100644 (file)
@@ -4,15 +4,18 @@ use std::fmt::Write;
 use std::io::BufRead;
 use std::iter::Iterator;
 
-fn tally_marks(n: usize, mark: Option<&str>) -> String {
-    match mark {
+const TALLY_FONT: &str = "BabelStone Han";
+
+fn tally_marks(n: usize, mark: Option<&str>) -> HTML {
+    HTML(match mark {
         None => {
             let fives = { 0..n / 5 }.map(|_| '𝍸');
             let ones = { 0..n % 5 }.map(|_| '𝍷');
-            fives.chain(ones).collect()
+            let marks: String = fives.chain(ones).collect();
+            format!("<span style=\"font-family: '{TALLY_FONT}'\">{marks}</span>")
         }
         Some(m) => { 0..n }.map(|_| m).collect(),
-    }
+    })
 }
 
 #[derive(PartialEq, Eq, Debug)]
@@ -308,7 +311,7 @@ fn render_instances(instances: &[Option<String>], mark: Option<&str>) -> HTML {
             None => tally += 1,
             Some(ref content) => {
                 if tally > 0 {
-                    out.push(HTML(tally_marks(tally, mark)));
+                    out.push(tally_marks(tally, mark));
                     tally = 0;
                 }
                 out.push(HTML::escape(content));
@@ -316,7 +319,7 @@ fn render_instances(instances: &[Option<String>], mark: Option<&str>) -> HTML {
         }
     }
     if tally > 0 {
-        out.push(HTML(tally_marks(tally, mark)));
+        out.push(tally_marks(tally, mark));
     }
     HTML(
         out.into_iter()
@@ -504,22 +507,77 @@ mod tests {
 
     #[test]
     fn test_tally_marks() {
-        assert_eq!(tally_marks(1, None), "𝍷");
-        assert_eq!(tally_marks(2, None), "𝍷𝍷");
-        assert_eq!(tally_marks(3, None), "𝍷𝍷𝍷");
-        assert_eq!(tally_marks(4, None), "𝍷𝍷𝍷𝍷");
-        assert_eq!(tally_marks(5, None), "𝍸");
-        assert_eq!(tally_marks(6, None), "𝍸𝍷");
-        assert_eq!(tally_marks(7, None), "𝍸𝍷𝍷");
-        assert_eq!(tally_marks(8, None), "𝍸𝍷𝍷𝍷");
-        assert_eq!(tally_marks(9, None), "𝍸𝍷𝍷𝍷𝍷");
-        assert_eq!(tally_marks(10, None), "𝍸𝍸");
-        assert_eq!(tally_marks(11, None), "𝍸𝍸𝍷");
-        assert_eq!(tally_marks(1, Some("x")), "x");
-        assert_eq!(tally_marks(4, Some("x")), "xxxx");
-        assert_eq!(tally_marks(5, Some("x")), "xxxxx");
-        assert_eq!(tally_marks(6, Some("x")), "xxxxxx");
-        assert_eq!(tally_marks(2, Some("πŸ”")), "πŸ”πŸ”");
+        assert_eq!(
+            tally_marks(1, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(2, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(3, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍷𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(4, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍷𝍷𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(5, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(6, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(7, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(8, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍷𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(9, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍷𝍷𝍷𝍷</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(10, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍸</span>"
+            ))
+        );
+        assert_eq!(
+            tally_marks(11, None),
+            HTML(format!(
+                "<span style=\"font-family: '{TALLY_FONT}'\">𝍸𝍸𝍷</span>"
+            ))
+        );
+        assert_eq!(tally_marks(1, Some("x")), "x".into());
+        assert_eq!(tally_marks(4, Some("x")), "xxxx".into());
+        assert_eq!(tally_marks(5, Some("x")), "xxxxx".into());
+        assert_eq!(tally_marks(6, Some("x")), "xxxxxx".into());
+        assert_eq!(tally_marks(2, Some("πŸ”")), "πŸ”πŸ”".into());
     }
 
     fn read_rows(input: impl std::io::Read) -> Result<Vec<Rowlike>, std::io::Error> {
@@ -760,7 +818,7 @@ mod tests {
                 }
             ),
             HTML::from(
-                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">𝍷</td>"#
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"><span style="font-family: 'BabelStone Han'">𝍷</span></td>"#
             )
         );
         assert_eq!(
@@ -773,7 +831,7 @@ mod tests {
                 }
             ),
             HTML::from(
-                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">𝍷𝍷</td>"#
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"><span style="font-family: 'BabelStone Han'">𝍷𝍷</span></td>"#
             )
         );
         assert_eq!(
@@ -802,7 +860,7 @@ mod tests {
                 }
             ),
             HTML::from(
-                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 π·</td>"#
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 <span style="font-family: 'BabelStone Han'">𝍷</span></td>"#
             )
         );
         assert_eq!(
@@ -841,7 +899,7 @@ mod tests {
                 }
             ),
             HTML::from(
-                r#"<td class="yes" onmouseover="h2('bob&#39;s','foo')" onmouseout="ch2('bob&#39;s','foo')">𝍷</td>"#
+                r#"<td class="yes" onmouseover="h2('bob&#39;s','foo')" onmouseout="ch2('bob&#39;s','foo')"><span style="font-family: 'BabelStone Han'">𝍷</span></td>"#
             )
         );
         assert_eq!(
@@ -916,7 +974,7 @@ mod tests {
                     ]),
                 }
             ),
-            HTML::from("bar: π·π·, foo")
+            HTML::from(r#"bar: <span style="font-family: 'BabelStone Han'">𝍷𝍷</span>, foo"#)
         );
         assert_eq!(
             render_all_leftovers(
@@ -1071,7 +1129,7 @@ mod tests {
                 })
             ),
             HTML::from(
-                r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')">𝍷</td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
+                r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')"><span style="font-family: 'BabelStone Han'">𝍷</span></td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
 "#
             )
         );