Skip to content

Commit 86c6d11

Browse files
Use ANSI colors for the default theme.
1 parent d29f20d commit 86c6d11

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/ptpython/python_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def __init__(
350350
#: Load styles.
351351
self.code_styles: dict[str, BaseStyle] = get_all_code_styles()
352352
self.ui_styles = get_all_ui_styles()
353-
self._current_code_style_name: str = "default"
353+
self._current_code_style_name: str = "default-ansi"
354354
self._current_ui_style_name: str = "default"
355355

356356
if is_windows():

src/ptpython/style.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def get_all_code_styles() -> dict[str, BaseStyle]:
1717
for name in get_all_styles()
1818
}
1919
result["win32"] = Style.from_dict(win32_code_style)
20+
result["default-ansi"] = Style.from_dict(default_ansi_code_style)
2021
return result
2122

2223

@@ -38,6 +39,63 @@ def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle:
3839
return merge_styles([python_style, ui_style])
3940

4041

42+
# Use ANSI colors for the default theme.
43+
# This is `DefaultStyle` from Pygments, modified to use ANSI colors instead of
44+
# RGB. This adapts better to light/dark mode, because the built-in themes from
45+
# a terminal are typically designed for whatever background is used. All the
46+
# other Pygments themes use RGB, which is fine, because the user consciously
47+
# chooses what works for them.
48+
49+
# To convert, do:
50+
# from prompt_toolkit.output import ColorDepth
51+
# from prompt_toolkit.output.vt100 import _EscapeCodeCache, _get_closest_ansi_color
52+
# print(_get_closest_ansi_color(
53+
# *_EscapeCodeCache(ColorDepth.DEPTH_8_BIT)._color_name_to_rgb('bbbbbb'))
54+
# )
55+
56+
default_ansi_code_style = {
57+
"pygments.whitespace": "ansigray", # "#bbbbbb",
58+
"pygments.comment": "italic ansibrightblack", # "italic #3d7b7b",
59+
"pygments.comment.preproc": "noitalic ansired", # "noitalic #9c6500",
60+
"pygments.keyword": "bold ansigreen", # "bold #008000",
61+
"pygments.keyword.pseudo": "nobold",
62+
"pygments.keyword.type": "nobold ansired", # "nobold #b00040",
63+
"pygments.operator": "ansibrightblack", # "#666666",
64+
"pygments.operator.word": "bold ansimagenta", # "bold #aa22ff",
65+
"pygments.name.builtin": "ansigreen", # "#008000",
66+
"pygments.name.function": "ansibrightblue", # "#0000ff",
67+
"pygments.name.class": "bold ansibrightblue", # "bold #0000ff",
68+
"pygments.name.namespace": "bold ansibrightblack", # "bold #0000ff",
69+
"pygments.name.exception": "bold ansired", # "bold #cb3f38",
70+
"pygments.name.variable": "ansiblue", # "#19177c",
71+
"pygments.name.constant": "ansired", # "#880000",
72+
"pygments.name.label": "ansiyellow", # "#767600",
73+
"pygments.name.entity": "bold ansibrightblack", # "bold #717171",
74+
"pygments.name.attribute": "ansibrightblack", # "#687822",
75+
"pygments.name.tag": "bold ansigreen", # "bold #008000",
76+
"pygments.name.decorator": "ansimagenta", # "#aa22ff",
77+
"pygments.literal.string": "ansired", # "#ba2121",
78+
"pygments.literal.string.doc": "italic",
79+
"pygments.literal.string.interpol": "bold ansibrightblack", # "bold #a45a77",
80+
"pygments.literal.string.escape": "bold ansired", # "bold #aa5d1f",
81+
"pygments.literal.string.regex": "ansibrightblack", # "#a45a77",
82+
"pygments.literal.string.symbol": "ansiblue", # "#19177c",
83+
"pygments.literal.string.other": "ansigreen", # "#008000",
84+
"pygments.literal.number": "ansibrightblack", # "#666666",
85+
"pygments.generic.heading": "bold ansiblue", # "bold #000080",
86+
"pygments.generic.subheading": "bold ansimagenta", # "bold #800080",
87+
"pygments.generic.deleted": "ansired", # "#a00000",
88+
"pygments.generic.inserted": "ansigreen", # "#008400",
89+
"pygments.generic.error": "ansigreen", # "#e40000",
90+
"pygments.generic.emph": "italic",
91+
"pygments.generic.strong": "bold",
92+
"pygments.generic.emphstrong": "bold italic",
93+
"pygments.generic.prompt": "bold ansiblue", # "bold #000080",
94+
"pygments.generic.output": "ansibrightblack", # "#717171",
95+
"pygments.generic.traceback": "ansiblue", # "#04d",
96+
"pygments.error": "", # "border:#ff0000",
97+
}
98+
4199
# Code style for Windows consoles. They support only 16 colors,
42100
# so we choose a combination that displays nicely.
43101
win32_code_style = {

0 commit comments

Comments
 (0)