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