Skip to content

Commit 81fbd23

Browse files
authored
Adds strikethrough text attribute. (#1446)
Adds strikethrough text attribute. - Adds tags: <strike>, <s> and <del> - Change `pygments.generic.deleted` from "#a00000" to "strike".
1 parent 76eee48 commit 81fbd23

File tree

10 files changed

+69
-6
lines changed

10 files changed

+69
-6
lines changed

prompt_toolkit/formatted_text/ansi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(self, value: str) -> None:
3535
self._bgcolor: Optional[str] = None
3636
self._bold = False
3737
self._underline = False
38+
self._strike = False
3839
self._italic = False
3940
self._blink = False
4041
self._reverse = False
@@ -138,6 +139,8 @@ def _select_graphic_rendition(self, attrs: List[int]) -> None:
138139
self._reverse = True
139140
elif attr == 8:
140141
self._hidden = True
142+
elif attr == 9:
143+
self._strike = True
141144
elif attr == 22:
142145
self._bold = False
143146
elif attr == 23:
@@ -148,11 +151,14 @@ def _select_graphic_rendition(self, attrs: List[int]) -> None:
148151
self._blink = False
149152
elif attr == 27:
150153
self._reverse = False
154+
elif attr == 29:
155+
self._strike = False
151156
elif not attr:
152157
self._color = None
153158
self._bgcolor = None
154159
self._bold = False
155160
self._underline = False
161+
self._strike = False
156162
self._italic = False
157163
self._blink = False
158164
self._reverse = False
@@ -199,6 +205,8 @@ def _create_style_string(self) -> str:
199205
result.append("bold")
200206
if self._underline:
201207
result.append("underline")
208+
if self._strike:
209+
result.append("strike")
202210
if self._italic:
203211
result.append("italic")
204212
if self._blink:

prompt_toolkit/formatted_text/html.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class HTML:
1616
# Turn something into red.
1717
HTML('<style fg="ansired" bg="#00ff44">...</style>')
1818
19-
# Italic, bold and underline.
19+
# Italic, bold, underline and strike.
2020
HTML('<i>...</i>')
2121
HTML('<b>...</b>')
2222
HTML('<u>...</u>')
23+
HTML('<s>...</s>')
2324
2425
All HTML elements become available as a "class" in the style sheet.
2526
E.g. ``<username>...</username>`` can be styled, by setting a style for

prompt_toolkit/output/vt100.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ def __missing__(self, value: Tuple[int, int, int]) -> int:
269269
class _EscapeCodeCache(Dict[Attrs, str]):
270270
"""
271271
Cache for VT100 escape codes. It maps
272-
(fgcolor, bgcolor, bold, underline, reverse) tuples to VT100 escape sequences.
272+
(fgcolor, bgcolor, bold, underline, strike, reverse) tuples to VT100
273+
escape sequences.
273274
274275
:param true_color: When True, use 24bit colors instead of 256 colors.
275276
"""
@@ -278,7 +279,17 @@ def __init__(self, color_depth: ColorDepth) -> None:
278279
self.color_depth = color_depth
279280

280281
def __missing__(self, attrs: Attrs) -> str:
281-
fgcolor, bgcolor, bold, underline, italic, blink, reverse, hidden = attrs
282+
(
283+
fgcolor,
284+
bgcolor,
285+
bold,
286+
underline,
287+
strike,
288+
italic,
289+
blink,
290+
reverse,
291+
hidden,
292+
) = attrs
282293
parts: List[str] = []
283294

284295
parts.extend(self._colors_to_code(fgcolor or "", bgcolor or ""))
@@ -295,6 +306,8 @@ def __missing__(self, attrs: Attrs) -> str:
295306
parts.append("7")
296307
if hidden:
297308
parts.append("8")
309+
if strike:
310+
parts.append("9")
298311

299312
if parts:
300313
result = "\x1b[0;" + ";".join(parts) + "m"

prompt_toolkit/output/win32.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,17 @@ def reset_attributes(self) -> None:
279279
self._hidden = False
280280

281281
def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None:
282-
fgcolor, bgcolor, bold, underline, italic, blink, reverse, hidden = attrs
282+
(
283+
fgcolor,
284+
bgcolor,
285+
bold,
286+
underline,
287+
strike,
288+
italic,
289+
blink,
290+
reverse,
291+
hidden,
292+
) = attrs
283293
self._hidden = bool(hidden)
284294

285295
# Start from the default attributes.

prompt_toolkit/renderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ def __missing__(self, style_str: str) -> bool:
308308
attrs.color
309309
or attrs.bgcolor
310310
or attrs.underline
311+
or attrs.strike
311312
or attrs.blink
312313
or attrs.reverse
313314
)

