Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Selftext preview #605

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions rtv/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def strip_praw_comment(cls, comment):

data['type'] = 'SavedComment'
data['level'] = None
data['title'] = '[Comment] {0}'.format(comment.body)
data['title'] = '[Comment] {0}'.format(comment.link_title)
data['body'] = comment.body
data['comments'] = None
data['url_full'] = comment._fast_permalink
data['url'] = comment._fast_permalink
Expand Down Expand Up @@ -216,7 +217,7 @@ def strip_praw_submission(cls, sub):
data['object'] = sub
data['type'] = 'Submission'
data['title'] = sub.title
data['text'] = sub.selftext
data['body'] = sub.selftext
data['created'] = cls.humanize_timestamp(sub.created_utc)
data['created_long'] = cls.humanize_timestamp(sub.created_utc, True)
data['comments'] = '{0} comments'.format(sub.num_comments)
Expand Down Expand Up @@ -367,7 +368,7 @@ def get(self, index, n_cols=70):
elif index == -1:
data = self._submission_data
data['split_title'] = self.wrap_text(data['title'], width=n_cols-2)
data['split_text'] = self.wrap_text(data['text'], width=n_cols-2)
data['split_text'] = self.wrap_text(data['body'], width=n_cols-2)
data['n_rows'] = len(data['split_title'] + data['split_text']) + 5
data['h_offset'] = 0

Expand Down Expand Up @@ -443,12 +444,12 @@ class SubredditContent(Content):
"""

def __init__(self, name, submissions, loader, order=None,
max_title_rows=4, query=None, filter_nsfw=False):
selftext_preview=True, query=None, filter_nsfw=False):

self.name = name
self.order = order
self.query = query
self.max_title_rows = max_title_rows
self.selftext_preview = selftext_preview
self.filter_nsfw = filter_nsfw
self._loader = loader
self._submissions = submissions
Expand All @@ -467,7 +468,8 @@ def __init__(self, name, submissions, loader, order=None,
raise exceptions.NoSubmissionsError(full_name)

@classmethod
def from_name(cls, reddit, name, loader, order=None, query=None):
def from_name(cls, reddit, name, loader, order=None, query=None,
selftext_preview=False):
"""
Params:
reddit (praw.Reddit): Instance of the reddit api.
Expand Down Expand Up @@ -643,8 +645,10 @@ def from_name(cls, reddit, name, loader, order=None, query=None):
filter_nsfw = (reddit.user and reddit.user.over_18 is False)

# We made it!
selftext_preview = True if resource_root == 'u' else selftext_preview
return cls(display_name, submissions, loader, order=display_order,
query=query, filter_nsfw=filter_nsfw)
query=query, filter_nsfw=filter_nsfw,
selftext_preview=selftext_preview)

@property
def range(self):
Expand Down Expand Up @@ -701,10 +705,12 @@ def get(self, index, n_cols=70):
# Modifies the original dict, faster than copying
data = self._submission_data[index]
data['split_title'] = self.wrap_text(data['title'], width=n_cols)
if len(data['split_title']) > self.max_title_rows:
data['split_title'] = data['split_title'][:self.max_title_rows-1]
data['split_title'].append('(Not enough space to display)')
data['n_rows'] = len(data['split_title']) + 3
if self.selftext_preview:
data['split_text'] = self.wrap_text(data['body'], width=n_cols)
data['split_text'] = [_ for _ in data['split_text'] if _]
else:
data['split_text'] = []
data['n_rows'] = len(data['split_title'] + data['split_text']) + 3
data['h_offset'] = 0

return data
Expand Down
3 changes: 1 addition & 2 deletions rtv/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,11 @@ def edit(self):
self.term.flash()
return

content = data.get('body')
if data['type'] == 'Submission':
content = data['text']
info = docs.SUBMISSION_EDIT_FILE.format(
content=content, id=data['object'].id)
elif data['type'] == 'Comment':
content = data['body']
info = docs.COMMENT_EDIT_FILE.format(
content=content, id=data['object'].id)
else:
Expand Down
9 changes: 3 additions & 6 deletions rtv/submission_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ def open_pager(self):
n_rows, n_cols = self.term.stdscr.getmaxyx()

data = self.get_selected_item()
if data['type'] == 'Submission':
text = '\n\n'.join((data['permalink'], data['text']))
self.term.open_pager(text, wrap=n_cols)
elif data['type'] == 'Comment':
if data['type'] in ['Submission', 'Comment']:
text = '\n\n'.join((data['permalink'], data['body']))
self.term.open_pager(text, wrap=n_cols)
else:
Expand All @@ -181,7 +178,7 @@ def add_comment(self):

data = self.get_selected_item()
if data['type'] == 'Submission':
body = data['text']
body = data['body']
reply = data['object'].add_comment
elif data['type'] == 'Comment':
body = data['body']
Expand Down Expand Up @@ -233,7 +230,7 @@ def comment_urlview(self):
"""

data = self.get_selected_item()
comment = data.get('body') or data.get('text') or data.get('url_full')
comment = data.get('body') or data.get('url_full')
if comment:
self.term.open_urlview(comment)
else:
Expand Down
10 changes: 8 additions & 2 deletions rtv/subreddit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,13 @@ def _draw_item(self, win, data, inverted):
if row in valid_rows:
self.term.add_line(win, '{url}'.format(**data), row, 1, attr)

row = n_title + offset + 1
n_text = len(data['split_text'])
attr = self.term.attr('SubmissionText')
for row, text in enumerate(data['split_text'], start=n_title+offset+1):
if row in valid_rows:
self.term.add_line(win, text, row, 1, attr)

row = n_title + n_text + offset + 1
if row in valid_rows:

attr = self.term.attr('Score')
Expand Down Expand Up @@ -389,7 +395,7 @@ def _draw_item(self, win, data, inverted):
self.term.add_space(win)
self.term.add_line(win, 'NSFW', attr=attr)

row = n_title + offset + 2
row = n_title + n_text + offset + 2
if row in valid_rows:
attr = self.term.attr('SubmissionAuthor')
self.term.add_line(win, '{author}'.format(**data), row, 1, attr)
Expand Down
3 changes: 3 additions & 0 deletions rtv/templates/rtv.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ hide_username = False
; been installed into either the custom of default theme paths.
;theme = molokai

; Display body of self post in subreddit listings
selftext_preview = True

################
# OAuth Settings
################
Expand Down