diff options
author | Frederick Yin <fkfd@macaw.me> | 2020-06-14 21:48:20 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@macaw.me> | 2020-06-14 21:48:20 +0800 |
commit | 4110a4df25170289dce8690a572d26c58063adb9 (patch) | |
tree | 176f04ccb064b7c797e4f1072dfb4d7fd4d0f505 /git-gmi | |
parent | d34ba645c25f13ed53c64025d7a08ebda192af8a (diff) |
Humanized file sizes
Includes hurry.filesize dep and its license ZPL-2.0
Diffstat (limited to 'git-gmi')
-rw-r--r-- | git-gmi/git.py | 11 |
1 files changed, 8 insertions, 3 deletions
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" ) |