slaaphistogram/hist.py

69 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import re
import time
unknown = 0
skipped = 0
hours = [0] * 24
for line in sys.stdin:
m = re.match(r'([0-9]*):.*,([0-9])', line.strip())
assert m
if m.group(2) == "0":
skipped += 1
elif m.group(2) == "2":
unknown += 1
else:
assert m.group(2) == "1"
hours[int(m.group(1))] += 1
BLOCKS = "▁▂▃▄▅▆▇█"
HEIGHT = 20
SPEED = 400
max_val = max([*hours, unknown, skipped])
hours_norm = [h / max_val for h in hours]
unknown_norm = unknown / max_val
skipped_norm = skipped / max_val
def print_block(value, row):
height_value = value * HEIGHT - (HEIGHT - row)
if height_value >= 0.99:
print(BLOCKS[-1], end=" ")
elif height_value < 0.01:
if HEIGHT - 1 == row and value > 0:
print("", end=" ")
else:
print(" ", end=" ")
else:
print(BLOCKS[int(height_value * len(BLOCKS))], end=" ")
sys.stdout.flush()
time.sleep(2 / SPEED)
cutoff = 12
hour_indices = [*range(cutoff, 24), *range(0, cutoff)]
for row in range(HEIGHT):
print("", end=" ")
for h in hour_indices:
print_block(hours_norm[h], row)
print("", end=" ")
print_block(skipped_norm, row)
print("", end=" ")
print_block(unknown_norm, row)
print()
for c in (
"".join(f"{h: >2} " for h in hour_indices[::2]) +
" skp unk"
):
print(c, flush=True, end="")
time.sleep(1 / SPEED)
print()