1
+ import pathlib
1
2
from dataclasses import dataclass
2
3
3
4
from textual import on
@@ -53,19 +54,15 @@ class GoToSubmitted(Message):
53
54
def __init__ (self , * args , suggestor : Suggester | None = None , ** kwargs ):
54
55
super ().__init__ (* args , ** kwargs )
55
56
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
+
56
63
self ._active_filter : dict [bool , str | None ] = {True : None , False : None }
57
64
self ._suggestor = suggestor
58
65
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
-
69
66
def query_failed (self , query : str ):
70
67
self ._history .remove (query )
71
68
for child in self .walk_children (ListItem ):
@@ -86,8 +83,15 @@ async def apply_filter(self, event: Input.Submitted):
86
83
the_list = self .query_one (ListView )
87
84
if new_value :
88
85
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 ] :
90
87
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
+
91
95
the_list .index = None
92
96
await the_list .insert (0 , [ListItem (Label (new_value ), name = new_value )])
93
97
the_list .index = 0
0 commit comments