Improved history command
This commit is contained in:
@@ -55,16 +55,14 @@ def get_dir_time(path: Path) -> str:
|
|||||||
def add_history(path: Path) -> list[HistoryEntry]:
|
def add_history(path: Path) -> list[HistoryEntry]:
|
||||||
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
||||||
logs = list_history()
|
logs = list_history()
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
with locked_open(user_history_file(), "w+") as f:
|
|
||||||
for entry in logs:
|
for entry in logs:
|
||||||
if entry.path == str(path):
|
if entry.path == str(path):
|
||||||
found = True
|
found = True
|
||||||
entry.last_used = datetime.datetime.now().isoformat()
|
entry.last_used = datetime.datetime.now().isoformat()
|
||||||
|
|
||||||
flake = inspect_flake(path, "defaultVM")
|
flake = inspect_flake(path, "defaultVM")
|
||||||
|
|
||||||
flake.flake_url = str(flake.flake_url)
|
flake.flake_url = str(flake.flake_url)
|
||||||
dir_datetime = get_dir_time(path)
|
dir_datetime = get_dir_time(path)
|
||||||
|
|
||||||
@@ -74,10 +72,10 @@ def add_history(path: Path) -> list[HistoryEntry]:
|
|||||||
path=str(path),
|
path=str(path),
|
||||||
last_used=datetime.datetime.now().isoformat(),
|
last_used=datetime.datetime.now().isoformat(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
logs.append(history)
|
logs.append(history)
|
||||||
|
|
||||||
|
with locked_open(user_history_file(), "w+") as f:
|
||||||
f.write(json.dumps(logs, cls=EnhancedJSONEncoder, indent=4))
|
f.write(json.dumps(logs, cls=EnhancedJSONEncoder, indent=4))
|
||||||
f.truncate()
|
f.truncate()
|
||||||
|
|
||||||
|
|||||||
@@ -11,16 +11,19 @@ from .add import EnhancedJSONEncoder, HistoryEntry, get_dir_time, list_history
|
|||||||
|
|
||||||
|
|
||||||
def update_history() -> list[HistoryEntry]:
|
def update_history() -> list[HistoryEntry]:
|
||||||
with locked_open(user_history_file(), "w+") as f:
|
|
||||||
logs = list_history()
|
logs = list_history()
|
||||||
|
|
||||||
new_logs = []
|
new_logs = []
|
||||||
for entry in logs:
|
for entry in logs:
|
||||||
new_entry = copy.deepcopy(entry)
|
new_entry = copy.deepcopy(entry)
|
||||||
new_time = get_dir_time(Path(entry.path))
|
new_time = get_dir_time(Path(entry.path))
|
||||||
if new_time != entry.dir_datetime:
|
if new_time != entry.dir_datetime:
|
||||||
|
print(f"Updating {entry.path} from {entry.dir_datetime} to {new_time}")
|
||||||
new_entry.dir_datetime = new_time
|
new_entry.dir_datetime = new_time
|
||||||
new_entry.last_used = datetime.datetime.now().isoformat()
|
new_entry.last_used = datetime.datetime.now().isoformat()
|
||||||
new_logs.append(new_entry)
|
new_logs.append(new_entry)
|
||||||
|
|
||||||
|
with locked_open(user_history_file(), "w+") as f:
|
||||||
f.write(json.dumps(new_logs, cls=EnhancedJSONEncoder, indent=4))
|
f.write(json.dumps(new_logs, cls=EnhancedJSONEncoder, indent=4))
|
||||||
f.truncate()
|
f.truncate()
|
||||||
return new_logs
|
return new_logs
|
||||||
|
|||||||
Reference in New Issue
Block a user