-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
100 lines (84 loc) · 3.39 KB
/
utilities.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
import os
import readline
import atexit
class PathHistory:
def __init__(self, max_history=10):
self.history = []
self.max_history = max_history
def add(self, path):
if path in self.history:
self.history.remove(path)
self.history.insert(0, path)
if len(self.history) > self.max_history:
self.history.pop()
def get(self):
return self.history
def clear(self):
self.history.clear()
class CommandLineInterface:
def __init__(self):
self.path_history = PathHistory()
self.history_file = os.path.expanduser('~/.exiftool_search_history')
# Set up readline
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
# Set a reasonable history length
readline.set_history_length(1000)
# Load history file if it exists
if os.path.exists(self.history_file):
readline.read_history_file(self.history_file)
# Save history on exit
atexit.register(readline.write_history_file, self.history_file)
def input(self, prompt):
user_input = input(prompt)
return user_input
def prompt_for_directory(self, prompt_message):
while True:
directory_input = self.input(prompt_message).strip()
if directory_input.lower() == 'h':
return self.show_and_select_history()
elif directory_input:
paths = self.process_directory_input(directory_input)
if paths:
return paths
print("Invalid input. Please enter valid directory path(s) or 'h' for history.")
def show_and_select_history(self):
history = self.path_history.get()
if not history:
print("History is empty.")
return None
print("Recent directories:")
for i, path in enumerate(history, 1):
print(f"{i}: {path}")
selection = self.input("Enter the number(s) of the directory(ies) you want to use (comma-separated) or 'c' to cancel: ")
if selection.lower() == 'c':
return None
try:
selected_indices = [int(i.strip()) - 1 for i in selection.split(',')]
selected_paths = [history[i] for i in selected_indices if 0 <= i < len(history)]
if not selected_paths:
print("No valid selections made.")
return None
return selected_paths
except ValueError:
print("Invalid input. Please enter numbers separated by commas.")
return None
def process_directory_input(self, input_string):
paths = []
for item in input_string.split(','):
item = item.strip()
if item.isdigit():
history = self.path_history.get()
index = int(item) - 1
if 0 <= index < len(history):
paths.append(history[index])
else:
print(f"Invalid history index: {item}")
return None
elif os.path.isdir(item):
paths.append(os.path.abspath(item))
self.path_history.add(os.path.abspath(item))
else:
print(f"Invalid directory path: {item}")
return None
return paths if paths else None