|
10 | 10 |
|
11 | 11 | from .base import DummyOutput, Output
|
12 | 12 | from .color_depth import ColorDepth
|
| 13 | +from .plain_text import PlainTextOutput |
13 | 14 |
|
14 | 15 | __all__ = [
|
15 | 16 | "create_output",
|
16 | 17 | ]
|
17 | 18 |
|
18 | 19 |
|
19 | 20 | def create_output(
|
20 |
| - stdout: Optional[TextIO] = None, always_prefer_tty: bool = True |
| 21 | + stdout: Optional[TextIO] = None, always_prefer_tty: bool = False |
21 | 22 | ) -> Output:
|
22 | 23 | """
|
23 | 24 | Return an :class:`~prompt_toolkit.output.Output` instance for the command
|
24 | 25 | line.
|
25 | 26 |
|
26 | 27 | :param stdout: The stdout object
|
27 | 28 | :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout`
|
28 |
| - is not a TTY. (The prompt_toolkit render output is not meant to be |
29 |
| - consumed by something other then a terminal, so this is a reasonable |
30 |
| - default.) |
| 29 | + is not a TTY. Useful if `sys.stdout` is redirected to a file, but we |
| 30 | + still want user input and output on the terminal. |
| 31 | +
|
| 32 | + By default, this is `False`. If `sys.stdout` is not a terminal (maybe |
| 33 | + it's redirected to a file), then a `PlainTextOutput` will be returned. |
| 34 | + That way, tools like `print_formatted_text` will write plain text into |
| 35 | + that file. |
31 | 36 | """
|
32 | 37 | # Consider TERM, PROMPT_TOOLKIT_BELL, and PROMPT_TOOLKIT_COLOR_DEPTH
|
33 | 38 | # environment variables. Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is
|
@@ -82,6 +87,12 @@ def create_output(
|
82 | 87 | else:
|
83 | 88 | from .vt100 import Vt100_Output
|
84 | 89 |
|
| 90 | + # Stdout is not a TTY? Render as plain text. |
| 91 | + # This is mostly useful if stdout is redirected to a file, and |
| 92 | + # `print_formatted_text` is used. |
| 93 | + if not stdout.isatty(): |
| 94 | + return PlainTextOutput(stdout) |
| 95 | + |
85 | 96 | return Vt100_Output.from_pty(
|
86 | 97 | stdout,
|
87 | 98 | term=term_from_env,
|
|
0 commit comments