prompt_toolkit/styles/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
("bgcolor", Optional[str]),
2424
("bold", Optional[bool]),
2525
("underline", Optional[bool]),
26+
("strike", Optional[bool]),
2627
("italic", Optional[bool]),
2728
("blink", Optional[bool]),
2829
("reverse", Optional[bool]),
@@ -35,6 +36,7 @@
3536
:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'ansired'
3637
:param bold: Boolean
3738
:param underline: Boolean
39+
:param strike: Boolean
3840
:param italic: Boolean
3941
:param blink: Boolean
4042
:param reverse: Boolean
@@ -47,6 +49,7 @@
4749
bgcolor="",
4850
bold=False,
4951
underline=False,
52+
strike=False,
5053
italic=False,
5154
blink=False,
5255
reverse=False,

prompt_toolkit/styles/defaults.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,22 @@
102102
# Default styling of HTML elements.
103103
("i", "italic"),
104104
("u", "underline"),
105+
("s", "strike"),
105106
("b", "bold"),
106107
("em", "italic"),
107108
("strong", "bold"),
109+
("del", "strike"),
108110
("hidden", "hidden"),
109111
# It should be possible to use the style names in HTML.
110112
# <reverse>...</reverse> or <noreverse>...</noreverse>.
111113
("italic", "italic"),
112114
("underline", "underline"),
115+
("strike", "strike"),
113116
("bold", "bold"),
114117
("reverse", "reverse"),
115118
("noitalic", "noitalic"),
116119
("nounderline", "nounderline"),
120+
("nostrike", "nostrike"),
117121
("nobold", "nobold"),
118122
("noreverse", "noreverse"),
119123
# Prompt bottom toolbar
@@ -193,7 +197,7 @@
193197
"pygments.literal.number": "#666666",
194198
"pygments.generic.heading": "bold #000080",
195199
"pygments.generic.subheading": "bold #800080",
196-
"pygments.generic.deleted": "#a00000",
200+
"pygments.generic.deleted": "strike",
197201
"pygments.generic.inserted": "#00a000",
198202
"pygments.generic.error": "#ff0000",
199203
"pygments.generic.emph": "italic",

prompt_toolkit/styles/style.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def parse_color(text: str) -> str:
8181
bgcolor=None,
8282
bold=None,
8383
underline=None,
84+
strike=None,
8485
italic=None,
8586
blink=None,
8687
reverse=None,
@@ -130,6 +131,10 @@ def _parse_style_str(style_str: str) -> Attrs:
130131
attrs = attrs._replace(underline=True)
131132
elif part == "nounderline":
132133
attrs = attrs._replace(underline=False)
134+
elif part == "strike":
135+
attrs = attrs._replace(strike=True)
136+
elif part == "nostrike":
137+
attrs = attrs._replace(strike=False)
133138

134139
# prompt_toolkit extensions. Not in Pygments.
135140
elif part == "blink":
@@ -338,6 +343,7 @@ def _or(*values: _T) -> _T:
338343
bgcolor=_or("", *[a.bgcolor for a in list_of_attrs]),
339344
bold=_or(False, *[a.bold for a in list_of_attrs]),
340345
underline=_or(False, *[a.underline for a in list_of_attrs]),
346+
strike=_or(False, *[a.strike for a in list_of_attrs]),
341347
italic=_or(False, *[a.italic for a in list_of_attrs]),
342348
blink=_or(False, *[a.blink for a in list_of_attrs]),
343349
reverse=_or(False, *[a.reverse for a in list_of_attrs]),

