-
Notifications
You must be signed in to change notification settings - Fork 2
/
most_used_programs
executable file
·56 lines (49 loc) · 1.57 KB
/
most_used_programs
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, re
aliases_file_name = "/tmp/aliases.tmp"
history_file_name = os.path.expanduser("~") + "/.zsh_history"
os.system(f'zsh -c "source ~/.zshrc; alias > {aliases_file_name}"')
aliases = {}
with open(aliases_file_name) as aliases_file:
for line in aliases_file:
try:
alias, command = line.strip().split("=", 1)
aliases[alias] = re.sub("'", "", command.split()[0])
except:
continue
print("error line: ", line)
statistics = {}
with open(history_file_name, encoding="utf8", errors='ignore') as history_file:
for line in history_file:
# print(line)
# if only as \n char we should ignore it
if len(line) == 1:
continue
try:
trash, command = line.strip().split(";", 1)
command = command.split()[0]
except:
continue
print("error line: ", line)
if command in aliases:
if aliases[command] in statistics:
statistics[aliases[command]] += 1
else:
statistics[aliases[command]] = 1
else:
if command in statistics:
statistics[command] += 1
else:
statistics[command] = 1
total = 0
for v in statistics.values():
total += v
results = sorted(statistics.items(), key=lambda x: x[1], reverse=True)
n = 1
for result in results:
k, v = result
print("{:3} {:5.2f}% {:5} {}".format(n, v / total * 100, v, k))
if n == 40:
break
n = n + 1