diff --git a/rtv/docs.py b/rtv/docs.py index 0796575..0cf48db 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -57,6 +57,13 @@ COMMENT_FILE = """ {content} """ +COMMENT_EDIT_FILE = """{content} +# Please enter a comment. Lines starting with '#' will be ignored, +# and an empty message aborts the comment. +# +# Editing your comment +""" + SUBMISSION_FILE = """ # Please enter your submission. Lines starting with '#' will be ignored, # and an empty field aborts the submission. diff --git a/rtv/page.py b/rtv/page.py index fa9a412..8d8f775 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -1,12 +1,13 @@ import curses +import time import six import sys import praw.errors -from .helpers import clean +from .helpers import clean, open_editor from .curses_helpers import Color, show_notification, show_help, text_input -from .docs import AGENT +from .docs import AGENT, COMMENT_EDIT_FILE, SUBMISSION_FILE __all__ = ['Navigator'] @@ -276,6 +277,45 @@ class BasePage(object): time.sleep(0.5) self.refresh_content() + @BaseController.register('e') + def edit(self): + """ + Edit a submission or comment. + """ + data = self.content.get(self.nav.absolute_index) + if data['author'] != self.reddit.user.name: + show_notification(self.stdscr, ['You can\'t edit this']) + return + + if data['type'] == 'Submission': + subreddit = self.reddit.get_subreddit(self.content.name) + sub = str(subreddit).split('/')[2] + sub_file = '{content}' + SUBMISSION_FILE + content = data['text'] + info = sub_file.format(content=content, name=sub) + + elif data['type'] == 'Comment': + content = data['body'] + info = COMMENT_EDIT_FILE.format(content=content) + else: + curses.flash() + return + + curses.endwin() + text = open_editor(info) + curses.doupdate() + + if text == content: + return + + try: + data['object'].edit(text) + except praw.errors.APIException as e: + show_notification(self.stdscr, [e.message]) + else: + time.sleep(0.5) + self.refresh_content() + def logout(self): "Prompt to log out of the user's account."