From e34758bb2abacba4c6269e02c12d9514bd320f6e Mon Sep 17 00:00:00 2001 From: Eeo Jun Date: Thu, 31 Mar 2016 11:23:02 +0800 Subject: [PATCH 1/5] Use /dev/tty by default This is because vim (and other terminal editors) typically output to stdout. This is not very nice behaviour especially if the program using the editor is piped to some other program. Hence, using tty is saner. --- editor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/editor.py b/editor.py index ac80576..021e0ee 100755 --- a/editor.py +++ b/editor.py @@ -71,7 +71,7 @@ def get_editor(): "Please consider setting your %s variable" % get_platform_editor_var()) -def edit(filename=None, contents=None): +def edit(filename=None, contents=None, use_tty=True): editor = get_editor() args = get_editor_args(os.path.basename(os.path.realpath(editor))) args = [editor] + args.split(' ') @@ -85,8 +85,9 @@ def edit(filename=None, contents=None): f.write(contents) args += [filename] + extra = {'stdout': open('/dev/tty', 'wb')} if use_tty else {} - proc = subprocess.Popen(args, close_fds=True) + proc = subprocess.Popen(args, close_fds=True, **extra) proc.communicate() with open(filename, mode='rb') as f: From 8ea8b41d94ba9e69a4c8e22a3bb5a765e73efa58 Mon Sep 17 00:00:00 2001 From: Eeo Jun Date: Thu, 31 Mar 2016 15:36:46 +0800 Subject: [PATCH 2/5] Use branch instead of ternary operator --- editor.py | 7 +++++-- test.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test.py diff --git a/editor.py b/editor.py index 021e0ee..c808456 100755 --- a/editor.py +++ b/editor.py @@ -85,9 +85,12 @@ def edit(filename=None, contents=None, use_tty=True): f.write(contents) args += [filename] - extra = {'stdout': open('/dev/tty', 'wb')} if use_tty else {} - proc = subprocess.Popen(args, close_fds=True, **extra) + stdout = None + if use_tty: + stdout = open('/dev/tty', 'wb') + + proc = subprocess.Popen(args, close_fds=True, stdout=stdout) proc.communicate() with open(filename, mode='rb') as f: diff --git a/test.py b/test.py new file mode 100644 index 0000000..58b6713 --- /dev/null +++ b/test.py @@ -0,0 +1,6 @@ +import sys +import editor + +cont = editor.edit(contents='ABC!', + use_tty='use_tty' in sys.argv) +sys.stdout.write(cont) \ No newline at end of file From 0956e21cbaa0a116a419008d83ff15d10bd63990 Mon Sep 17 00:00:00 2001 From: Eeo Jun Date: Thu, 31 Mar 2016 15:37:22 +0800 Subject: [PATCH 3/5] Default to not using tty --- editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor.py b/editor.py index c808456..c69cc16 100755 --- a/editor.py +++ b/editor.py @@ -71,7 +71,7 @@ def get_editor(): "Please consider setting your %s variable" % get_platform_editor_var()) -def edit(filename=None, contents=None, use_tty=True): +def edit(filename=None, contents=None, use_tty=False): editor = get_editor() args = get_editor_args(os.path.basename(os.path.realpath(editor))) args = [editor] + args.split(' ') From 05045bb016af696237bb34d1c89cd4e1fe219041 Mon Sep 17 00:00:00 2001 From: Eeo Jun Date: Fri, 1 Apr 2016 11:47:28 +0800 Subject: [PATCH 4/5] Detect tty and add helper function --- editor.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/editor.py b/editor.py index c69cc16..422e106 100755 --- a/editor.py +++ b/editor.py @@ -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, use_tty=False): +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 @@ -88,7 +98,7 @@ def edit(filename=None, contents=None, use_tty=False): stdout = None if use_tty: - stdout = open('/dev/tty', 'wb') + stdout = open(get_tty_filename(), 'wb') proc = subprocess.Popen(args, close_fds=True, stdout=stdout) proc.communicate() From 9cb458ff5bc279470910f4f56c3ae121c168edfd Mon Sep 17 00:00:00 2001 From: Eeo Jun Date: Fri, 1 Apr 2016 11:49:37 +0800 Subject: [PATCH 5/5] Added newline to end of test.py --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index 58b6713..73b7cea 100644 --- a/test.py +++ b/test.py @@ -3,4 +3,4 @@ cont = editor.edit(contents='ABC!', use_tty='use_tty' in sys.argv) -sys.stdout.write(cont) \ No newline at end of file +sys.stdout.write(cont)