yagit

Yet another static site generator for Git 🙀️

Commit
de57a3b97bf4e0386cee6ca1374a7fab06ce6445
Parent
594d76a374b64e64a37692388e4a280e42ae6c8f
Author
Pablo <pablo-pie@riseup.net>
Date

Improved the rendering of file sizes

Diffstats

1 files changed, 22 insertions, 2 deletions

Status Name Changes Insertions Deletions
Modified src/main.rs 2 files changed 22 2
diff --git a/src/main.rs b/src/main.rs
@@ -646,8 +646,7 @@ impl<'repo> RepoRenderer<'repo> {
                      root = self.output_root,
                      name = Escaped(&self.name),
                      path = Escaped(&path.to_string_lossy()))?;
-    // TODO: [feature]: print the size differently for larger blobs?
-    writeln!(&mut f, "<td align=\"right\">{}B</td>", blob.size())?;
+    writeln!(&mut f, "<td align=\"right\">{}</td>", FileSize(blob.size()))?;
     writeln!(&mut f, "<td align=\"right\">{}</td>", mode)?;
     writeln!(&mut f, "</tr>")?;
     writeln!(&mut f, "</tbody>")?;
@@ -1205,6 +1204,27 @@ impl<'repo> RepoRenderer<'repo> {
   }
 }
 
+#[derive(Clone, Copy)]
+struct FileSize(usize);
+
+impl Display for FileSize {
+  // TODO: [feature]: print LOC instead of file size for text files?
+  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    const KIBI: usize = 1024;
+    const MEBI: usize = KIBI * 1024;
+
+    let size = self.0;
+
+    if size < KIBI {
+      write!(f, "{}B", size)
+    } else if size < MEBI {
+      write!(f, "{}KB", size/MEBI)
+    } else {
+      write!(f, "{}MB", size/KIBI)
+    }
+  }
+}
+
 #[derive(Clone, Copy, Debug)]
 /// POSIX filemode
 struct Mode(pub i32);