Fix for gedit.
This commit is contained in:
@@ -3,25 +3,36 @@ import os
|
|||||||
import textwrap
|
import textwrap
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
from . import config
|
from . import config
|
||||||
|
|
||||||
__all__ = ['open_browser', 'clean', 'wrap_text', 'strip_textpad',
|
__all__ = ['open_browser', 'clean', 'wrap_text', 'strip_textpad',
|
||||||
'strip_subreddit_url', 'humanize_timestamp']
|
'strip_subreddit_url', 'humanize_timestamp', 'open_editor']
|
||||||
|
|
||||||
def open_editor(filename):
|
def open_editor(data=''):
|
||||||
"""
|
"""
|
||||||
Open the given file using the system's default editor.
|
Open a temporary file using the system's default editor.
|
||||||
|
|
||||||
|
The data string will be written to the file before opening. This function
|
||||||
|
will block until the editor has closed. At that point the file will be
|
||||||
|
read and and lines starting with '#' will be stripped.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
editors = [os.getenv('RTV_EDITOR'), os.getenv('EDITOR'), 'nano', 'vi']
|
with NamedTemporaryFile(prefix='rtv-', suffix='.txt', mode='w') as fp:
|
||||||
for program in editors:
|
fp.write(data)
|
||||||
if program:
|
fp.flush()
|
||||||
try:
|
editor = os.getenv('RTV_EDITOR') or os.getenv('EDITOR') or 'nano'
|
||||||
subprocess.Popen([program, filename]).wait()
|
subprocess.Popen([editor, fp.name]).wait()
|
||||||
break
|
|
||||||
except OSError:
|
# Open a second file object to read. This appears to be necessary in
|
||||||
pass
|
# order to read the changes made by some editors (gedit). w+ mode does
|
||||||
|
# not work!
|
||||||
|
with open(fp.name) as fp2:
|
||||||
|
text = ''.join(line for line in fp2 if not line.startswith('#'))
|
||||||
|
text = text.rstrip()
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
def open_browser(url):
|
def open_browser(url):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import curses
|
import curses
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from tempfile import NamedTemporaryFile
|
|
||||||
|
|
||||||
import praw.errors
|
import praw.errors
|
||||||
|
|
||||||
@@ -126,26 +125,18 @@ class SubmissionPage(BasePage):
|
|||||||
|
|
||||||
# Comment out every line of the content
|
# Comment out every line of the content
|
||||||
content = '\n'.join(['# |' + line for line in content.split('\n')])
|
content = '\n'.join(['# |' + line for line in content.split('\n')])
|
||||||
info = {'author': data['author'],
|
comment_info = COMMENT_FILE.format(
|
||||||
'type': data['type'],
|
author=data['author'],
|
||||||
'content': content}
|
type=data['type'].lower(),
|
||||||
comment_info = COMMENT_FILE.format(**info)
|
content=content)
|
||||||
|
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
with NamedTemporaryFile(prefix='rtv-comment-', mode='w+') as fp:
|
comment_text = open_editor(comment_info)
|
||||||
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("#"))
|
|
||||||
comment_text = comment_text.rstrip()
|
|
||||||
curses.doupdate()
|
curses.doupdate()
|
||||||
|
|
||||||
if not comment_text:
|
if not comment_text:
|
||||||
curses.flash()
|
curses.flash()
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if data['type'] == 'Submission':
|
if data['type'] == 'Submission':
|
||||||
data['object'].add_comment(comment_text)
|
data['object'].add_comment(comment_text)
|
||||||
|
|||||||
Reference in New Issue
Block a user