Merge pull request #8 from eugene-eeo/master

Use /dev/tty by default when stdout is not a tty, but stdin is
This commit is contained in:
Peter Ruibal 2016-04-01 08:10:42 -07:00
commit 50b188636b
2 changed files with 22 additions and 2 deletions

View File

@ -3,6 +3,7 @@
from __future__ import print_function
import sys
import locale
import os.path
import subprocess
@ -71,11 +72,20 @@ def get_editor():
"Please consider setting your %s variable" % get_platform_editor_var())
def edit(filename=None, contents=None):
def get_tty_filename():
if sys.platform == 'win32':
return 'CON:'
return '/dev/tty'
def edit(filename=None, contents=None, use_tty=None):
editor = get_editor()
args = get_editor_args(os.path.basename(os.path.realpath(editor)))
args = [editor] + args.split(' ')
if use_tty is None:
use_tty = sys.stdin.isatty() and not sys.stdout.isatty()
if filename is None:
tmp = tempfile.NamedTemporaryFile()
filename = tmp.name
@ -86,7 +96,11 @@ def edit(filename=None, contents=None):
args += [filename]
proc = subprocess.Popen(args, close_fds=True)
stdout = None
if use_tty:
stdout = open(get_tty_filename(), 'wb')
proc = subprocess.Popen(args, close_fds=True, stdout=stdout)
proc.communicate()
with open(filename, mode='rb') as f:

6
test.py Normal file
View File

@ -0,0 +1,6 @@
import sys
import editor
cont = editor.edit(contents='ABC!',
use_tty='use_tty' in sys.argv)
sys.stdout.write(cont)