8000 Use stdout as input device if stdin is not a tty, but stdout is. · mxr/python-prompt-toolkit@188a705 · GitHub
[go: up one dir, main page]

Skip to content

Commit 188a705

Browse files
Use stdout as input device if stdin is not a tty, but stdout is.
1 parent 21eeda9 commit 188a705

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

prompt_toolkit/input/defaults.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,31 @@
1212

1313

1414
def create_input(stdin: Optional[TextIO] = None) -> Input:
15-
stdin = stdin or sys.stdin
16-
15+
"""
16+
Create the appropriate `Input` object for the current os/environment.
17+
"""
1718
if is_windows():
1819
from .win32 import Win32Input
1920

20-
return Win32Input(stdin)
21+
return Win32Input(stdin or sys.stdin)
2122
else:
2223
from .vt100 import Vt100Input
2324

25+
# If no input TextIO is given, use stdin/stdout.
26+
if stdin is None:
27+
# Try stdin first, if it's a TTY.
28+
if sys.stdin.isatty():
29+
stdin = sys.stdin
30+
# If stdin is not a TTY, it's possible we're piping something into
31+
# stdin. Use stdout instead if stdout is a TTY. (We can actually
32+
# use stdout to read input from, this is how a $PAGER works.)
33+
elif sys.stdout.isatty():
34+
stdin = sys.stdout
35+
# If stdout is also not a tty, then use stdin. (This will print a
36+
# "Input is not a terminal" warning in `Vt100Input`.)
37+
else:
38+
stdin = sys.stdin
39+
2440
return Vt100Input(stdin)
2541

2642

0 commit comments

Comments
 (0)
0