Skip to content

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

Open
@xavierog

Description

@xavierog

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
$

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions