-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to remember read news items
The state is stored in $XDG_STATE_HOME/ruyi/news.read.txt. The stored data is just plain text, with one alphabetically-sorted news item ID per line.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class NewsReadStatusStore: | ||
def __init__(self, path: str) -> None: | ||
self._path = path | ||
self._status = set() | ||
self._orig_status = set() | ||
|
||
def load(self) -> None: | ||
try: | ||
with open(self._path, "r") as fp: | ||
for l in fp: | ||
self._orig_status.add(l.strip()) | ||
except FileNotFoundError: | ||
return | ||
|
||
self._status = self._orig_status.copy() | ||
|
||
def __contains__(self, key: str) -> bool: | ||
return key in self._status | ||
|
||
def add(self, id: str) -> None: | ||
return self._status.add(id) | ||
|
||
def save(self) -> None: | ||
if self._status == self._orig_status: | ||
return | ||
|
||
content = "".join(f"{id}\n" for id in self._status) | ||
with open(self._path, "w", encoding="utf-8") as fp: | ||
fp.write(content) |