From ee6939aafe34859bb1cb54013daf7fe0fae43ae3 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Mon, 3 Aug 2020 15:37:39 -0400 Subject: [PATCH] feat: change output to echo via pager --- Makefile | 11 +++++++++- README.md | 6 +++--- rfd/VERSION | 2 +- rfd/cli.py | 61 +++++++++++++++++++++++++++++------------------------ tox.ini | 5 ----- 5 files changed, 48 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index fe5e725..7ba1813 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,15 @@ lint: test: tmp/.tests-passed.sentinel .PHONY: test +## examples: Run basic commands +examples: tmp/.tests-passed.sentinel +> rfd --version +> rfd threads >/dev/null +> rfd threads --sort-by score >/dev/null +> rfd search 'pizza' >/dev/null +> rfd search '(coffee|starbucks)' >/dev/null +.PHONY: examples + # Tests - re-ran if any file under src has been changed since tmp/.tests-passed.sentinel was last touched tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f) > mkdir -p $(@D) @@ -45,7 +54,7 @@ tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f) pr: precommit lint test .PHONY: pr -ci: lint test +ci: lint test examples .PHONY: ci ## help: Print this help message diff --git a/README.md b/README.md index ff9e371..901c877 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,8 @@ Hot deals on the command line. [![Dependabot](https://badgen.net/badge/Dependabot/enabled/green?icon=dependabot)](https://dependabot.com/) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/davegallant/rfd.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/davegallant/rfd/context:python) - ![screenshot](https://user-images.githubusercontent.com/4519234/85969861-e10a4100-b996-11ea-9a31-6203322c60ee.png) - ## Install ```bash @@ -37,6 +35,7 @@ Commands: ## Examples ### View Hot Deals + ```console $ rfd threads ``` @@ -52,6 +51,7 @@ $ rfd threads --sort-by views --limit 40 ``` ### Simple Search + ```console $ rfd search 'pizza' ``` @@ -61,7 +61,7 @@ $ rfd search 'pizza' Regular expressions can be used for search. ```console -$ rfd search '(coffee|starbucks)' --num-pages 100 +$ rfd search '(coffee|starbucks)' --pages 10 --sort-by views ``` ## Shell Completion diff --git a/rfd/VERSION b/rfd/VERSION index 267577d..8f0916f 100644 --- a/rfd/VERSION +++ b/rfd/VERSION @@ -1 +1 @@ -0.4.1 +0.5.0 diff --git a/rfd/cli.py b/rfd/cli.py index 008723f..d0bccb9 100644 --- a/rfd/cli.py +++ b/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)) + ) diff --git a/tox.ini b/tox.ini index a72cb78..81d8bd6 100644 --- a/tox.ini +++ b/tox.ini @@ -9,8 +9,3 @@ envlist = py{27, passenv = SSH_AUTH_SOCK commands = make ci - rfd --version - rfd threads - rfd threads --sort-by score - rfd search 'pizza' - rfd search '(coffee|starbucks)'