Skip to content

Commit 24ce852

Browse files
committed
Persist filter queries
1 parent 9855fb8 commit 24ce852

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/dt_browser/filter_box.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
from dataclasses import dataclass
23

34
from textual import on
@@ -53,19 +54,15 @@ class GoToSubmitted(Message):
5354
def __init__(self, *args, suggestor: Suggester | None = None, **kwargs):
5455
super().__init__(*args, **kwargs)
5556
self._history: list[str] = []
57+
self._history_file = pathlib.Path("~/.cache/dtbrowser/filters.txt").expanduser()
58+
self._history_file.parent.mkdir(exist_ok=True, parents=True)
59+
if self._history_file.exists():
60+
with self._history_file.open("r", encoding="utf-8") as f:
61+
self._history = [x.rstrip() for x in f.readlines()]
62+
5663
self._active_filter: dict[bool, str | None] = {True: None, False: None}
5764
self._suggestor = suggestor
5865

59-
def save_state(self, existing: dict) -> dict:
60-
history = self._history.copy()
61-
for x in existing["history"]:
62-
if x not in history:
63-
history.append(x)
64-
return {"history": history}
65-
66-
def load_state(self, state: dict, *_):
67-
self._history = state["history"]
68-
6966
def query_failed(self, query: str):
7067
self._history.remove(query)
7168
for child in self.walk_children(ListItem):
@@ -86,8 +83,15 @@ async def apply_filter(self, event: Input.Submitted):
8683
the_list = self.query_one(ListView)
8784
if new_value:
8885
self.query_one(Input).value = new_value
89-
if new_value not in self._history:
86+
if new_value not in self._history[0:10]:
9087
self._history.append(new_value)
88+
if len(self._history) < 100:
89+
with self._history_file.open("a+", encoding="utf-8") as f:
90+
f.write(f"{new_value}\n")
91+
else:
92+
with self._history_file.open("w+", encoding="utf-8") as f:
93+
f.writelines([f"{x}\n" for x in self._history[-100:]])
94+
9195
the_list.index = None
9296
await the_list.insert(0, [ListItem(Label(new_value), name=new_value)])
9397
the_list.index = 0

0 commit comments

Comments
 (0)