Skip to content

Commit ffc3a0c

Browse files
committed
Add get_default_color_depth method to Output objects
1 parent 6ddbbfc commit ffc3a0c

File tree

8 files changed

+52
-17
lines changed

8 files changed

+52
-17
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 self.output.get_default_color_depth()
370370

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

374374
return depth
375375

prompt_toolkit/output/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ def get_rows_below_cursor_position(self) -> int:
163163
" For Windows only. "
164164
raise NotImplementedError
165165

166+
@abstractmethod
167+
def get_default_color_depth(self) -> ColorDepth:
168+
" Get default color depth for this output. "
169+
166170

167171
class DummyOutput(Output):
168172
"""
@@ -265,3 +269,6 @@ def get_size(self) -> Size:
265269

266270
def get_rows_below_cursor_position(self) -> int:
267271
return 40
272+
273+
def get_default_color_depth(self) -> ColorDepth:
274+
return ColorDepth.DEPTH_1_BIT

prompt_toolkit/output/color_depth.py

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

3737
@classmethod
38-
def default(cls, term: Optional[str] = None) -> "ColorDepth":
38+
def local_default(cls, term: Optional[str] = None) -> "ColorDepth":
3939
"""
4040
Return the default color depth, according to the $TERM value.
4141
@@ -50,25 +50,46 @@ def default(cls, term: Optional[str] = None) -> "ColorDepth":
5050
"""
5151
# Take `TERM` value from environment variable if nothing was passed.
5252
if term is None:
53-
term = os.environ.get("TERM", "")
53+
term = os.environ.get("TERM")
54+
55+
if is_dumb_terminal(term):
56+
return cls.DEPTH_1_BIT
57+
58+
# Check the `PROMPT_TOOLKIT_COLOR_DEPTH` environment variable.
59+
all_values = [i.value for i in ColorDepth]
60+
if os.environ.get("PROMPT_TOOLKIT_COLOR_DEPTH") in all_values:
61+
return cls(os.environ["PROMPT_TOOLKIT_COLOR_DEPTH"])
62+
63+
return cls.windows_default() if is_windows() else cls.vt100_default(term)
64+
65+
@classmethod
66+
def vt100_default(cls, term: Optional[str] = None) -> "ColorDepth":
67+
"""Return the default color depth for a vt100 terminal, according to the term
68+
value.
69+
70+
Contrary to `local_default`, this method doesn't take the local system into
71+
account.
72+
"""
73+
if term is None:
74+
return cls.DEFAULT
5475

5576
if is_dumb_terminal(term):
5677
return cls.DEPTH_1_BIT
5778

5879
if term in ("linux", "eterm-color"):
5980
return cls.DEPTH_4_BIT
6081

82+
return cls.DEFAULT
83+
84+
@classmethod
85+
def windows_default(cls) -> "ColorDepth":
86+
"""Return the default color depth for a windows terminal.
87+
88+
Contrary to `local_default`, this method doesn't take the local system into
89+
account.
90+
"""
6191
# For now, always use 4 bit color on Windows 10 by default, even when
6292
# vt100 escape sequences with ENABLE_VIRTUAL_TERMINAL_PROCESSING are
6393
# supported. We don't have a reliable way yet to know whether our
6494
# console supports true color or only 4-bit.
65-
if is_windows() and "PROMPT_TOOLKIT_COLOR_DEPTH" not in os.environ:
66-
return cls.DEPTH_4_BIT
67-
68-
# Check the `PROMPT_TOOLKIT_COLOR_DEPTH` environment variable.
69-
all_values = [i.value for i in ColorDepth]
70-
71-
if os.environ.get("PROMPT_TOOLKIT_COLOR_DEPTH") in all_values:
72-
return cls(os.environ["PROMPT_TOOLKIT_COLOR_DEPTH"])
73-
74-
return cls.DEPTH_8_BIT
95+
return cls.DEPTH_4_BIT

prompt_toolkit/output/vt100.py

Lines changed: 4 additions & 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] = {
@@ -683,3 +683,6 @@ def bell(self) -> None:
683683
" Sound bell. "
684684
self.write_raw("\a")
685685
self.flush()
686+
687+
def get_default_color_depth(self) -> ColorDepth:
688+
return ColorDepth.vt100_default(self.term)

prompt_toolkit/output/win32.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ def win32_refresh_window(cls) -> None:
479479
RDW_INVALIDATE = 0x0001
480480
windll.user32.RedrawWindow(handle, None, None, c_uint(RDW_INVALIDATE))
481481

482+
def get_default_color_depth(self) -> ColorDepth:
483+
return ColorDepth.windows_default()
484+
482485

483486
class FOREGROUND_COLOR:
484487
BLACK = 0x0000

prompt_toolkit/output/windows10.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __getattr__(self, name: str) -> Any:
6060
"get_win32_screen_buffer_info",
6161
"enable_bracketed_paste",
6262
"disable_bracketed_paste",
63+
"get_default_color_depth",
6364
):
6465
return getattr(self.win32_output, name)
6566
else:

prompt_toolkit/renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def print_formatted_text(
742742
"""
743743
fragments = to_formatted_text(formatted_text)
744744
style_transformation = style_transformation or DummyStyleTransformation()
745-
color_depth = color_depth or ColorDepth.default()
745+
color_depth = color_depth or output.get_default_color_depth()
746746

747747
# Reset first.
748748
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 output.get_default_color_depth()
118118

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

0 commit comments

Comments
 (0)