Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import print_function

import sys
import locale
import os.path
import subprocess
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import editor

cont = editor.edit(contents='ABC!',
use_tty='use_tty' in sys.argv)
sys.stdout.write(cont)