Skip to content

Conversation

eugene-eeo
Copy link
Contributor

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. Example:

# test.py
import sys
import editor

cont = editor.edit(contents='ABC!',
                   use_tty='use_tty' in sys.argv)
sys.stdout.write(cont)
$ python test.py > t.xt
Vim: Warning: Output is not to a terminal
$ python test.py use_tty > t.txt
# happily edit in vim
$ cat t.txt
ABC!

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.
@fmoo
Copy link
Owner

fmoo commented Mar 31, 2016

Some initial feedback:

  • can you test with gedit and gvim as your editor? I've been taking special care to make sure that continues to work.
  • I'd like to avoid picking up the dependency on /dev/ as it's not very portable. Is there a more platform independent way of solving this?
  • I'm not yet convinced this should be the default. What do you think about comparing isatty on sys.stdin and sys.stdout and being a little more discriminatory as to when we enable the feature?

@fmoo
Copy link
Owner

fmoo commented Mar 31, 2016

Also, style wise, I prefer assignment and a light branch to ternary operators and most extra logic to be split into helper functions.

Also prefer explicit kwargs to **extra. E.g., explicitly pass stdout=... and initialize to sys.stdout I guess?

@eugene-eeo
Copy link
Contributor Author

Tested on MacVim ('mvim') and Sublime Text as they are the only GUI editors I have on OSX, it doesn't work for both cases. The string gets returned immediately.

On /dev/tty - unfortunately such a thing doesn't exist on windows. However one can always run cygwin, and given that it is used in many other tools (selecta comes to mind), I expect it to be pretty much universal for modern distros. From Unix Stackexchange:

/dev/tty works as long as the process has a controlling terminal

We can check if std{out,in,err} are ttys before we enable the feature as well, if you want.

Update: What do you mean by light branch to ternary operators? Ok, I see what you mean.

Update: Even git seems to have trouble with GUI $EDITORs.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the blank newline at the end of file

@fmoo
Copy link
Owner

fmoo commented Mar 31, 2016

unfortunately such a thing doesn't exist on windows.

I think it does, via the special file "CON:". I'll have to test when I'm on windows later today.

The default should be use_tty=None. Then:

if use_tty is None:
    use_tty = sys.stdin.isatty() and not sys.stdout.isatty()

Add a helper function to get the tty filename:

def get_tty_filename():
    if sys.platform == 'win32':
        return 'CON:'
    return '/dev/tty'

Which is exactly why the current

@eugene-eeo
Copy link
Contributor Author

👍 Check out the new commits

@fmoo fmoo merged commit 50b1886 into fmoo:master Apr 1, 2016
@fmoo
Copy link
Owner

fmoo commented Apr 1, 2016

Pushed to pypi as 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants