#!/usr/bin/env python3

# Usage: ./zsh-history-stats <path_to_history_file>

if __name__ == "__main__":
    import sys
    import os
    import pprint
    from collections import defaultdict
    from operator import itemgetter

    commands = defaultdict(int)

    zsh_history = os.path.realpath(os.path.expanduser("~/.zsh_history"))
    if len(sys.argv) == 2:
        zsh_history = sys.argv[1]

    with open(zsh_history, "r") as f:
        line = f.readline()
        while line:
            try:
                line = f.readline()
                commands[line.split(";")[1].split()[0]] += 1
            except UnicodeDecodeError:
                continue
            except IndexError:
                continue

    for key, value in commands.items():
        print(f"{key}, {value}")
