Moved editor to a helper function.

This commit is contained in:
Michael Lazar
2015-03-27 23:21:26 -07:00
parent 32fd689544
commit 9ddce7fd35
2 changed files with 66 additions and 63 deletions

View File

@@ -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.

View File

@@ -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()