tests/test_style.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def test_style_from_dict():
55
style = Style.from_dict(
66
{
7-
"a": "#ff0000 bold underline italic",
7+
"a": "#ff0000 bold underline strike italic",
88
"b": "bg:#00ff00 blink reverse",
99
}
1010
)
@@ -15,6 +15,7 @@ def test_style_from_dict():
1515
bgcolor="",
1616
bold=True,
1717
underline=True,
18+
strike=True,
1819
italic=True,
1920
blink=False,
2021
reverse=False,
@@ -28,6 +29,7 @@ def test_style_from_dict():
2829
bgcolor="00ff00",
2930
bold=False,
3031
underline=False,
32+
strike=False,
3133
italic=False,
3234
blink=True,
3335
reverse=True,
@@ -41,6 +43,7 @@ def test_style_from_dict():
4143
bgcolor="",
4244
bold=False,
4345
underline=False,
46+
strike=False,
4447
italic=False,
4548
blink=False,
4649
reverse=False,
@@ -54,6 +57,7 @@ def test_style_from_dict():
5457
bgcolor="",
5558
bold=True,
5659
underline=True,
60+
strike=True,
5761
italic=True,
5862
blink=False,
5963
reverse=False,
@@ -66,6 +70,7 @@ def test_style_from_dict():
6670
bgcolor="",
6771
bold=True,
6872
underline=True,
73+
strike=True,
6974
italic=True,
7075
blink=False,
7176
reverse=False,
@@ -89,6 +94,7 @@ def test_class_combinations_1():
8994
bgcolor="",
9095
bold=False,
9196
underline=False,
97+
strike=False,
9298
italic=False,
9399
blink=False,
94100
reverse=False,
@@ -118,6 +124,7 @@ def test_class_combinations_2():
118124
bgcolor="",
119125
bold=False,
120126
underline=False,
127+
strike=False,
121128
italic=False,
122129
blink=False,
123130
reverse=False,
@@ -133,6 +140,7 @@ def test_class_combinations_2():
133140
bgcolor="",
134141
bold=False,
135142
underline=False,
143+
strike=False,
136144
italic=False,
137145
blink=False,
138146
reverse=False,
@@ -158,6 +166,7 @@ def test_substyles():
158166
bgcolor="",
159167
bold=False,
160168
underline=False,
169+
strike=False,
161170
italic=False,
162171
blink=False,
163172
reverse=False,
@@ -170,6 +179,7 @@ def test_substyles():
170179
bgcolor="",
171180
bold=True,
172181
underline=False,
182+
strike=False,
173183
italic=False,
174184
blink=False,
175185
reverse=False,
@@ -184,6 +194,7 @@ def test_substyles():
184194
bgcolor="",
185195
bold=False,
186196
underline=False,
197+
strike=False,
187198
italic=False,
188199
blink=False,
189200
reverse=False,
@@ -197,6 +208,7 @@ def test_substyles():
197208
bgcolor="",
198209
bold=False,
199210
underline=False,
211+
strike=False,
200212
italic=True,
201213
blink=False,
202214
reverse=False,
@@ -215,6 +227,7 @@ def test_swap_light_and_dark_style_transformation():
215227
bgcolor="888844",
216228
bold=True,
217229
underline=True,
230+
strike=True,
218231
italic=True,
219232
blink=False,
220233
reverse=False,
@@ -225,6 +238,7 @@ def test_swap_light_and_dark_style_transformation():
225238
bgcolor="bbbb76",
226239
bold=True,
227240
underline=True,
241+
strike=True,
228242
italic=True,
229243
blink=False,
230244
reverse=False,
@@ -239,6 +253,7 @@ def test_swap_light_and_dark_style_transformation():
239253
bgcolor="ansiblack",
240254
bold=True,
241255
underline=True,
256+
strike=True,
242257
italic=True,
243258
blink=False,
244259
reverse=False,
@@ -249,6 +264,7 @@ def test_swap_light_and_dark_style_transformation():
249264
bgcolor="ansiwhite",
250265
bold=True,
251266
underline=True,
267+
strike=True,
252268
italic=True,
253269
blink=False,
254270
reverse=False,

tests/test_style_transformation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def default_attrs():
1010
bgcolor="",
1111
bold=False,
1212
underline=False,
13+
strike=False,
1314
italic=False,
1415
blink=False,
1516
reverse=False,

0 commit comments

Comments
 (0)