Skip to content

Commit c7e7160

Browse files
Refactoring of the prompt_toolkit.styles.
1 parent b0eea7b commit c7e7160

17 files changed

+499
-264
lines changed

docs/pages/building_prompts.rst

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,55 @@ Colors
6767
The colors for syntax highlighting are defined by a
6868
:class:`~prompt_toolkit.styles.Style` instance. By default, a neutral built-in
6969
style is used, but any style instance can be passed to the
70-
:func:`~prompt_toolkit.shortcuts.prompt` function. All Pygments style classes
71-
can be used as well, when they are wrapped in a
72-
:class:`~prompt_toolkit.styles.PygmentsStyle`.
70+
:func:`~prompt_toolkit.shortcuts.prompt` function. A simple way to create a
71+
style, is by using the :class:`~prompt_toolkit.styles.style_from_dict`
72+
function:
73+
74+
.. code:: python
75+
76+
from prompt_toolkit.shortcuts import prompt
77+
from prompt_toolkit.styles import style_from_dict
78+
79+
our_style = style_from_dict({
80+
Token.Comment: '#888888 bold',
81+
Token.Keyword: '#ff88ff bold',
82+
})
83+
84+
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer),
85+
style=our_style)
86+
87+
88+
The style dictionary is very similar to the Pygments ``styles`` dictionary,
89+
with a few differences:
90+
91+
- The `roman`, `sans`, `mono` and `border` options are not ignored.
92+
- The style has a few additions: `blink`, `noblink`, `reverse` and `noreverse`.
93+
- Colors can be in the `#ff0000` format, but they can be one of the built-in
94+
ANSI color names as well. In that case, they map directly to the 16 color
95+
palette of the terminal.
96+
97+
Using a Pygments style
98+
^^^^^^^^^^^^^^^^^^^^^^
99+
100+
All Pygments style classes can be used as well, when they are wrapped through
101+
:func:`~prompt_toolkit.styles.style_from_pygments`.
73102

74103
Suppose we'd like to use a Pygments style, for instance
75-
``pygments.styles.tango.TangoStyle``. That works when we wrap it inside
76-
:class:`~prompt_toolkit.styles.PygmentsStyle`, but we would still miss some
77-
``prompt_toolkit`` specific styling, like the highlighting of selected text and
78-
the styling of the completion menus. Because of that, we recommend to use the
79-
:meth:`~prompt_toolkit.styles.PygmentsStyle.from_defaults` method to generate a
80-
a :class:`~prompt_toolkit.styles.Style` instance.
104+
``pygments.styles.tango.TangoStyle``, that is possible like this:
81105

82106
Creating a custom style could be done like this:
83107

