From af969d1a49d3759fc9d51ebcd312d518c665ff06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 19:25:29 +0200 Subject: [PATCH 01/16] Subscriptions page foundations --- rtv/__main__.py | 3 ++- rtv/content.py | 62 +++++++++++++++++++++++++++++++++++++++++-- rtv/exceptions.py | 4 +++ rtv/page.py | 16 +++++++++++ rtv/subscriptions.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 rtv/subscriptions.py diff --git a/rtv/__main__.py b/rtv/__main__.py index af50d7d..733b02b 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -10,10 +10,11 @@ import praw.errors from six.moves import configparser from . import config -from .exceptions import SubmissionError, SubredditError, ProgramError +from .exceptions import SubmissionError, SubredditError, SubscriptionError, ProgramError from .curses_helpers import curses_session from .submission import SubmissionPage from .subreddit import SubredditPage +from .subscriptions import SubscriptionPage from .docs import * from .__version__ import __version__ diff --git a/rtv/content.py b/rtv/content.py index c0d397f..f4031da 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -4,10 +4,10 @@ import praw import requests import re -from .exceptions import SubmissionError, SubredditError, AccountError +from .exceptions import SubmissionError, SubredditError, SubscriptionError, AccountError from .helpers import humanize_timestamp, wrap_text, strip_subreddit_url -__all__ = ['SubredditContent', 'SubmissionContent'] +__all__ = ['SubredditContent', 'SubmissionContent', 'SubscriptionContent'] _logger = logging.getLogger(__name__) @@ -143,6 +143,20 @@ class BaseContent(object): return data + @staticmethod + def strip_praw_subscription(subscription): + """ + Parse through a subscription and return a dict with data ready to be + displayed through the terminal. + """ + + data = {} + data['object'] = subscription + data['type'] = 'Subscription' + data['name'] = subscription._case_name + data['title'] = subscription.title + + return data class SubmissionContent(BaseContent): """ @@ -352,3 +366,47 @@ class SubredditContent(BaseContent): data['offset'] = 0 return data + +class SubscriptionContent(BaseContent): + def __init__(self, subscriptions, loader): + self.name = "Subscriptions" + self._loader = loader + self._subscriptions = subscriptions + self._subscription_data = [] + + @classmethod + def get_list(cls, reddit, loader): + try: + with loader(): + subscriptions = reddit.get_my_subreddits(limit=None) + except praw.errors.APIException: + raise SubscriptionError() + + return cls(subscriptions, loader) + + def get(self, index, n_cols=70): + """ + Grab the `i`th subscription, with the title field formatted to fit inside + of a window of width `n_cols` + """ + + if index < 0: + raise IndexError + + while index >= len(self._subscription_data): + try: + with self._loader(): + subscription = next(self._subscriptions) + except StopIteration: + raise IndexError + else: + data = self.strip_praw_subscription(subscription) + self._subscription_data.append(data) + + data = self._subscription_data[index] + subreddit_info = "/r/" + data['name'] + " - " + data['title'] + data['split_title'] = wrap_text(subreddit_info, width=n_cols) + data['n_rows'] = len(data['split_title']) + 3 + data['offset'] = 0 + + return data diff --git a/rtv/exceptions.py b/rtv/exceptions.py index dece7fe..80fb76f 100644 --- a/rtv/exceptions.py +++ b/rtv/exceptions.py @@ -24,6 +24,10 @@ class SubredditError(RTVError): self.name = name +class SubscriptionError(RTVError): + "Subscriptions could not be fetched" + + class ProgramError(RTVError): "Problem executing an external program" diff --git a/rtv/page.py b/rtv/page.py index cc4d7b4..686f5d4 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -412,6 +412,22 @@ class BasePage(object): s.catch = False self.refresh_content() + @BaseController.register('s') + def get_subscriptions(self): + """ + Displays subscribed subreddits + """ + + if not self.reddit.is_logged_in(): + show_notification(self.stdscr, ['Not logged in']) + return + + data = self.content.get(self.nav.absolute_index) + with self.safe_call as s: + subscriptions = SubscriptionPage(self.stdscr, self.reddit) + subscriptions.loop() + self.refresh_content() + @BaseController.register('i') def get_inbox(self): """ diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py new file mode 100644 index 0000000..5d9cf02 --- /dev/null +++ b/rtv/subscriptions.py @@ -0,0 +1,63 @@ +import curses +import sys +import time +import logging + +from .content import SubscriptionContent +from .page import BasePage, Navigator, BaseController +from .helpers import open_browser, open_editor +from .curses_helpers import (Color, LoadScreen, get_arrow, get_gold, add_line, + show_notification) +from .docs import COMMENT_FILE + +__all__ = ['SubscriptionController', 'SubscriptionPage'] +_logger = logging.getLogger(__name__) + +class SubscriptionController(BaseController): + character_map = {} + +class SubscriptionPage(BasePage): + def __init__(self, stdscr, reddit): + self.controller = SubscriptionController(self) + self.loader = LoadScreen(stdscr) + + content = SubscriptionContent.get_list(reddit, self.loader) + super(SubscriptionPage, self).__init__(stdscr, reddit, content) + + def loop(self): + "Main control loop" + + self.active = True + while self.active: + self.draw() + cmd = self.stdscr.getch() + self.controller.trigger(cmd) + + @SubscriptionController.register(curses.KEY_F5, 'r') + def refresh_content(self): + "Re-download all subscriptions and reset the page index" + + try: + self.content = SubscriptionContent.get_list(self.reddit, self.loader) + except AccountError: + show_notification(self.stdscr, ['Not logged in']) + except SubredditError: + show_notification(self.stdscr, ['Invalid subreddit']) + except requests.HTTPError: + show_notification(self.stdscr, ['Could not reach subreddit']) + else: + self.nav = Navigator(self.content.get) + + @staticmethod + def draw_item(win, data, inverted=False): + n_rows, n_cols = win.getmaxyx() + n_cols -= 1 # Leave space for the cursor in the first column + + # Handle the case where the window is not large enough to fit the data. + valid_rows = range(0, n_rows) + offset = 0 if not inverted else -(data['n_rows'] - n_rows) + + n_title = len(data['split_title']) + for row, text in enumerate(data['split_title'], start=offset): + if row in valid_rows: + add_line(win, text, row, 1, curses.A_BOLD) From 57d3f10caab99d6e2dab50b2aedad839315d887d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 19:33:22 +0200 Subject: [PATCH 02/16] No need for this much space --- rtv/content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtv/content.py b/rtv/content.py index f4031da..8fa3b65 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -406,7 +406,7 @@ class SubscriptionContent(BaseContent): data = self._subscription_data[index] subreddit_info = "/r/" + data['name'] + " - " + data['title'] data['split_title'] = wrap_text(subreddit_info, width=n_cols) - data['n_rows'] = len(data['split_title']) + 3 + data['n_rows'] = len(data['split_title']) data['offset'] = 0 return data From 0113f81705714d5b7577ea4a79c7d4559ed97240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 19:48:12 +0200 Subject: [PATCH 03/16] Split subreddit name and title on two lines --- rtv/content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtv/content.py b/rtv/content.py index 8fa3b65..1cc7b4d 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -404,7 +404,7 @@ class SubscriptionContent(BaseContent): self._subscription_data.append(data) data = self._subscription_data[index] - subreddit_info = "/r/" + data['name'] + " - " + data['title'] + subreddit_info = "/r/" + data['name'] + "\n" + data['title'] data['split_title'] = wrap_text(subreddit_info, width=n_cols) data['n_rows'] = len(data['split_title']) data['offset'] = 0 From be7cd1a07184a97cab727cfa345fa85c11006069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 21:07:04 +0200 Subject: [PATCH 04/16] Cleaner subscriptions draw_item() implementation --- rtv/content.py | 7 +++---- rtv/subreddit.py | 14 ++++++++++++++ rtv/subscriptions.py | 7 ++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 1cc7b4d..f77c7a9 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -153,7 +153,7 @@ class BaseContent(object): data = {} data['object'] = subscription data['type'] = 'Subscription' - data['name'] = subscription._case_name + data['name'] = "/r/" + subscription._case_name data['title'] = subscription.title return data @@ -404,9 +404,8 @@ class SubscriptionContent(BaseContent): self._subscription_data.append(data) data = self._subscription_data[index] - subreddit_info = "/r/" + data['name'] + "\n" + data['title'] - data['split_title'] = wrap_text(subreddit_info, width=n_cols) - data['n_rows'] = len(data['split_title']) + data['split_title'] = wrap_text(data['name'], width=n_cols) + data['n_rows'] = len(data['split_title']) + 1 data['offset'] = 0 return data diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 342bea2..d73fcb4 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -8,6 +8,7 @@ import requests from .exceptions import SubredditError, AccountError from .page import BasePage, Navigator, BaseController from .submission import SubmissionPage +from .subscriptions import SubscriptionPage from .content import SubredditContent from .helpers import open_browser, open_editor, strip_subreddit_url from .docs import SUBMISSION_FILE @@ -158,6 +159,19 @@ class SubredditPage(BasePage): page.loop() self.refresh_content() + @SubredditController.register('s') + def open_subscriptions(self): + "Open user subscriptions page" + + if not self.reddit.is_logged_in(): + show_notification(self.stdscr, ['Not logged in']) + return + + with self.safe_call as s: + page = SubscriptionPage(self.stdscr, self.reddit) + page.loop() + self.refresh_content() + @staticmethod def draw_item(win, data, inverted=False): diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index 5d9cf02..81b3e5a 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -60,4 +60,9 @@ class SubscriptionPage(BasePage): n_title = len(data['split_title']) for row, text in enumerate(data['split_title'], start=offset): if row in valid_rows: - add_line(win, text, row, 1, curses.A_BOLD) + attr = curses.A_BOLD | Color.YELLOW + add_line(win, u'{name}'.format(**data), row, 1, attr) + + row = n_title + offset + if row in valid_rows: + add_line(win, u'{title}'.format(**data), row, 1) From 0274823b7e767cc8cc02b8506a649c772f88644e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 23:32:17 +0200 Subject: [PATCH 05/16] Safe call is useless here + some refactoring --- rtv/content.py | 7 ++++--- rtv/subreddit.py | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index f77c7a9..6dd0b1f 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -368,21 +368,22 @@ class SubredditContent(BaseContent): return data class SubscriptionContent(BaseContent): - def __init__(self, subscriptions, loader): - self.name = "Subscriptions" + def __init__(self, name, subscriptions, loader): + self.name = name self._loader = loader self._subscriptions = subscriptions self._subscription_data = [] @classmethod def get_list(cls, reddit, loader): + display_name = "Subscriptions" try: with loader(): subscriptions = reddit.get_my_subreddits(limit=None) except praw.errors.APIException: raise SubscriptionError() - return cls(subscriptions, loader) + return cls(display_name, subscriptions, loader) def get(self, index, n_cols=70): """ diff --git a/rtv/subreddit.py b/rtv/subreddit.py index d73fcb4..4c9e448 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -167,10 +167,9 @@ class SubredditPage(BasePage): show_notification(self.stdscr, ['Not logged in']) return - with self.safe_call as s: - page = SubscriptionPage(self.stdscr, self.reddit) - page.loop() - self.refresh_content() + page = SubscriptionPage(self.stdscr, self.reddit) + page.loop() + self.refresh_content() @staticmethod def draw_item(win, data, inverted=False): From 77db8f6c5cf70b66c8ae54562981b170be132639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Sun, 9 Aug 2015 23:46:03 +0200 Subject: [PATCH 06/16] Typo --- rtv/curses_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtv/curses_helpers.py b/rtv/curses_helpers.py index f9489fd..0d48684 100644 --- a/rtv/curses_helpers.py +++ b/rtv/curses_helpers.py @@ -24,7 +24,7 @@ ESCAPE = 27 def get_gold(): """ - Return the guilded symbol. + Return the gilded symbol. """ symbol = u'\u272A' if config.unicode else '*' From 0da69a193501107ba36aeb567b90c90d52dc6d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 00:33:48 +0200 Subject: [PATCH 07/16] Ability to open highlighted subreddit and go back --- rtv/subreddit.py | 1 - rtv/subscriptions.py | 27 +++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 4c9e448..5b2ca41 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -169,7 +169,6 @@ class SubredditPage(BasePage): page = SubscriptionPage(self.stdscr, self.reddit) page.loop() - self.refresh_content() @staticmethod def draw_item(win, data, inverted=False): diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index 81b3e5a..39f9c87 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -37,16 +37,23 @@ class SubscriptionPage(BasePage): def refresh_content(self): "Re-download all subscriptions and reset the page index" - try: - self.content = SubscriptionContent.get_list(self.reddit, self.loader) - except AccountError: - show_notification(self.stdscr, ['Not logged in']) - except SubredditError: - show_notification(self.stdscr, ['Invalid subreddit']) - except requests.HTTPError: - show_notification(self.stdscr, ['Could not reach subreddit']) - else: - self.nav = Navigator(self.content.get) + self.content = SubscriptionContent.get_list(self.reddit, self.loader) + self.nav = Navigator(self.content.get) + + @SubscriptionController.register(curses.KEY_ENTER, 10) + def open_selected_subreddit(self): + "Open the selected subreddit" + + from .subreddit import SubredditPage + data = self.content.get(self.nav.absolute_index) + page = SubredditPage(self.stdscr, self.reddit, data['name'][2:]) # Strip the leading /r + page.loop() + + @SubscriptionController.register(curses.KEY_LEFT) + def close_subscriptions(self): + "Close subscriptions and return to the subreddit page" + + self.active = False @staticmethod def draw_item(win, data, inverted=False): From 57d59c034d0653fbc482d7a94185b9cbdd90fe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 00:40:04 +0200 Subject: [PATCH 08/16] Add new command to help menu --- rtv/docs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rtv/docs.py b/rtv/docs.py index f25e178..d2c5482 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -42,6 +42,7 @@ Authenticated Commands `c` : Compose a new post or comment `e` : Edit an existing post or comment `d` : Delete an existing post or comment + `s` : Open subscribed subreddits list Subreddit Mode `l` or `RIGHT` : Enter the selected submission From 2baecbdf58a9b1a1a03106b21e49cbd0eff35882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 00:56:09 +0200 Subject: [PATCH 09/16] Removing unneeded imports --- rtv/content.py | 7 +++---- rtv/subscriptions.py | 5 +---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 6dd0b1f..f77c7a9 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -368,22 +368,21 @@ class SubredditContent(BaseContent): return data class SubscriptionContent(BaseContent): - def __init__(self, name, subscriptions, loader): - self.name = name + def __init__(self, subscriptions, loader): + self.name = "Subscriptions" self._loader = loader self._subscriptions = subscriptions self._subscription_data = [] @classmethod def get_list(cls, reddit, loader): - display_name = "Subscriptions" try: with loader(): subscriptions = reddit.get_my_subreddits(limit=None) except praw.errors.APIException: raise SubscriptionError() - return cls(display_name, subscriptions, loader) + return cls(subscriptions, loader) def get(self, index, n_cols=70): """ diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index 39f9c87..a4c69a0 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -5,10 +5,7 @@ import logging from .content import SubscriptionContent from .page import BasePage, Navigator, BaseController -from .helpers import open_browser, open_editor -from .curses_helpers import (Color, LoadScreen, get_arrow, get_gold, add_line, - show_notification) -from .docs import COMMENT_FILE +from .curses_helpers import (Color, LoadScreen, add_line) __all__ = ['SubscriptionController', 'SubscriptionPage'] _logger = logging.getLogger(__name__) From ffd4d4f2a52bbc8e68ab25ec3a6181f2f69c4224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 01:16:00 +0200 Subject: [PATCH 10/16] Removing another unneeded import --- rtv/__main__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rtv/__main__.py b/rtv/__main__.py index 733b02b..8d6e561 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -14,7 +14,6 @@ from .exceptions import SubmissionError, SubredditError, SubscriptionError, Prog from .curses_helpers import curses_session from .submission import SubmissionPage from .subreddit import SubredditPage -from .subscriptions import SubscriptionPage from .docs import * from .__version__ import __version__ From 7e6f63916261aec99dfff71522a5d8f3931d2f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 01:20:20 +0200 Subject: [PATCH 11/16] Update README.rst --- README.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 4aa9083..2d87cb6 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,7 @@ Installation Install using pip .. code-block:: bash - + $ sudo pip install rtv Or clone the repository @@ -47,12 +47,12 @@ The installation will place a script in the system path $ rtv --help ===== -Usage +Usage ===== RTV supports browsing both subreddits and submission comments. -Navigating is simple and intuitive. +Navigating is simple and intuitive. Move the cursor using either the arrow keys or *Vim* style movement. Move **up** and **down** to scroll through the page. Move **right** to view the selected submission, and **left** to exit the submission. @@ -84,6 +84,7 @@ Once you are logged in your username will appear in the top-right corner of the :``c``: Compose a new post or comment :``e``: Edit an existing post or comment :``d``: Delete an existing post or comment +:``s``: Open subscribed subreddits list -------------- Subreddit Mode @@ -96,7 +97,7 @@ In subreddit mode you can browse through the top submissions on either the front :``f``: Open a prompt to search the current subreddit The ``/`` prompt accepts subreddits in the following formats - + * ``/r/python`` * ``/r/python/new`` * ``/r/python+linux`` supports multireddits @@ -126,7 +127,7 @@ You can specify which text editor you would like to use by setting the ``$RTV_ED .. code-block:: bash $ export RTV_EDITOR=gedit - + If no editor is specified, RTV will fallback to the system's default ``$EDITOR``, and finally to ``nano``. ----------- From 248a87a7e599c4204f046494e358ba41df70c1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 18:06:55 +0200 Subject: [PATCH 12/16] Some additional keybindings --- README.rst | 2 +- rtv/docs.py | 2 +- rtv/subscriptions.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 2d87cb6..59c68bc 100644 --- a/README.rst +++ b/README.rst @@ -84,7 +84,7 @@ Once you are logged in your username will appear in the top-right corner of the :``c``: Compose a new post or comment :``e``: Edit an existing post or comment :``d``: Delete an existing post or comment -:``s``: Open subscribed subreddits list +:``s``: Open/close subscribed subreddits list -------------- Subreddit Mode diff --git a/rtv/docs.py b/rtv/docs.py index d2c5482..8775728 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -42,7 +42,7 @@ Authenticated Commands `c` : Compose a new post or comment `e` : Edit an existing post or comment `d` : Delete an existing post or comment - `s` : Open subscribed subreddits list + `s` : Open/close subscribed subreddits list Subreddit Mode `l` or `RIGHT` : Enter the selected submission diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index a4c69a0..8097871 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -37,7 +37,7 @@ class SubscriptionPage(BasePage): self.content = SubscriptionContent.get_list(self.reddit, self.loader) self.nav = Navigator(self.content.get) - @SubscriptionController.register(curses.KEY_ENTER, 10) + @SubscriptionController.register(curses.KEY_ENTER, 10, curses.KEY_RIGHT) def open_selected_subreddit(self): "Open the selected subreddit" @@ -46,7 +46,7 @@ class SubscriptionPage(BasePage): page = SubredditPage(self.stdscr, self.reddit, data['name'][2:]) # Strip the leading /r page.loop() - @SubscriptionController.register(curses.KEY_LEFT) + @SubscriptionController.register(curses.KEY_LEFT, 's') def close_subscriptions(self): "Close subscriptions and return to the subreddit page" From a02322d5fe9cc3daa0baa3f263bf480ca3aadaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Mon, 10 Aug 2015 18:17:51 +0200 Subject: [PATCH 13/16] Remove old method (moved in SubredditController) --- rtv/page.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/rtv/page.py b/rtv/page.py index 686f5d4..cc4d7b4 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -412,22 +412,6 @@ class BasePage(object): s.catch = False self.refresh_content() - @BaseController.register('s') - def get_subscriptions(self): - """ - Displays subscribed subreddits - """ - - if not self.reddit.is_logged_in(): - show_notification(self.stdscr, ['Not logged in']) - return - - data = self.content.get(self.nav.absolute_index) - with self.safe_call as s: - subscriptions = SubscriptionPage(self.stdscr, self.reddit) - subscriptions.loop() - self.refresh_content() - @BaseController.register('i') def get_inbox(self): """ From 691986abd445063175822e99f1c4897cdd7a5a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Wed, 12 Aug 2015 14:39:49 +0200 Subject: [PATCH 14/16] Refactoring the way a selected sub is opened --- rtv/content.py | 2 +- rtv/subreddit.py | 6 ++++++ rtv/subscriptions.py | 14 ++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index f77c7a9..2586d89 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -153,7 +153,7 @@ class BaseContent(object): data = {} data['object'] = subscription data['type'] = 'Subscription' - data['name'] = "/r/" + subscription._case_name + data['name'] = "/r/" + subscription.display_name data['title'] = subscription.title return data diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 5b2ca41..eaecef9 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -167,9 +167,15 @@ class SubredditPage(BasePage): show_notification(self.stdscr, ['Not logged in']) return + # Open subscriptions page page = SubscriptionPage(self.stdscr, self.reddit) page.loop() + # When user has chosen a subreddit in the subscriptions list, refresh content with the selected subreddit + chosen_subreddit = page.get_selected_subreddit_data() + if chosen_subreddit is not None: + self.refresh_content(name=chosen_subreddit['name']) + @staticmethod def draw_item(win, data, inverted=False): diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index 8097871..1bddf1a 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -17,6 +17,7 @@ class SubscriptionPage(BasePage): def __init__(self, stdscr, reddit): self.controller = SubscriptionController(self) self.loader = LoadScreen(stdscr) + self.selected_subreddit_data = None content = SubscriptionContent.get_list(reddit, self.loader) super(SubscriptionPage, self).__init__(stdscr, reddit, content) @@ -30,6 +31,9 @@ class SubscriptionPage(BasePage): cmd = self.stdscr.getch() self.controller.trigger(cmd) + def get_selected_subreddit_data(self): + return self.selected_subreddit_data + @SubscriptionController.register(curses.KEY_F5, 'r') def refresh_content(self): "Re-download all subscriptions and reset the page index" @@ -38,13 +42,11 @@ class SubscriptionPage(BasePage): self.nav = Navigator(self.content.get) @SubscriptionController.register(curses.KEY_ENTER, 10, curses.KEY_RIGHT) - def open_selected_subreddit(self): - "Open the selected subreddit" + def store_selected_subreddit(self): + "Store the selected subreddit and return to the subreddit page" - from .subreddit import SubredditPage - data = self.content.get(self.nav.absolute_index) - page = SubredditPage(self.stdscr, self.reddit, data['name'][2:]) # Strip the leading /r - page.loop() + self.selected_subreddit_data = self.content.get(self.nav.absolute_index) + self.active = False @SubscriptionController.register(curses.KEY_LEFT, 's') def close_subscriptions(self): From 2c7a0ed4ba7cf281067658316d537365d86e7ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Wed, 12 Aug 2015 15:27:55 +0200 Subject: [PATCH 15/16] Wrap subreddit title instead of name --- rtv/content.py | 2 +- rtv/subscriptions.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 2586d89..43dd331 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -404,7 +404,7 @@ class SubscriptionContent(BaseContent): self._subscription_data.append(data) data = self._subscription_data[index] - data['split_title'] = wrap_text(data['name'], width=n_cols) + data['split_title'] = wrap_text(data['title'], width=n_cols) data['n_rows'] = len(data['split_title']) + 1 data['offset'] = 0 diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index 1bddf1a..cbb5926 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -66,9 +66,9 @@ class SubscriptionPage(BasePage): n_title = len(data['split_title']) for row, text in enumerate(data['split_title'], start=offset): if row in valid_rows: - attr = curses.A_BOLD | Color.YELLOW - add_line(win, u'{name}'.format(**data), row, 1, attr) + add_line(win, text, row, 1) row = n_title + offset if row in valid_rows: - add_line(win, u'{title}'.format(**data), row, 1) + attr = curses.A_BOLD | Color.YELLOW + add_line(win, u'{name}'.format(**data), row, 1, attr) From ac939b5f96b4ae2ed545f4e5698244346898e595 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Wed, 12 Aug 2015 10:12:53 -0700 Subject: [PATCH 16/16] Switched back to putting the subreddit name first on the submission page. --- rtv/content.py | 5 ++++- rtv/subreddit.py | 8 ++++---- rtv/subscriptions.py | 21 ++++++++++----------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index fca4830..afa64bd 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -385,14 +385,17 @@ class SubredditContent(BaseContent): return data class SubscriptionContent(BaseContent): + def __init__(self, subscriptions, loader): + self.name = "Subscriptions" + self.order = None self._loader = loader self._subscriptions = subscriptions self._subscription_data = [] @classmethod - def get_list(cls, reddit, loader): + def from_user(cls, reddit, loader): try: with loader(): subscriptions = reddit.get_my_subreddits(limit=None) diff --git a/rtv/subreddit.py b/rtv/subreddit.py index fdebfce..a869a3b 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -177,10 +177,10 @@ class SubredditPage(BasePage): page = SubscriptionPage(self.stdscr, self.reddit) page.loop() - # When user has chosen a subreddit in the subscriptions list, refresh content with the selected subreddit - chosen_subreddit = page.get_selected_subreddit_data() - if chosen_subreddit is not None: - self.refresh_content(name=chosen_subreddit['name']) + # When user has chosen a subreddit in the subscriptions list, + # refresh content with the selected subreddit + if page.selected_subreddit_data is not None: + self.refresh_content(name=page.selected_subreddit_data['name']) @staticmethod def draw_item(win, data, inverted=False): diff --git a/rtv/subscriptions.py b/rtv/subscriptions.py index cbb5926..8ef2094 100644 --- a/rtv/subscriptions.py +++ b/rtv/subscriptions.py @@ -14,12 +14,14 @@ class SubscriptionController(BaseController): character_map = {} class SubscriptionPage(BasePage): + def __init__(self, stdscr, reddit): + self.controller = SubscriptionController(self) self.loader = LoadScreen(stdscr) self.selected_subreddit_data = None - content = SubscriptionContent.get_list(reddit, self.loader) + content = SubscriptionContent.from_user(reddit, self.loader) super(SubscriptionPage, self).__init__(stdscr, reddit, content) def loop(self): @@ -31,9 +33,6 @@ class SubscriptionPage(BasePage): cmd = self.stdscr.getch() self.controller.trigger(cmd) - def get_selected_subreddit_data(self): - return self.selected_subreddit_data - @SubscriptionController.register(curses.KEY_F5, 'r') def refresh_content(self): "Re-download all subscriptions and reset the page index" @@ -48,7 +47,7 @@ class SubscriptionPage(BasePage): self.selected_subreddit_data = self.content.get(self.nav.absolute_index) self.active = False - @SubscriptionController.register(curses.KEY_LEFT, 's') + @SubscriptionController.register(curses.KEY_LEFT, 'h', 's') def close_subscriptions(self): "Close subscriptions and return to the subreddit page" @@ -63,12 +62,12 @@ class SubscriptionPage(BasePage): valid_rows = range(0, n_rows) offset = 0 if not inverted else -(data['n_rows'] - n_rows) - n_title = len(data['split_title']) - for row, text in enumerate(data['split_title'], start=offset): - if row in valid_rows: - add_line(win, text, row, 1) - - row = n_title + offset + row = offset if row in valid_rows: attr = curses.A_BOLD | Color.YELLOW add_line(win, u'{name}'.format(**data), row, 1, attr) + + row = offset + 1 + for row, text in enumerate(data['split_title'], start=row): + if row in valid_rows: + add_line(win, text, row, 1) \ No newline at end of file