<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
from os import getenv
from os.path import isfile


def main():
    history = get_history()
    if not history:
        print("Shell history not found.")
    commands = {}
    for line in history:
        cmd = line.split()
        # if not cmd: continue
        if cmd[0] in commands:
            commands[cmd[0]] += 1
        else:
            commands[cmd[0]] = 1

    total = len(history)
    frequency = sorted(commands.keys(), key=lambda x: commands[x], reverse=True)
    print("{:>3} {:<20} {:<10} {:<3}".format('', "Command", "Count", "Percentage"))
    # min for when history is very small 
    for i in min(range(10), range(len(frequency)), key=len):
        cmd = frequency[i]
        count = commands[cmd]
        print("{i:>3} {cmd:<20} {count:<10} {percent:<3.3}%"
              .format(i=i+1, cmd=cmd, count=count, percent=count / total * 100))
    return frequency


def get_history(history_file=None):
    """Get usage history for the shell in use."""
    shell = getenv("SHELL").split('/')[-1]
    home = getenv("HOME") + '/'
    hist_files = {"bash": [".bash_history"], "fish": [".config/fish/fish_history"],
                  "zsh": [".zhistory", ".zsh_history"]}
    if shell in hist_files:
        for hist_file in hist_files[shell]:
            if isfile(home + hist_file):
                history_file = home + hist_file
    with open(history_file, 'r') as h:
        history = [l.strip() for l in h.readlines() if l.strip()]
    if shell == "fish":
        history = [l[7:] for l in  history if l.startswith("- cmd:")]
    return history


if __name__ == "__main__":
    main()<commit_msg>add pie chart, split code in functions<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
from os import getenv
from os.path import isfile
from sys import argv, exit


def main(n=10):
    history = get_history()
    if not history:
        print("Shell history not found.")
        exit()
    commands = {}
    for line in history:
        cmd = line.split()
        # if not cmd: continue
        if cmd[0] in commands:
            commands[cmd[0]] += 1
        else:
            commands[cmd[0]] = 1

    total = len(history)
    counts = sorted(commands.items(), key=lambda x: x[1], reverse=True)
    print_top(n, counts, total)
    pie_top(n, counts)
    return counts


def pie_top(n, counts):
    """Show a pie chart of n most used commands."""
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        print("Please install matplotlib to use this option.")
        exit()
    label, x = zip(*counts[:n])
    fig = plt.figure()
    fig.canvas.set_window_title("ShellStats")
    plt.axes(aspect=1)
    plt.title("Top {0} used shell commands.".format(min(n, len(counts))))
    plt.pie(x, labels=label)
    plt.show()


def print_top(n, counts, total):
    """Print the top n used commands."""
    print("{:>3} {:<20} {:<10} {:<3}".format('', "Command", "Count", "Percentage"))
    # min for when history is too small
    for i in min(range(n), range(len(counts)), key=len):
        cmd, count = counts[i]
        print("{i:>3} {cmd:<20} {count:<10} {percent:<3.3}%"
              .format(i=i+1, cmd=cmd, count=count, percent=count / total * 100))


def get_history(history_file=None):
    """Get usage history for the shell in use."""
    if history_file is None:
        shell = getenv("SHELL").split('/')[-1]
        home = getenv("HOME") + '/'
        hist_files = {"bash": [".bash_history"], "fish": [".config/fish/fish_history"],
                      "zsh": [".zhistory", ".zsh_history"]}
        if shell in hist_files:
            for hist_file in hist_files[shell]:
                if isfile(home + hist_file):
                    history_file = home + hist_file
        if not history_file:
            print("Shell history file not found.")
            exit()

    with open(history_file, 'r') as h:
        history = [l.strip() for l in h.readlines() if l.strip()]
    if shell == "fish":
        history = [l[7:] for l in history if l.startswith("- cmd:")]
    return history


if __name__ == "__main__":
    if len(argv) > 1:
        main(int(argv[1]))
    else:
        main()
{'fraction_non_alphanumeric': 0.15665735567970204, 'fraction_numerical': 0.013733705772811917, 'mean_word_length': 2.5134914145543745, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3570197', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1373, 'n_words': 404}