Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/output/defaults.py: 24%
37 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1from __future__ import annotations
3import sys
4from typing import TextIO, cast
6from prompt_toolkit.utils import (
7 get_bell_environment_variable,
8 get_term_environment_variable,
9 is_conemu_ansi,
10)
12from .base import DummyOutput, Output
13from .color_depth import ColorDepth
14from .plain_text import PlainTextOutput
16__all__ = [
17 "create_output",
18]
21def create_output(
22 stdout: TextIO | None = None, always_prefer_tty: bool = False
23) -> Output:
24 """
25 Return an :class:`~prompt_toolkit.output.Output` instance for the command
26 line.
28 :param stdout: The stdout object
29 :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout`
30 is not a TTY. Useful if `sys.stdout` is redirected to a file, but we
31 still want user input and output on the terminal.
33 By default, this is `False`. If `sys.stdout` is not a terminal (maybe
34 it's redirected to a file), then a `PlainTextOutput` will be returned.
35 That way, tools like `print_formatted_text` will write plain text into
36 that file.
37 """
38 # Consider TERM, PROMPT_TOOLKIT_BELL, and PROMPT_TOOLKIT_COLOR_DEPTH
39 # environment variables. Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is
40 # the default that's used if the Application doesn't override it.
41 term_from_env = get_term_environment_variable()
42 bell_from_env = get_bell_environment_variable()
43 color_depth_from_env = ColorDepth.from_env()
45 if stdout is None:
46 # By default, render to stdout. If the output is piped somewhere else,
47 # render to stderr.
48 stdout = sys.stdout
50 if always_prefer_tty:
51 for io in [sys.stdout, sys.stderr]:
52 if io is not None and io.isatty():
53 # (This is `None` when using `pythonw.exe` on Windows.)
54 stdout = io
55 break
57 # If the output is still `None`, use a DummyOutput.
58 # This happens for instance on Windows, when running the application under
59 # `pythonw.exe`. In that case, there won't be a terminal Window, and
60 # stdin/stdout/stderr are `None`.
61 if stdout is None:
62 return DummyOutput()
64 # If the patch_stdout context manager has been used, then sys.stdout is
65 # replaced by this proxy. For prompt_toolkit applications, we want to use
66 # the real stdout.
67 from prompt_toolkit.patch_stdout import StdoutProxy
69 while isinstance(stdout, StdoutProxy):
70 stdout = stdout.original_stdout
72 if sys.platform == "win32":
73 from .conemu import ConEmuOutput
74 from .win32 import Win32Output
75 from .windows10 import Windows10_Output, is_win_vt100_enabled
77 if is_win_vt100_enabled():
78 return cast(
79 Output,
80 Windows10_Output(stdout, default_color_depth=color_depth_from_env),
81 )
82 if is_conemu_ansi():
83 return cast(
84 Output, ConEmuOutput(stdout, default_color_depth=color_depth_from_env)
85 )
86 else:
87 return Win32Output(stdout, default_color_depth=color_depth_from_env)
88 else:
89 from .vt100 import Vt100_Output
91 # Stdout is not a TTY? Render as plain text.
92 # This is mostly useful if stdout is redirected to a file, and
93 # `print_formatted_text` is used.
94 if not stdout.isatty():
95 return PlainTextOutput(stdout)
97 return Vt100_Output.from_pty(
98 stdout,
99 term=term_from_env,
100 default_color_depth=color_depth_from_env,
101 enable_bell=bell_from_env,
102 )