Can browse through list of subscribed multireddits
This commit is contained in:
@@ -189,6 +189,20 @@ class Content(object):
|
||||
data['title'] = subscription.title
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_multireddit(multireddit):
|
||||
"""
|
||||
Parse through a multireddits and return a dict with data ready to be
|
||||
displayed through the terminal.
|
||||
"""
|
||||
|
||||
data = {}
|
||||
data['object'] = multireddit
|
||||
data['type'] = 'Multireddit'
|
||||
data['name'] = multireddit.path
|
||||
data['title'] = multireddit.description_md
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def humanize_timestamp(utc_timestamp, verbose=False):
|
||||
"""
|
||||
@@ -560,3 +574,52 @@ class SubscriptionContent(Content):
|
||||
data['offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class MultiredditContent(Content):
|
||||
|
||||
def __init__(self, multireddits, loader):
|
||||
|
||||
self.name = "Multireddits"
|
||||
self.order = None
|
||||
self._loader = loader
|
||||
self._multireddits = multireddits
|
||||
self._multireddit_data = []
|
||||
|
||||
try:
|
||||
self.get(0)
|
||||
except IndexError:
|
||||
raise exceptions.SubscriptionError('No multireddits')
|
||||
|
||||
@classmethod
|
||||
def from_user(cls, reddit, multireddits, loader):
|
||||
multireddits = (m for m in multireddits)
|
||||
return cls(multireddits, 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._multireddit_data):
|
||||
try:
|
||||
with self._loader('Loading multireddits'):
|
||||
multireddit = next(self._multireddits)
|
||||
if self._loader.exception:
|
||||
raise IndexError
|
||||
except StopIteration:
|
||||
raise IndexError
|
||||
else:
|
||||
data = self.strip_praw_multireddit(multireddit)
|
||||
self._multireddit_data.append(data)
|
||||
|
||||
data = self._multireddit_data[index]
|
||||
data['split_title'] = self.wrap_text(data['title'], width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 1
|
||||
data['offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
@@ -120,6 +120,7 @@ SUBREDDIT_POST = c
|
||||
SUBREDDIT_OPEN = l, <KEY_RIGHT>
|
||||
SUBREDDIT_OPEN_IN_BROWSER = o, <LF>, <KEY_ENTER>, <KEY_ENTER>
|
||||
SUBREDDIT_OPEN_SUBSCRIPTIONS = s
|
||||
MULTIREDDIT_OPEN_SUBSCRIPTIONS = S
|
||||
|
||||
; Subscription page
|
||||
SUBSCRIPTION_SELECT = l, <LF>, <KEY_ENTER>, <KEY_RIGHT>
|
||||
|
||||
@@ -9,6 +9,7 @@ from .content import SubredditContent
|
||||
from .page import Page, PageController, logged_in
|
||||
from .objects import Navigator, Color, Command
|
||||
from .submission import SubmissionPage
|
||||
from .multireddits import MultiredditPage
|
||||
from .subscription import SubscriptionPage
|
||||
from .exceptions import TemporaryFileError
|
||||
|
||||
@@ -168,6 +169,27 @@ class SubredditPage(Page):
|
||||
self.refresh_content(order='ignore',
|
||||
name=page.subreddit_data['name'])
|
||||
|
||||
@SubredditController.register(Command('MULTIREDDIT_OPEN_SUBSCRIPTIONS'))
|
||||
@logged_in
|
||||
def open_multireddit_subscriptions(self):
|
||||
"Open user multireddit subscriptions page"
|
||||
|
||||
with self.term.loader('Loading multireddits'):
|
||||
page = MultiredditPage(
|
||||
self.reddit, self.reddit.get_my_multireddits(),
|
||||
self.term, self.config)
|
||||
if self.term.loader.exception:
|
||||
return
|
||||
|
||||
page.loop()
|
||||
|
||||
# When the user has chosen a subreddit in the subscriptions list,
|
||||
# refresh content with the selected subreddit
|
||||
if page.multireddit_data is not None:
|
||||
self.refresh_content(order='ignore',
|
||||
name=page.multireddit_data['name'])
|
||||
|
||||
|
||||
def _draw_item(self, win, data, inverted):
|
||||
|
||||
n_rows, n_cols = win.getmaxyx()
|
||||
|
||||
Reference in New Issue
Block a user