From 9ddce7fd35361082e66c38f8347ba14e9ea05ad6 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Fri, 27 Mar 2015 23:21:26 -0700 Subject: [PATCH] Moved editor to a helper function. --- rtv/helpers.py | 14 ++++++ rtv/submission.py | 115 +++++++++++++++++++++------------------------- 2 files changed, 66 insertions(+), 63 deletions(-) diff --git a/rtv/helpers.py b/rtv/helpers.py index 5be4fc5..550ce0c 100644 --- a/rtv/helpers.py +++ b/rtv/helpers.py @@ -9,6 +9,20 @@ from . import config __all__ = ['open_browser', 'clean', 'wrap_text', 'strip_textpad', 'strip_subreddit_url', 'humanize_timestamp'] +def open_editor(filename): + """ + Open the given file using the system's default editor. + """ + + editors = [os.getenv('RTV_EDITOR'), os.getenv('EDITOR'), 'nano', 'vi'] + for program in editors: + if program: + try: + subprocess.Popen([program, filename]).wait() + break + except OSError: + pass + def open_browser(url): """ Call webbrowser.open_new_tab(url) and redirect stdout/stderr to devnull. diff --git a/rtv/submission.py b/rtv/submission.py index b31e136..0a9c4ec 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -1,15 +1,13 @@ import curses import sys import time -import os from tempfile import NamedTemporaryFile -from subprocess import Popen import praw.errors from .content import SubmissionContent from .page import BasePage -from .helpers import clean, open_browser +from .helpers import clean, open_browser, open_editor from .curses_helpers import (BULLET, UARROW, DARROW, Color, LoadScreen, show_help, show_notification, text_input) from .docs import COMMENT_FILE @@ -108,6 +106,56 @@ class SubmissionPage(BasePage): url = self.content.get(-1)['permalink'] open_browser(url) + def add_comment(self): + """ + Add a comment on the submission if a header is selected. + Reply to a comment if the comment is selected. + """ + if not self.reddit.is_logged_in(): + show_notification(self.stdscr, ["Login to reply"]) + return + + data = self.content.get(self.nav.absolute_index) + if data['type'] == 'Submission': + content = data['text'] + elif data['type'] == 'Comment': + content = data['body'] + else: + curses.flash() + return + + # Comment out every line of the content + content = '\n'.join(['#|' + line for line in content.split('\n')]) + info = {'author': data['author'], + 'type': data['type'], + 'content': content} + comment_info = COMMENT_FILE.format(**info) + + curses.endwin() + with NamedTemporaryFile(prefix='rtv-comment-', mode='w+') as fp: + fp.write(comment_info) + fp.flush() + open_editor(fp.name) # Open the default text editor and block + fp.seek(0) + comment_text = '\n'.join(line for line in fp.read().split('\n') + if not line.startswith("#")) + curses.doupdate() + + if comment_text is None or comment_text.isspace(): + curses.flash() + return + + try: + if data['type'] == 'Submission': + data['object'].add_comment(comment_text) + else: + data['object'].reply(comment_text) + except praw.errors.APIException as e: + show_notification(self.stdscr, [e.message]) + else: + time.sleep(0.5) + self.refresh_content() + def draw_item(self, win, data, inverted=False): if data['type'] == 'MoreComments': @@ -231,63 +279,4 @@ class SubmissionPage(BasePage): text = clean('{score} {comments}'.format(**data)) win.addnstr(row, 1, text, n_cols, curses.A_BOLD) - win.border() - - def add_comment(self): - """ - Add a comment on the submission if a header is selected. - Reply to a comment if the comment is selected. - """ - if not self.reddit.is_logged_in(): - show_notification(self.stdscr, ["Login to reply"]) - return - - data = self.content.get(self.nav.absolute_index) - if data['type'] not in ('Comment', 'Submission'): - curses.flash() - return - - if data['type'] is 'Submission': - content = data['text'] - else: - content = data['body'] - - # Comment out every line of the content - content = '\n'.join(['#|' + line for line in content.split('\n')]) - - info = {'author': data['author'], - 'type': data['type'], - 'content': content} - - comment_info = COMMENT_FILE.format(**info) - - with NamedTemporaryFile(prefix='rtv-comment-', mode='w+') as fp: - fp.write(comment_info) - fp.flush() - - editor = os.getenv('RTVEDITOR') or os.getenv('EDITOR') - if editor is None: - show_notification(self.stdscr, ['No EDITOR defined']) - return - - curses.endwin() - Popen([editor, fp.name]).wait() - curses.doupdate() - - fp.seek(0) - comment_text = '\n'.join([line for line in fp.read().split('\n') - if not line.startswith("#")]) - - if comment_text is None or comment_text.isspace(): - return - - try: - if data['type'] == 'Submission': - data['object'].add_comment(comment_text) - else: - data['object'].reply(comment_text) - except praw.errors.APIException as e: - show_notification(self.stdscr, [e.message]) - else: - time.sleep(0.5) - self.refresh_content() + win.border() \ No newline at end of file