Filter show by pattern, if requested

This commit is contained in:
John Doty 2024-07-10 09:37:50 +09:00
parent 26353a2779
commit 34c0b6dd7d
2 changed files with 15 additions and 5 deletions

View file

@ -120,11 +120,16 @@ def refresh(url):
@cli.command(name="show")
def show():
"""Show feeds."""
@click.argument("pattern", required=False, default="")
def show(pattern):
"""Show feeds and entries.
If a pattern is supplied, then filter the feeds to urls or titles that
match the pattern. Otherwise, just show everything.
"""
db = database.Database.local()
feeds = db.load_all()
feeds = db.load_all(pattern=pattern or "")
def feed_sort_key(f: feed.Feed) -> int:
if len(f.entries) > 0:

View file

@ -153,7 +153,9 @@ class Database:
for url, last_fetched_ts, retry_after_ts, status, etag, modified in rows
]
def load_all(self, feed_limit: int = 20) -> list[feed.Feed]:
def load_all(self, feed_limit: int = 20, pattern: str = "") -> list[feed.Feed]:
pattern = pattern.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
sql_pattern = f"%{pattern}%"
cursor = self.db.execute(
"""
SELECT
@ -166,7 +168,10 @@ class Database:
title,
link
FROM feeds
"""
WHERE title LIKE :sql_pattern ESCAPE '\\'
OR link LIKE :sql_pattern ESCAPE '\\'
""",
{"sql_pattern": sql_pattern},
)
rows = cursor.fetchall()