Skip to content

Commit

Permalink
Refactor long stat values to use units
Browse files Browse the repository at this point in the history
  • Loading branch information
R055A committed Oct 17, 2023
1 parent c662273 commit 0b40900
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/generate_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ def generate_output_folder() -> None:
if not isdir(OUTPUT_DIR):
mkdir(OUTPUT_DIR)


def add_metric_unit(num):
"""
Add metric units to large numbers to reduce length of string
Example: 12,456 to 12.46K
"""
metric_units = ['K', 'M', 'B', 'T']
metric_units_index = -1

if num > 9999:
while num > 999:
num /= 1000
metric_units_index += 1
return str(num)[:TXT_SPACER_MAX_LEN - 2] + metric_units[metric_units_index]
return str(num)


###############################################################################
# GenerateImages class
###############################################################################
Expand Down Expand Up @@ -104,7 +121,9 @@ async def generate_overview(self) -> None:
output)

forks = f"{await self.__stats.forks:,}"
forks = forks if len(str(forks)) < TXT_SPACER_MAX_LEN else add_metric_unit(int(forks))
stars = f"{await self.__stats.stargazers:,}"
stars = stars if len(str(stars)) < TXT_SPACER_MAX_LEN else add_metric_unit(int(stars))
forks_and_stars = \
forks + ' ' * max(1, TXT_SPACER_MAX_LEN - len(str(forks)) + 1) + '| ' + stars
output = sub("{{ forks_and_stars }}",
Expand Down Expand Up @@ -143,7 +162,10 @@ async def generate_overview(self) -> None:
output)

pull_requests = f"{await self.__stats.pull_requests:,}"
pull_requests = pull_requests if len(str(pull_requests)) < TXT_SPACER_MAX_LEN \
else add_metric_unit(int(pull_requests))
issues = f"{await self.__stats.issues:,}"
issues = stars if len(str(issues)) < TXT_SPACER_MAX_LEN else add_metric_unit(int(issues))
pull_requests_and_issues = \
pull_requests + ' ' * max(1, TXT_SPACER_MAX_LEN - len(str(pull_requests)) + 1) + '| ' + issues
output = sub("{{ pull_requests_and_issues }}",
Expand Down

0 comments on commit 0b40900

Please sign in to comment.