From 42235e39bed8c3b6d4b0b0c18129a0c9d08dcaf1 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Mon, 1 Jan 2024 18:24:40 +0100 Subject: [PATCH] Added settings attribute to history file --- pkgs/clan-cli/clan_cli/history/add.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkgs/clan-cli/clan_cli/history/add.py b/pkgs/clan-cli/clan_cli/history/add.py index 8a1f01c09..7efa9612c 100644 --- a/pkgs/clan-cli/clan_cli/history/add.py +++ b/pkgs/clan-cli/clan_cli/history/add.py @@ -23,12 +23,28 @@ class EnhancedJSONEncoder(json.JSONEncoder): class HistoryEntry: last_used: str flake: FlakeConfig + settings: dict[str, Any] = dataclasses.field(default_factory=dict) def __post_init__(self) -> None: if isinstance(self.flake, dict): self.flake = FlakeConfig(**self.flake) +def merge_dicts(d1: dict, d2: dict) -> dict: + # create a new dictionary that copies d1 + merged = dict(d1) + # iterate over the keys and values of d2 + for key, value in d2.items(): + # if the key is in d1 and both values are dictionaries, merge them recursively + if key in d1 and isinstance(d1[key], dict) and isinstance(value, dict): + merged[key] = merge_dicts(d1[key], value) + # otherwise, update the value of the key in the merged dictionary + else: + merged[key] = value + # return the merged dictionary + return merged + + def list_history() -> list[HistoryEntry]: logs: list[HistoryEntry] = [] if not user_history_file().exists(): @@ -36,6 +52,8 @@ def list_history() -> list[HistoryEntry]: try: parsed = read_history_file() + for i, p in enumerate(parsed.copy()): + parsed[i] = merge_dicts(p, p["settings"]) logs = [HistoryEntry(**p) for p in parsed] except (json.JSONDecodeError, TypeError) as ex: print("Failed to load history. Invalid JSON.")