From 4110a4df25170289dce8690a572d26c58063adb9 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sun, 14 Jun 2020 21:48:20 +0800 Subject: Humanized file sizes Includes hurry.filesize dep and its license ZPL-2.0 --- git-gmi/git.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'git-gmi') diff --git a/git-gmi/git.py b/git-gmi/git.py index 990ca0c..c8fa5c6 100644 --- a/git-gmi/git.py +++ b/git-gmi/git.py @@ -1,4 +1,5 @@ from pygit2 import * +from hurry.filesize import size, alternative import mimetypes from const import * @@ -6,6 +7,10 @@ mimetypes.add_type("text/gemini", ".gmi") mimetypes.add_type("text/gemini", ".gemini") +def convert_filesize(bytes: int) -> str: + return size(bytes, system=alternative) + + class GitGmiRepo: def __init__(self, name: str, path: str): self.name = name @@ -38,7 +43,7 @@ class GitGmiRepo: ): found_readme = True response += ( - f"## {item['name']} | {item['size']} bytes\n" + f"## {item['name']} | {convert_filesize(item['size'])}\n" f"{item['blob'].data.decode('utf-8')}" ) if not found_readme: @@ -147,7 +152,7 @@ class GitGmiRepo: f"=> {item['name']}/ {item['name']}/ | {item['size']} items\n" ) elif item["type"] == "file": - response += f"=> {item['name']} {item['name']} | {item['size']} bytes\n" + response += f"=> {item['name']} {item['name']} | {convert_filesize(item['size'])}\n" return response def get_blob(self, commit_str: str, location=[]) -> Blob: @@ -169,7 +174,7 @@ class GitGmiRepo: response = ( f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header() - + f"## {self.name}/{'/'.join(location)} | {blob.size} bytes\n\n" + + f"## {self.name}/{'/'.join(location)} | {convert_filesize(blob.size)}\n\n" f"=> {blob.name}?raw view raw\n\n" f"```\n" ) -- cgit v1.2.3