textual icon indicating copy to clipboard operation
textual copied to clipboard

Linux driver: consider using /dev/tty instead of stdin (and possibly stderr)

Open xavierog opened this issue 11 months ago • 1 comments

In the beginning, the Linux driver used to write to stdout; this was changed to stderr to enable piping between a Textual producer and an external consumer. As these lines are being written, the Linux driver still relies on stdin to read terminal events, making it difficult to enable piping between an arbitrary producer and a Textual consumer. This was still achieved in e.g. toolong's cli.py by combining /dev/tty and various tricks (temporary file, fork and reexec).

It should be possible to make these tricks unnecessary by having the Linux driver systematically use /dev/tty for both input and output (with possible failover to stdin and stderr if /dev/tty is somehow unavailable, or if a dedicated environment variable says so). That would leave all three standard file descriptors (stdin, stdout, stderr) available to developers and end users for regular use, e.g. mytextualapp < mydata > myresults 2> myerrors while still being able to read events from the controlling terminal and draw widgets to it.

This is best demonstrated by a Python snippet interacting with the terminal despite having all three standard file descriptors pointing to /dev/null: test.py:

from os import open, read, write, O_RDWR
t = open("/dev/tty", O_RDWR)
write(t, b"Tell me something: ")
v = read(t, 512)
write(t, b"You wrote " + v)
$ python3 test.py < /dev/null 1> /dev/null 2> /dev/null
Tell me something: hello
You wrote hello
$

xavierog avatar Feb 15 '25 13:02 xavierog

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Feb 15 '25 13:02 github-actions[bot]