mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-07 09:02:32 +00:00
feat: change output to echo via pager
This commit is contained in:
@@ -1 +1 @@
|
||||
0.4.1
|
||||
0.5.0
|
||||
|
61
rfd/cli.py
61
rfd/cli.py
@@ -42,25 +42,29 @@ def print_version(ctx, value):
|
||||
ctx.exit()
|
||||
|
||||
|
||||
def display_thread(click, thread, count): # pylint: disable=redefined-outer-name
|
||||
dealer = thread.dealer_name
|
||||
if dealer and dealer is not None:
|
||||
dealer = "[" + dealer + "] "
|
||||
else:
|
||||
dealer = ""
|
||||
click.echo(
|
||||
" "
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
+ "%s%s" % (dealer, thread.title)
|
||||
+ Fore.LIGHTYELLOW_EX
|
||||
+ " (%d views)" % thread.views
|
||||
+ Fore.RESET
|
||||
)
|
||||
click.echo(Fore.BLUE + " {}".format(thread.url))
|
||||
click.echo(Style.RESET_ALL)
|
||||
def generate_thread_output(_threads):
|
||||
for count, thread in enumerate(_threads, 1):
|
||||
output = ""
|
||||
dealer = thread.dealer_name
|
||||
if dealer and dealer is not None:
|
||||
dealer = "[" + dealer + "] "
|
||||
else:
|
||||
dealer = ""
|
||||
output += (
|
||||
" "
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
+ "%s%s" % (dealer, thread.title)
|
||||
+ Fore.LIGHTYELLOW_EX
|
||||
+ " (%d views)" % thread.views
|
||||
+ Fore.RESET
|
||||
)
|
||||
output += Fore.BLUE + " {}".format(thread.url)
|
||||
output += Style.RESET_ALL
|
||||
output += "\n\n"
|
||||
yield output
|
||||
|
||||
|
||||
@click.group(invoke_without_command=True)
|
||||
@@ -137,17 +141,17 @@ def threads(limit, forum_id, sort_by):
|
||||
_threads = sort_threads(
|
||||
parse_threads(get_threads(forum_id, limit), limit), sort_by=sort_by
|
||||
)
|
||||
for count, thread in enumerate(_threads, 1):
|
||||
display_thread(click, thread, count)
|
||||
click.echo_via_pager(generate_thread_output(_threads))
|
||||
|
||||
|
||||
@cli.command(short_help="Search deals based on a regular expression.")
|
||||
@click.option("--num-pages", default=5, help="Number of pages to search.")
|
||||
@click.option("--pages", default=5, help="Number of pages to search.")
|
||||
@click.option(
|
||||
"--forum-id", default=9, help="The forum id number. Defaults to 9 (hot deals)."
|
||||
)
|
||||
@click.option("--sort-by", default=None, help="Sort threads by")
|
||||
@click.argument("regex")
|
||||
def search(num_pages, forum_id, regex):
|
||||
def search(pages, forum_id, sort_by, regex):
|
||||
"""Search deals based on regex.
|
||||
|
||||
Popular forum ids:
|
||||
@@ -165,9 +169,12 @@ def search(num_pages, forum_id, regex):
|
||||
88 \t cell phones
|
||||
"""
|
||||
|
||||
count = 0
|
||||
for page in range(1, num_pages):
|
||||
matched_threads = []
|
||||
|
||||
for page in range(1, pages):
|
||||
_threads = parse_threads(get_threads(forum_id, 100, page=page), limit=100)
|
||||
for thread in search_threads(threads=_threads, regex=regex):
|
||||
count += 1
|
||||
display_thread(click, thread, count)
|
||||
matched_threads.append(thread)
|
||||
click.echo_via_pager(
|
||||
generate_thread_output(sort_threads(matched_threads, sort_by=sort_by))
|
||||
)
|
||||
|
Reference in New Issue
Block a user