-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuhist
executable file
·61 lines (47 loc) · 1.45 KB
/
uhist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import argparse
from collections import Counter
from os.path import expanduser
# Commands like 'git' where 'git add' and 'git rebase' should be treated as
# distinctly different.
MULTI_COMMAND_COMMANDS = {
"cargo",
"docker",
"git",
}
COMMAND_WRAPPERS = {
"notify",
"sudo",
"time",
}
BASH_HISTORY = expanduser("~/.bash_history")
def get_subcommands(command):
"""Takes the root, or root + subcommand if indicated by MULTI_COMMAND_COMMANDS."""
if not command:
return ""
# Strip off the wrapper
if command[0] in COMMAND_WRAPPERS and len(command) >= 2:
command = command[1:]
root = command[0]
# Concatenate any subcommands
if root in MULTI_COMMAND_COMMANDS and len(command) >= 2:
root = root + " " + command[1]
return root
def main(n):
"""Analyze and print statistics about ~/.bash_history"""
with open(BASH_HISTORY, "r") as history:
history = Counter(get_subcommands(c.split()) for c in history)
total = sum(history.values())
for command, count in history.most_common(n):
print("\t{}\t{:05.2f}%\t{}".format(count, (count / total) * 100, command))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-n",
"--number",
default=10,
type=int,
help="The number of most-common items to show",
)
args = parser.parse_args()
main(args.number)