Monday, April 20, 2020

Re: Meta: reading file from stdin while still interacting with the terminal

On 19Apr2020 22:06, Tim Chase <vim@tim.thechases.com> wrote:
>I'm playing around with a curses program and would like it to behave
>similarly to how vim lets me do
>
> $ echo hello | vim -
>
>where vim reads data from stdin but then interacts with the terminal
>directrly. What magic is vim doing here?

Typically stdin, stdout and stderr are all os.dup()s of the terminal.

In the above vim's stdin will of course be the pipe but stdout and
stderr are still attached; it could dup one of them to stdin again and
proceed.

Or it might just open /dev/tty again after reading stdin if that isn't a
tty.

>For context, it's a dumb Python curses program, but if I try to pipe
>data into it, it interprets the EOF on stdin and then complains
>there's no input for the curses getch()/getkey() call.

Aye. The pipe is empty. Reach for the tty.

You _could_ close sys.stdin and reopen, but you might be better off just
using os.close(0) and then duping fd 1 to fd 0. I'd expect getch and
getkey to go direct to the file descriptor.

Cheers,
Cameron Simpson <cs@cskk.id.au>

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200420222039.GA46939%40cskk.homeip.net.

No comments:

Post a Comment