Skip to content

Commit 50b1886

Browse files
committed
Merge pull request fmoo#8 from eugene-eeo/master
Use /dev/tty by default when stdout is not a tty, but stdin is
2 parents d58b00f + 9cb458f commit 50b1886

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

editor.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import print_function
55

6+
import sys
67
import locale
78
import os.path
89
import subprocess
@@ -71,11 +72,20 @@ def get_editor():
7172
"Please consider setting your %s variable" % get_platform_editor_var())
7273

7374

74-
def edit(filename=None, contents=None):
75+
def get_tty_filename():
76+
if sys.platform == 'win32':
77+
return 'CON:'
78+
return '/dev/tty'
79+
80+
81+
def edit(filename=None, contents=None, use_tty=None):
7582
editor = get_editor()
7683
args = get_editor_args(os.path.basename(os.path.realpath(editor)))
7784
args = [editor] + args.split(' ')
7885

86+
if use_tty is None:
87+
use_tty = sys.stdin.isatty() and not sys.stdout.isatty()
88+
7989
if filename is None:
8090
tmp = tempfile.NamedTemporaryFile()
8191
filename = tmp.name
@@ -86,7 +96,11 @@ def edit(filename=None, contents=None):
8696

8797
args += [filename]
8898

89-
proc = subprocess.Popen(args, close_fds=True)
99+
stdout = None
100+
if use_tty:
101+
stdout = open(get_tty_filename(), 'wb')
102+
103+
proc = subprocess.Popen(args, close_fds=True, stdout=stdout)
90104
proc.communicate()
91105

92106
with open(filename, mode='rb') as f:

test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
import editor
3+
4+
cont = editor.edit(contents='ABC!',
5+
use_tty='use_tty' in sys.argv)
6+
sys.stdout.write(cont)

0 commit comments

Comments
 (0)