-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchid_command.py
executable file
·123 lines (95 loc) · 3.86 KB
/
chid_command.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import annotations
from pathlib import Path
import sys
from typing import Optional
from chid_utils import get_destination_path, get_count_uses, get_history_directory_path, get_root_path, split_line
import argparse
try:
from rich import print
except ImportError:
...
def display_by_occurance(destination_file: Path, reverse: bool, limit: Optional[int] = None, contain: str = "") -> list[tuple[str]]:
with open(destination_file, "r") as file:
w = file.readlines()
if reverse:
w = reversed(w)
s = set()
index = 0
result = []
for line in w:
time, command = split_line(line)
if command not in s and contain in command:
index += 1
s.add(command)
# Index;Time;Percent;Command
result.append(f"{index};{time};;{command}")
if limit is not None and limit <= index:
return result
return result
def display_by_frequency(destination_file: Path, limit: Optional[int] = None, contain: str = "") -> list[tuple[str]]:
with open(destination_file, "r") as file:
counted_uses = get_count_uses(file, contain)
total = sum(counted_uses.values())
result = []
for index, line in enumerate(counted_uses.most_common(limit)):
command, occurence = line
# Index;Time;Percent;Command
result.append(f"{index+1};;{occurence/total:4.2%};{command}")
return result
def delete_commands(destination_file: Path) -> None:
destination_file.unlink(missing_ok=True)
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
def save_to_file(output):
root_path = get_root_path()
with open(root_path / "last_output", "w") as file:
for line in output:
file.write(line)
file.write("\n")
def display(output):
ln = len(str(len(output)))
for line in output:
index, time, percent, command = line.split(";", 3)
if index:
print(f"{index:<{ln}} ", end="")
if percent:
print(f"{percent:>6} ", end="")
if command:
print(f"{command} ", end="")
print()
def main():
parser = argparse.ArgumentParser(prog="chid", description="Find history for current directory", epilog="")
parser.add_argument("dir", help=argparse.SUPPRESS)
parser.add_argument("alias", help=argparse.SUPPRESS)
group = parser.add_mutually_exclusive_group()
group.add_argument("--last-occurance", "--sl", action="store_true", help="Sort by last execution")
group.add_argument("--first-occurance", "--sf", action="store_true", help="Sort by first execution")
parser.add_argument("-l", "--limit", type=check_positive, help="Limit size of output", default=20)
parser.add_argument("-d", "--delete", action="store_true", default=False)
parser.add_argument("-c", "--contain", help="String that command needs to contain", default="")
args = parser.parse_args()
dir = args.dir
limit = args.limit
contain = args.contain
history_directory_path = get_history_directory_path()
destination_path = get_destination_path(history_directory_path, dir)
destination_file = destination_path / "chid_commands"
if not destination_file.exists():
return
if args.delete:
delete_commands(destination_file)
sys.exit()
if args.first_occurance:
output = display_by_occurance(destination_file, False, limit, contain)
elif args.last_occurance:
output = display_by_occurance(destination_file, True, limit, contain)
else:
output = display_by_frequency(destination_file, limit, contain)
save_to_file(output)
display(output)
if __name__ == "__main__":
""" """
main()