Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xavierog opened this issue Feb 15, 2025 · 1 comment

Comments

@xavierog
Copy link
Contributor

xavierog commented Feb 15, 2025

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 xavierog changed the title Linux driver: consider using /dev/tty instead of stdin (and possible stderr) Linux driver: consider using /dev/tty instead of stdin (and possibly stderr) Feb 15, 2025
Copy link

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

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

No branches or pull requests

1 participant