From 6f41902db07ca2eb7f20f6b089100432dffe10f1 Mon Sep 17 00:00:00 2001 From: Alexander Lin Date: Sat, 29 Oct 2016 15:38:12 -0700 Subject: [PATCH] Add single line display mode to `gh issues` Closes #61 --- gitsome/formatter.py | 28 +++++++++++++++++++++------- gitsome/github.py | 17 ++++++++++++++--- gitsome/githubcli.py | 10 ++++++++-- tests/data/issue.py | 1 + tests/test_github.py | 8 +++++++- tests/test_github_cli.py | 3 ++- 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/gitsome/formatter.py b/gitsome/formatter.py index 4c06149..738ae99 100644 --- a/gitsome/formatter.py +++ b/gitsome/formatter.py @@ -497,13 +497,7 @@ def format_issue(self, view_entry): :return: The formatted issue. """ issue = view_entry.item - item = self.format_index_title(view_entry.index, issue.title) - item += click.style('@' + str(issue.user) + ' ', - fg=self.config.clr_primary) - item += click.style(('(' + - self.format_issues_url_from_issue(issue) + - ')'), - fg=self.config.clr_view_link) + item = self.format_issue_short(view_entry) item += '\n' indent = ' ' if len(item) == 8: @@ -522,6 +516,26 @@ def format_issue(self, view_entry): fg=self.config.clr_quaternary) return item + def format_issue_short(self, view_entry): + """Format an issue into a single line. + + :type view_entry: :class:`github3` Issue + :param view_entry: An instance of `github3` Issue. + + :rtype: str + :return: The formatted issue. + """ + + issue = view_entry.item + item = self.format_index_title(view_entry.index, issue.title) + item += click.style('@' + str(issue.user) + ' ', + fg=self.config.clr_primary) + item += click.style(('(' + + self.format_issues_url_from_issue(issue) + + ')'), + fg=self.config.clr_view_link) + return item + def format_repo(self, view_entry): """Format a repo. diff --git a/gitsome/github.py b/gitsome/github.py index b16f69a..d621109 100644 --- a/gitsome/github.py +++ b/gitsome/github.py @@ -417,7 +417,8 @@ def issue(self, user_repo_number): self.web_viewer.view_url(url) @authenticate - def issues(self, issues_list, limit=1000, pager=False, sort=True): + def issues(self, issues_list, limit=1000, short=False, pager=False, + sort=True): """List all issues. :type issues_list: list @@ -430,6 +431,9 @@ def issues(self, issues_list, limit=1000, pager=False, sort=True): :param pager: Determines whether to show the output in a pager, if available. + :type short: bool + :param short: Determines whether to display only a single-line view. + :type sort: bool :param sort: Determines whether to sort the issues by: state, repo, created_at. @@ -446,14 +450,20 @@ def issues(self, issues_list, limit=1000, pager=False, sort=True): sort_key_tertiary=current_issue.created_at)) if sort: view_entries = sorted(view_entries, reverse=False) + + if short: + format_fn = self.formatter.format_issue_short + else: + format_fn = self.formatter.format_issue + self.table.build_table(view_entries, limit, pager, - self.formatter.format_issue) + format_fn) @authenticate def issues_setup(self, issue_filter='subscribed', issue_state='open', - limit=1000, pager=False): + limit=1000, short=False, pager=False): """Prepare to list all issues matching the filter. :type issue_filter: str @@ -472,6 +482,7 @@ def issues_setup(self, issue_filter='subscribed', issue_state='open', """ self.issues(self.config.api.issues(issue_filter, issue_state), limit, + short, pager) @authenticate diff --git a/gitsome/githubcli.py b/gitsome/githubcli.py index 74422de..66796ed 100644 --- a/gitsome/githubcli.py +++ b/gitsome/githubcli.py @@ -350,8 +350,9 @@ def issue(github, user_repo_number): @click.option('-s', '--issue_state', required=False, default='open') @click.option('-l', '--limit', required=False, default=1000) @click.option('-p', '--pager', is_flag=True) + @click.option('-t', '--short', is_flag=True) @pass_github - def issues(github, issue_filter, issue_state, limit, pager): + def issues(github, issue_filter, issue_state, limit, short, pager): """List all issues matching the filter. Usage: @@ -364,6 +365,7 @@ def issues(github, issue_filter, issue_state, limit, pager): gh issues -s all -l 20 -p gh issues --issue_state closed --limit 20 --pager gh issues -f created -s all -p + gh issues -t :type github: :class:`github.GitHub` :param github: An instance of `github.GitHub`. @@ -377,11 +379,15 @@ def issues(github, issue_filter, issue_state, limit, pager): :type limit: int :param limit: The number of items to display. + :type short: bool + :param short: Determines whether to show a single-line view of + the issue. + :type pager: bool :param pager: Determines whether to show the output in a pager, if available. """ - github.issues_setup(issue_filter, issue_state, limit, pager) + github.issues_setup(issue_filter, issue_state, limit, short, pager) @cli.command() @click.argument('license_name') diff --git a/tests/data/issue.py b/tests/data/issue.py index a0e3d1b..b5426b4 100644 --- a/tests/data/issue.py +++ b/tests/data/issue.py @@ -14,4 +14,5 @@ # language governing permissions and limitations under the License. formatted_issues = u'\x1b[35m 1. \x1b[0mtitle1 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/1)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n\x1b[35m 2. \x1b[0mtitle2 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/2)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n\x1b[35m 3. \x1b[0mtitle3 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/3)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n View the page for \x1b[0m\x1b[35m1 through \x1b[0m\x1b[35m3\x1b[0m with the following command:\n\x1b[0m\x1b[35m gh view [#] \x1b[0moptional: [-b/--browser] [--help]\n\x1b[0m\x1b[0m' +formatted_issues_short = u'\x1b[35m 1. \x1b[0mtitle1 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/1)\x1b[0m\n\x1b[35m 2. \x1b[0mtitle2 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/2)\x1b[0m\n\x1b[35m 3. \x1b[0mtitle3 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/3)\x1b[0m\n View the page for \x1b[0m\x1b[35m1 through \x1b[0m\x1b[35m3\x1b[0m with the following command:\n\x1b[0m\x1b[35m gh view [#] \x1b[0moptional: [-b/--browser] [--help]\n\x1b[0m\x1b[0m' formatted_pull_requests = u'\x1b[35m 1. \x1b[0mtitle1 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/1)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n\x1b[35m 2. \x1b[0mtitle1 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/1)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n\x1b[35m 3. \x1b[0mtitle1 \x1b[0m@user2 \x1b[0m\x1b[35m(user1/repo1/issues/1)\x1b[0m\n\x1b[32m State: open \x1b[0m\x1b[36mComments: 1 \x1b[0m\x1b[33mAssignee: user1 \x1b[0m\n View the page for \x1b[0m\x1b[35m1 through \x1b[0m\x1b[35m3\x1b[0m with the following command:\n\x1b[0m\x1b[35m gh view [#] \x1b[0moptional: [-b/--browser] [--help]\n\x1b[0m\x1b[0m' diff --git a/tests/test_github.py b/tests/test_github.py index 261620c..91dcea6 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -29,7 +29,8 @@ from tests.data.events import formatted_events from tests.data.user import formatted_org, formatted_user, formatted_users from tests.data.gitignores import formatted_gitignores, formatted_gitignores_tip -from tests.data.issue import formatted_issues, formatted_pull_requests +from tests.data.issue import (formatted_issues, formatted_issues_short, + formatted_pull_requests) from tests.data.license import formatted_licenses, formatted_licenses_tip from tests.data.thread import formatted_threads from tests.data.trends import formatted_trends @@ -192,6 +193,11 @@ def test_issues_setup(self, mock_click_secho): self.github.issues_setup() mock_click_secho.assert_called_with(formatted_issues) + @mock.patch('gitsome.github.click.secho') + def test_issues_setup_short(self, mock_click_secho): + self.github.issues_setup(short=True) + mock_click_secho.assert_called_with(formatted_issues_short) + @mock.patch('gitsome.github.click.secho') def test_license(self, mock_click_secho): self.github.license('valid_license') diff --git a/tests/test_github_cli.py b/tests/test_github_cli.py index 8a45790..35d1fa2 100644 --- a/tests/test_github_cli.py +++ b/tests/test_github_cli.py @@ -135,8 +135,9 @@ def test_issues(self, mock_gh_call): '--issue_filter', 'mentioned', '--issue_state', 'closed', '--limit', '10', + '--short', '--pager']) - mock_gh_call.assert_called_with('mentioned', 'closed', 10, True) + mock_gh_call.assert_called_with('mentioned', 'closed', 10, True, True) assert result.exit_code == 0 @mock.patch('gitsome.githubcli.GitHub.license')