defaults.py 3.5 KB

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