Skip to content

Commit cb901ba

Browse files
committed
Add output.is_windows() method so remote terminals are not affected by the host configuration
1 parent 56e511a commit cb901ba

File tree

8 files changed

+25
-13
lines changed

8 files changed

+25
-13
lines changed

prompt_toolkit/application/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ def color_depth(self) -> ColorDepth:
366366
depth = self._color_depth
367367

368368
if callable(depth):
369-
return depth() or ColorDepth.default()
369+
return depth() or ColorDepth.default(self.output)
370370

371371
if depth is None:
372-
return ColorDepth.default()
372+
return ColorDepth.default(self.output)
373373

374374
return depth
375375

prompt_toolkit/output/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Output(metaclass=ABCMeta):
2424
:class:`~prompt_toolkit.output.win32.Win32Output`.
2525
"""
2626

27+
def is_windows(self):
28+
return False
29+
2730
@abstractmethod
2831
def fileno(self) -> int:
2932
" Return the file descriptor to which we can write for the output. "

prompt_toolkit/output/color_depth.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class ColorDepth(str, Enum):
3535
TRUE_COLOR = DEPTH_24_BIT
3636

3737
@classmethod
38-
def default(cls, term: Optional[str] = None) -> "ColorDepth":
38+
def default(cls, output: Optional[Output] = None) -> "ColorDepth":
3939
"""
40-
Return the default color depth, according to the $TERM value.
40+
Return the default color depth, according to the provided output.
4141
4242
We prefer 256 colors almost always, because this is what most terminals
4343
support these days, and is a good default.
@@ -46,9 +46,12 @@ def default(cls, term: Optional[str] = None) -> "ColorDepth":
4646
override this outcome. This is a way to enforce a certain color depth
4747
in all prompt_toolkit applications.
4848
49-
If no `term` parameter is given, we use the $TERM environment variable.
49+
If no `output` parameter is given or it does not provide a corresponding
50+
`term`, we use the $TERM environment variable.
5051
"""
5152
# Take `TERM` value from environment variable if nothing was passed.
53+
term = getattr(output, "term", None)
54+
5255
if term is None:
5356
term = os.environ.get("TERM", "")
5457

@@ -62,7 +65,7 @@ def default(cls, term: Optional[str] = None) -> "ColorDepth":
6265
# vt100 escape sequences with ENABLE_VIRTUAL_TERMINAL_PROCESSING are
6366
# supported. We don't have a reliable way yet to know whether our
6467
# console supports true color or only 4-bit.
65-
if is_windows() and "PROMPT_TOOLKIT_COLOR_DEPTH" not in os.environ:
68+
if output.is_windows() and "PROMPT_TOOLKIT_COLOR_DEPTH" not in os.environ:
6669
return cls.DEPTH_4_BIT
6770

6871
# Check the `PROMPT_TOOLKIT_COLOR_DEPTH` environment variable.

prompt_toolkit/output/vt100.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def __init__(
426426
self.stdout = stdout
427427
self.write_binary = write_binary
428428
self._get_size = get_size
429-
self.term = term or "xterm"
429+
self.term = term
430430

431431
# Cache for escape codes.
432432
self._escape_code_caches: Dict[ColorDepth, _EscapeCodeCache] = {

prompt_toolkit/output/win32.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def __init__(self, stdout: TextIO, use_complete_width: bool = False) -> None:
104104
if _DEBUG_RENDER_OUTPUT:
105105
self.LOG = open(_DEBUG_RENDER_OUTPUT_FILENAME, "ab")
106106

107+
def is_windows(self):
108+
return True
109+
107110
def fileno(self) -> int:
108111
" Return file descriptor. "
109112
return self.stdout.fileno()

prompt_toolkit/output/windows10.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def flush(self) -> None:
5252

5353
def __getattr__(self, name: str) -> Any:
5454
if name in (
55+
"term",
56+
"is_windows",
5557
"get_size",
5658
"get_rows_below_cursor_position",
5759
"enable_mouse_support",

prompt_toolkit/renderer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
DummyStyleTransformation,
2222
StyleTransformation,
2323
)
24-
from prompt_toolkit.utils import is_windows
2524

2625
if TYPE_CHECKING:
2726
from prompt_toolkit.application import Application
@@ -396,7 +395,7 @@ def reset(self, _scroll: bool = False, leave_alternate_screen: bool = True) -> N
396395

397396
# In case of Windows, also make sure to scroll to the current cursor
398397
# position. (Only when rendering the first time.)
399-
if is_windows() and _scroll:
398+
if self.output.is_windows() and _scroll:
400399
self.output.scroll_buffer_to_prompt()
401400

402401
# Quit alternate screen.
@@ -433,7 +432,9 @@ def height_is_known(self) -> bool:
433432
is known, in order to avoid flickering when the CPR response arrives.)
434433
"""
435434
return (
436-
self.full_screen or self._min_available_height > 0 or is_windows()
435+
self.full_screen
436+
or self._min_available_height > 0
437+
or self.output.is_windows()
437438
) # On Windows, we don't have to wait for a CPR.
438439

439440
@property
@@ -471,7 +472,7 @@ def request_absolute_cursor_position(self) -> None:
471472

472473
# For Win32, we have an API call to get the number of rows below the
473474
# cursor.
474-
elif is_windows():
475+
elif self.output.is_windows():
475476
self._min_available_height = self.output.get_rows_below_cursor_position()
476477

477478
# Use CPR.
@@ -742,7 +743,7 @@ def print_formatted_text(
742743
"""
743744
fragments = to_formatted_text(formatted_text)
744745
style_transformation = style_transformation or DummyStyleTransformation()
745-
color_depth = color_depth or ColorDepth.default()
746+
color_depth = color_depth or ColorDepth.default(output)
746747

747748
# Reset first.
748749
output.reset_attributes()

prompt_toolkit/shortcuts/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def print_formatted_text(
114114
assert isinstance(output, Output)
115115

116116
# Get color depth.
117-
color_depth = color_depth or ColorDepth.default()
117+
color_depth = color_depth or ColorDepth.default(output)
118118

119119
# Merges values.
120120
def to_text(val: Any) -> StyleAndTextTuples:

0 commit comments

Comments
 (0)