84108
.. code:: python
85109
86110
from prompt_toolkit.shortcuts import prompt
87-
from prompt_toolkit.styles import PygmentsStyle
111+
from prompt_toolkit.styles import style_from_pygments
88112
89-
from pygments.style import Style
90113
from pygments.styles.tango import TangoStyle
91114
92-
our_style = PygmentsStyle.from_defaults(
93-
pygments_style_cls=TangoStyle,
94-
style_dict={
95-
Token.Comment: '#888888 bold',
96-
Token.Keyword: '#ff88ff bold',
97-
})
115+
our_style = style_from_pygments(TangoStyle, {
116+
Token.Comment: '#888888 bold',
117+
Token.Keyword: '#ff88ff bold',
118+
})
98119
99120
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer),
100121
style=our_style)
@@ -112,10 +133,9 @@ Each token is a Pygments token and can be styled individually.
112133
.. code:: python
113134
114135
from prompt_toolkit.shortcuts import prompt
115-
from pygments.style import Style
116-
from prompt_toolkit.styles import PygmentsStyle
136+
from prompt_toolkit.styles import style_from_dict
117137
118-
example_style = PygmentsStyle.from_defaults({
138+
example_style = style_from_dict({
119139
# User input.
120140
Token: '#ff0066',
121141
@@ -159,7 +179,7 @@ is simple with the :func:`~prompt_toolkit.shortcuts.print_tokens` function.
159179
.. code:: python
160180
161181
# Create a stylesheet.
162-
style = PygmentsStyle.from_defaults(style_dict={
182+
style = style_from_dict({
163183
Token.Hello: '#ff0066',
164184
Token.World: '#44ff44 italic',
165185
})

examples/bottom-toolbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import unicode_literals
66
from prompt_toolkit import prompt
77
from prompt_toolkit.styles import PygmentsStyle
8-
from pygments.token import Token
8+
from prompt_toolkit.token import Token
99

1010

1111
test_style = PygmentsStyle.from_defaults({

examples/clock-input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
Example of a 'dynamic' prompt. On that shows the current time in the prompt.
44
"""
55
from __future__ import unicode_literals
6-
from prompt_toolkit.interface import CommandLineInterface
76
from prompt_toolkit.application import Application
7+
from prompt_toolkit.interface import CommandLineInterface
88
from prompt_toolkit.layout import Window
99
from prompt_toolkit.layout.controls import BufferControl
1010
from prompt_toolkit.layout.processors import BeforeInput
1111
from prompt_toolkit.shortcuts import create_eventloop
12+
from prompt_toolkit.token import Token
1213
from prompt_toolkit.utils import Callback
13-
from pygments.token import Token
1414

1515
import datetime
1616
import time

examples/colored-prompt.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from __future__ import unicode_literals
66

77
from prompt_toolkit import prompt
8-
from prompt_toolkit.styles import PygmentsStyle
9-
from pygments.token import Token
8+
from prompt_toolkit.styles import style_from_dict
9+
from prompt_toolkit.token import Token
1010

1111

12-
example_style = PygmentsStyle.from_defaults(style_dict={
12+
example_style = style_from_dict({
1313
# User input.
1414
Token: '#ff0066',
1515

@@ -20,6 +20,10 @@
2020
Token.Pound: '#00aa00',
2121
Token.Host: '#000088 bg:#aaaaff',
2222
Token.Path: '#884444 underline',
23+
24+
# Make a selection reverse/underlined.
25+
# (Use Control-Space to select.)
26+
Token.SelectedText: 'reverse underline',
2327
})
2428

2529

examples/full-screen-layout.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from prompt_toolkit.layout.controls import BufferControl, FillControl, TokenListControl
1818
from prompt_toolkit.layout.dimension import LayoutDimension as D
1919
from prompt_toolkit.shortcuts import create_eventloop
20-
21-
from pygments.token import Token
20+
from prompt_toolkit.token import Token
2221

2322

2423
# 1. First we create the layout

examples/get-multiline-input.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22
from __future__ import unicode_literals
33
from prompt_toolkit import prompt
4-
from pygments.token import Token
4+
from prompt_toolkit.token import Token
5+
56

67
def continuation_tokens(cli, width):
78
" The continuation: display dots before all the following lines. "

examples/html-input.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
"""
33
Simple example of a syntax-highlighted HTML input line.
4+
(This requires Pygments to be installed.)
45
"""
56
from __future__ import unicode_literals
67
from pygments.lexers import HtmlLexer

examples/regular-language.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer
2222
from prompt_toolkit.layout.lexers import SimpleLexer
2323
from prompt_toolkit.styles import PygmentsStyle
24-
25-
from pygments.token import Token
24+
from prompt_toolkit.token import Token
2625

2726
import math
2827

prompt_toolkit/shortcuts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454

5555
try:
5656
from pygments.lexer import Lexer as pygments_Lexer
57+
from pygments.style import Style as pygments_Style
5758
except ImportError:
5859
pygments_Lexer = None
60+
pygments_Style = None
5961

6062
if is_windows():
6163
from .terminal.win32_output import Win32Output
@@ -213,7 +215,7 @@ def create_prompt_layout(message='', lexer=None, is_password=False,
213215
# class is given, turn it into a PygmentsLexer. (Important for
214216
# backwards-compatibility.)
215217
try:
216-
if pygments_Lexer and issubclass(lexer, pygments.lexer.Lexer):
218+
if pygments_Lexer and issubclass(lexer, pygments_Lexer):
217219
lexer = PygmentsLexer(lexer)
218220
except TypeError: # Happens when lexer is `None` or an instance of something else.
219221
pass
@@ -418,7 +420,7 @@ def create_prompt_application(
418420

419421
# Accept Pygments styles as well for backwards compatibility.
420422
try:
421-
if issubclass(style, pygments.style.Style):
423+
if pygments_Style and issubclass(style, pygments_Style):
422424
style = PygmentsStyle(style)
423425
except TypeError: # Happens when style is `None` or an instance of something else.
424426
pass

0 commit comments

Comments
 (0)