4 changed files with 111 additions and 22 deletions
@ -0,0 +1,48 @@ |
|||
# pybsv - Backup, Synchronization, Versioning. |
|||
# Copyright (C) 2025 Simon Boyé |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as published by |
|||
# the Free Software Foundation, either version 3 of the License, or |
|||
# (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|||
"""Tools and utilities to build the command-line interface.""" |
|||
|
|||
from __future__ import annotations |
|||
|
|||
from typing import Final |
|||
|
|||
|
|||
BINARY_PREFIXES: Final[list[str]] = [ |
|||
"", |
|||
"Ki", |
|||
"Mi", |
|||
"Gi", |
|||
"Ti", |
|||
"Pi", |
|||
"Ei", |
|||
"Zi", |
|||
"Yi", |
|||
"Ri", |
|||
"Qi", |
|||
] |
|||
|
|||
|
|||
def format_human_byte_size(byte_size: int) -> str: |
|||
"""Format the given `byte_size` as a human-readable string.""" |
|||
index = min(max((byte_size.bit_length() - 1) // 10, 0), len(BINARY_PREFIXES) - 1) |
|||
size = byte_size / 1024**index |
|||
num_digits = len(str(int(size))) |
|||
decimals = max(0, 3 - num_digits) |
|||
rounded = round(size, decimals) |
|||
if rounded == 1024 and index + 1 < len(BINARY_PREFIXES): |
|||
rounded = 1 |
|||
index += 1 |
|||
return f"{rounded:.16g}{BINARY_PREFIXES[index]}B" |
|||
@ -0,0 +1,56 @@ |
|||
# pybsv - Backup, Synchronization, Versioning. |
|||
# Copyright (C) 2025 Simon Boyé |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as published by |
|||
# the Free Software Foundation, either version 3 of the License, or |
|||
# (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|||
"""Tests for cli_utils.py.""" |
|||
|
|||
from __future__ import annotations |
|||
|
|||
from bsv.cli_utils import format_human_byte_size |
|||
|
|||
|
|||
def test_format_human_byte_size(): |
|||
assert format_human_byte_size(0) == "0B" |
|||
assert format_human_byte_size(1) == "1B" |
|||
assert format_human_byte_size(9) == "9B" |
|||
assert format_human_byte_size(10) == "10B" |
|||
assert format_human_byte_size(99) == "99B" |
|||
assert format_human_byte_size(100) == "100B" |
|||
assert format_human_byte_size(999) == "999B" |
|||
assert format_human_byte_size(1000) == "1000B" |
|||
assert format_human_byte_size(1023) == "1023B" |
|||
assert format_human_byte_size(2**10) == "1KiB" |
|||
assert format_human_byte_size(int(1.23456 * 2**10)) == "1.23KiB" |
|||
assert format_human_byte_size(9 * 2**10) == "9KiB" |
|||
assert format_human_byte_size(10 * 2**10 - 1) == "10KiB" |
|||
assert format_human_byte_size(int(98.76543 * 2**10)) == "98.8KiB" |
|||
assert format_human_byte_size(99 * 2**10 - 1) == "99KiB" |
|||
assert format_human_byte_size(100 * 2**10 - 1) == "100KiB" |
|||
assert format_human_byte_size(int(192.8374 * 2**10)) == "193KiB" |
|||
assert format_human_byte_size(999 * 2**10 - 1) == "999KiB" |
|||
assert format_human_byte_size(1000 * 2**10 - 1) == "1000KiB" |
|||
assert format_human_byte_size(2**20 - 1) == "1MiB" |
|||
assert format_human_byte_size(2**20) == "1MiB" |
|||
assert format_human_byte_size(2**30) == "1GiB" |
|||
assert format_human_byte_size(2**40) == "1TiB" |
|||
assert format_human_byte_size(2**50) == "1PiB" |
|||
assert format_human_byte_size(2**60) == "1EiB" |
|||
assert format_human_byte_size(2**70) == "1ZiB" |
|||
assert format_human_byte_size(2**80) == "1YiB" |
|||
assert format_human_byte_size(2**90) == "1RiB" |
|||
assert format_human_byte_size(2**100 - 2**80) == "1QiB" |
|||
assert format_human_byte_size(2**100) == "1QiB" |
|||
assert format_human_byte_size(2**110 - 2**90) == "1024QiB" |
|||
assert format_human_byte_size(2**110) == "1024QiB" |
|||
assert format_human_byte_size(2**120) == "1048576QiB" |
|||
Loading…
Reference in new issue