Skip to content

Commit 94f6c9c

Browse files
Support for 24bit true color.
1 parent 0fa994e commit 94f6c9c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

examples/ptpython_config/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def configure(repl):
9797
# Use this colorscheme for the code.
9898
repl.use_code_colorscheme('pastie')
9999

100+
# Enable 24bit True color. (Not all terminals support this. -- maybe check
101+
# $TERM before changing.)
102+
repl.true_color = False
103+
100104
# Install custom colorscheme named 'my-colorscheme' and use it.
101105
"""
102106
repl.install_ui_colorscheme('my-colorscheme', _custom_ui_colorscheme)

ptpython/python_input.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from prompt_toolkit.interface import CommandLineInterface, Application, AcceptAction
2020
from prompt_toolkit.key_binding.manager import KeyBindingManager
2121
from prompt_toolkit.layout.lexers import PygmentsLexer
22+
from prompt_toolkit.shortcuts import create_output
2223
from prompt_toolkit.styles import DynamicStyle
2324
from prompt_toolkit.utils import Callback, is_windows
2425
from prompt_toolkit.validation import ConditionalValidator
@@ -120,7 +121,7 @@ class PythonInput(object):
120121
121122
python_input = PythonInput(...)
122123
application = python_input.create_application()
123-
cli = CommandLineInterface(application=application)
124+
cli = PythonCommandLineInterface(application=application)
124125
python_code = cli.run()
125126
"""
126127
def __init__(self,
@@ -210,6 +211,7 @@ def __init__(self,
210211
self._current_code_style_name = 'win32'
211212

212213
self._current_style = self._generate_style()
214+
self.true_color = False
213215

214216
# Options to be configurable from the sidebar.
215217
self.options = self._create_options()
@@ -479,6 +481,9 @@ def get_values():
479481
get_values=lambda: dict(
480482
(name, partial(self.use_ui_colorscheme, name)) for name in self.ui_styles)
481483
),
484+
simple_option(title='True color (24 bit)',
485+
description='Use 24 bit colors instead of 265 colors',
486+
field_name='true_color'),
482487
]),
483488
]
484489

@@ -613,8 +618,15 @@ def on_reset(self, cli):
613618

614619

615620
class PythonCommandLineInterface(CommandLineInterface):
616-
def __init__(self, eventloop=None, input=None, output=None):
617-
python_input = PythonInput()
621+
def __init__(self, eventloop=None, python_input=None, input=None, output=None):
622+
assert python_input is None or isinstance(python_input, PythonInput)
623+
624+
python_input = python_input or PythonInput()
625+
626+
# Make sure that the prompt_toolkit 'renderer' knows about the
627+
# 'true_color' property of PythonInput.
628+
if output is None:
629+
output=create_output(true_color=Condition(lambda: python_input.true_color))
618630

619631
super(PythonCommandLineInterface, self).__init__(
620632
application=python_input.create_application(),

ptpython/repl.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
from pygments.styles.default import DefaultStyle
1414

1515
from prompt_toolkit.application import AbortAction
16+
from prompt_toolkit.enums import DEFAULT_BUFFER
1617
from prompt_toolkit.interface import AcceptAction, CommandLineInterface
1718
from prompt_toolkit.layout.utils import token_list_width
1819
from prompt_toolkit.shortcuts import create_asyncio_eventloop
19-
from prompt_toolkit.utils import DummyContext, Callback
20-
from prompt_toolkit.enums import DEFAULT_BUFFER
2120
from prompt_toolkit.styles import PygmentsStyle
21+
from prompt_toolkit.utils import DummyContext, Callback
2222

23-
from .python_input import PythonInput
23+
from .python_input import PythonInput, PythonCommandLineInterface
2424
from .eventloop import create_eventloop
2525

2626
import os
@@ -291,7 +291,7 @@ def get_locals():
291291
if configure:
292292
configure(repl)
293293

294-
cli = CommandLineInterface(application=repl.create_application(), eventloop=eventloop)
294+
cli = PythonCommandLineInterface(python_input=repl, eventloop=eventloop)
295295

296296
# Start repl.
297297
patch_context = cli.patch_stdout_context() if patch_stdout else DummyContext()

0 commit comments

Comments
 (0)