From 08c78c54ecbc16342ac7e960ec88115dd4bbe792 Mon Sep 17 00:00:00 2001 From: noah morrison Date: Mon, 23 Mar 2015 18:59:08 -0400 Subject: [PATCH] Add instructions to the comment file. --- rtv/docs.py | 14 ++++++++++++++ rtv/submission.py | 27 ++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index 22f2e37..6497ad9 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -43,3 +43,17 @@ Submission Mode `LEFT` or `h` : Return to subreddit mode `RIGHT` or `l` : Fold the selected comment, or load additional comments """ + +COMMENT_FILE = """ +--% {0} %-- +Do not edit the line above. +Type your comment above the line. +All text below the line will be discarded. + +If there is nothing but whitespace above the line +then the comment will be discarded. + +Replying to {1}'s {2} + +{3} +""" diff --git a/rtv/submission.py b/rtv/submission.py index b497586..e1c8a5e 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -3,6 +3,7 @@ import sys import time import tempfile import os +import uuid import praw.errors @@ -11,6 +12,7 @@ from .page import BasePage from .helpers import clean, open_browser from .curses_helpers import (BULLET, UARROW, DARROW, Color, LoadScreen, show_help, show_notification, text_input) +from .docs import COMMENT_FILE __all__ = ['SubmissionPage'] @@ -247,16 +249,27 @@ class SubmissionPage(BasePage): curses.endwin() - fd, filename = tempfile.mkstemp(prefix="rtv_comment-"); - os.system('%s %s' % (os.getenv('EDITOR'), filename)) + fd, filename = tempfile.mkstemp(prefix="rtv-comment-", suffix=".txt") + cid = str(uuid.uuid4()) + try: + with open(filename, 'w') as comment_file: + if data['type'] == 'Submission': + info = (data['author'], 'submission', data['text']) + else: + info = (data['author'], 'comment', data['body']) - with os.fdopen(fd, 'r') as comment_file: - comment_text = comment_file.read() + comment_info = COMMENT_FILE.format(cid, *info) + comment_file.write(comment_info) - curses.doupdate() + os.system('%s %s' % (os.getenv('EDITOR'), filename)) + with open(filename, 'r') as comment_file: + comment_text = (comment_file.read() + .split('--% ' + cid + ' %--'))[0] + finally: + os.remove(filename) + curses.doupdate() - - if comment_text is None: + if comment_text is None or comment_text.isspace(): return try: