-
-
Save zeevro/b0f0979034db396b1f357ebf4fde6ffa to your computer and use it in GitHub Desktop.
A small terminal barchart utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
W = 30 | |
B = ['', *'▏▎▍▌▋▊▉█'] | |
C = B[-1] | |
def main() -> None: | |
p = argparse.ArgumentParser() | |
p.add_argument('-s', '--sort', action='store_true') | |
p.add_argument('-r', '--reverse', action='store_true') | |
p.add_argument('file', metavar='filename', nargs='?', type=argparse.FileType(), default='-') | |
args = p.parse_args() | |
data: list[tuple[int, str]] = [] | |
d_len = 0 | |
n_max = 0 | |
n_len = 0 | |
for line in args.file: | |
try: | |
ns, rest = line.split(None, 1) | |
data.append((n := int(ns, 0), rest.rstrip('\n'))) | |
except ValueError: | |
continue | |
d_len = max(d_len, len(rest)) | |
n_max = max(n_max, n) | |
n_len = max(n_len, len(ns)) | |
k = n_max / W | |
if args.sort: | |
data = sorted(data) | |
if args.reverse: | |
data = data[::-1] | |
for n, line in data: | |
c = n / k | |
bar = f'{C * int(c)}{B[round((c % 1) * 8)]}' | |
print(f'{line:{d_len}} {n:{n_len}} {bar}') # noqa: T201 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment