txtwrap 3.0.0__tar.gz → 3.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {txtwrap-3.0.0/txtwrap.egg-info → txtwrap-3.1.0}/PKG-INFO +85 -71
- {txtwrap-3.0.0 → txtwrap-3.1.0}/README.md +81 -70
- {txtwrap-3.0.0 → txtwrap-3.1.0}/setup.py +7 -3
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap/__init__.py +8 -3
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap/__init__.pyi +5 -4
- txtwrap-3.1.0/txtwrap/_utils.py +78 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap/constants.py +6 -2
- txtwrap-3.1.0/txtwrap/constants.pyi +10 -0
- txtwrap-3.1.0/txtwrap/identities.py +3 -0
- txtwrap-3.1.0/txtwrap/wrapper.py +636 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap/wrapper.pyi +83 -70
- {txtwrap-3.0.0 → txtwrap-3.1.0/txtwrap.egg-info}/PKG-INFO +85 -71
- txtwrap-3.0.0/txtwrap/_utils.py +0 -34
- txtwrap-3.0.0/txtwrap/constants.pyi +0 -8
- txtwrap-3.0.0/txtwrap/identities.py +0 -3
- txtwrap-3.0.0/txtwrap/wrapper.py +0 -640
- {txtwrap-3.0.0 → txtwrap-3.1.0}/MANIFEST.in +0 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/setup.cfg +0 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap/identities.pyi +0 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap.egg-info/SOURCES.txt +0 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap.egg-info/dependency_links.txt +0 -0
- {txtwrap-3.0.0 → txtwrap-3.1.0}/txtwrap.egg-info/top_level.txt +0 -0
@@ -1,11 +1,13 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: txtwrap
|
3
|
-
Version: 3.
|
3
|
+
Version: 3.1.0
|
4
4
|
Summary: A tool for wrapping and filling text.
|
5
5
|
Home-page: https://github.com/azzammuhyala/txtwrap
|
6
6
|
Author: azzammuhyala
|
7
7
|
Author-email: azzammuhyala@gmail.com
|
8
8
|
License: MIT
|
9
|
+
Project-URL: Source, https://github.com/azzammuhyala/txtwrap
|
10
|
+
Project-URL: Bug Tracker, https://github.com/azzammuhyala/txtwrap/issues
|
9
11
|
Keywords: wrap,wrapper,wrapping,wrapped,text wrap,text wrapper,text wrapping,text wrapped
|
10
12
|
Classifier: Programming Language :: Python
|
11
13
|
Classifier: Programming Language :: Python :: 3
|
@@ -22,27 +24,29 @@ Dynamic: description-content-type
|
|
22
24
|
Dynamic: home-page
|
23
25
|
Dynamic: keywords
|
24
26
|
Dynamic: license
|
27
|
+
Dynamic: project-url
|
25
28
|
Dynamic: requires-python
|
26
29
|
Dynamic: summary
|
27
30
|
|
28
31
|
# TxTWrap🔡
|
29
32
|
A tool for wrapping and filling text.🔨
|
30
33
|
|
31
|
-
Package version: **3.
|
34
|
+
Package version: **3.1.0** <br>
|
32
35
|
Python requires version: **>=3.0.0** <br>
|
33
36
|
Python stub file requires version: **>=3.5.0** <br>
|
34
37
|
|
35
38
|
- [`LOREM_IPSUM_WORDS`](#lorem-ipsum)
|
36
39
|
- [`LOREM_IPSUM_SENTENCES`](#lorem-ipsum)
|
37
40
|
- [`LOREM_IPSUM_PARAGRAPHS`](#lorem-ipsum)
|
38
|
-
- [`SEPARATOR_WHITESPACE`](#separators) (
|
39
|
-
- [`SEPARATOR_ESCAPE`](#separators) (
|
41
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (✅ Updated)
|
42
|
+
- [`SEPARATOR_ESCAPE`](#separators) (✅ Updated)
|
43
|
+
- [`SEPARATOR_NEWLINE`](#separators) (➕ New)
|
44
|
+
- [`SEPARATOR_NEWLINE_AND_BREAK`](#separators) (➕ New)
|
40
45
|
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
41
|
-
- [`sanitize`](#sanitizetext) (🛠️ Fixed)
|
42
46
|
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
43
|
-
- [`align`](#aligntext-return_detailsfalse) (
|
44
|
-
- [`fillstr`](#fillstrtext) (
|
45
|
-
- [`shorten`](#shortentext) (
|
47
|
+
- [`align`](#aligntext-return_detailsfalse) (✅ Updated)
|
48
|
+
- [`fillstr`](#fillstrtext) (✅ Updated)
|
49
|
+
- [`shorten`](#shortentext) (✅ Updated)
|
46
50
|
|
47
51
|
# Documents📄
|
48
52
|
This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
|
@@ -55,6 +59,7 @@ filling _monospace fonts_ but also for other font types, such as _Arial_, _Times
|
|
55
59
|
<h1></h1>
|
56
60
|
|
57
61
|
## Constants
|
62
|
+
_File: **txtwrap.constants**_
|
58
63
|
|
59
64
|
### Lorem ipsum
|
60
65
|
```py
|
@@ -73,16 +78,20 @@ A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used
|
|
73
78
|
```py
|
74
79
|
SEPARATOR_WHITESPACE
|
75
80
|
SEPARATOR_ESCAPE
|
81
|
+
SEPARATOR_NEWLINE
|
82
|
+
SEPARATOR_NEWLINE_AND_BREAK
|
76
83
|
```
|
77
84
|
A collection of separators that can be used to separate text.
|
78
|
-
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
79
|
-
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\
|
80
|
-
|
81
|
-
|
85
|
+
- `SEPARATOR_WHITESPACE` (for [`word_separator`](#word_separator)) regex contains whitespace characters.
|
86
|
+
- `SEPARATOR_ESCAPE` (for [`word_separator`](#word_separator)) regex contains whitespace characters including `'\a'`, `'\b'`, and `'\0'`.
|
87
|
+
- `SEPARATOR_NEWLINE` (for [`newline_separator`](#newline_separator)) regex contains newline characters.
|
88
|
+
- `SEPARATOR_NEWLINE_AND_BREAK` (for [`newline_separator`](#newline_separator)) regex contains newline characters including breaks tag `<br>`.
|
82
89
|
|
83
90
|
<h1></h1>
|
84
91
|
|
85
92
|
## `TextWrapper`
|
93
|
+
_File: **txtwrap.wrapper**_
|
94
|
+
|
86
95
|
```py
|
87
96
|
class TextWrapper:
|
88
97
|
|
@@ -93,15 +102,17 @@ class TextWrapper:
|
|
93
102
|
mode: Literal['mono', 'word'] = 'word',
|
94
103
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
95
104
|
placeholder: str = '...',
|
96
|
-
|
97
|
-
|
105
|
+
space_character: str = ' ',
|
106
|
+
newline_character: str = '\n',
|
107
|
+
word_separator: Optional[Union[str, Tuple[str, int]]] = None,
|
108
|
+
newline_separator: Optional[Union[str, Tuple[str, int]]] = None,
|
98
109
|
max_lines: Optional[int] = None,
|
99
|
-
|
110
|
+
empty_lines: bool = True,
|
100
111
|
minimum_width: bool = True,
|
101
|
-
|
112
|
+
excess_word_separator: bool = False,
|
102
113
|
justify_last_line: bool = False,
|
103
114
|
break_on_hyphens: bool = True,
|
104
|
-
|
115
|
+
size_function: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
105
116
|
) -> None
|
106
117
|
```
|
107
118
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -114,9 +125,6 @@ is equivalent to:
|
|
114
125
|
wrapper = TextWrapper()
|
115
126
|
wrapper.width = 100
|
116
127
|
```
|
117
|
-
You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
|
118
|
-
attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
|
119
|
-
as each attribute has type checking, which may reduce performance.
|
120
128
|
|
121
129
|
<h1></h1>
|
122
130
|
|
@@ -158,16 +166,29 @@ as each attribute has type checking, which may reduce performance.
|
|
158
166
|
|
159
167
|
<h1></h1>
|
160
168
|
|
161
|
-
#### **`
|
162
|
-
(Default: `' '`) The character used for padding.
|
169
|
+
#### **`space_character`**
|
170
|
+
(Default: `' '`) The character used for padding and for the word separator character.
|
171
|
+
|
172
|
+
<h1></h1>
|
173
|
+
|
174
|
+
#### **`newline_character`**
|
175
|
+
(Default: `'\n'`) The character used for line separator.
|
163
176
|
|
164
177
|
<h1></h1>
|
165
178
|
|
166
|
-
#### **`
|
179
|
+
#### **`word_separator`**
|
167
180
|
(Default: `None`) The character used to separate words.
|
168
181
|
- `None`: Uses whitespace as the separator.
|
169
|
-
- `str`:
|
170
|
-
- `
|
182
|
+
- `str`: Regex separator in split form.
|
183
|
+
- `Tuple[str, int]`: Regex separator in split form with flags.
|
184
|
+
|
185
|
+
<h1></h1>
|
186
|
+
|
187
|
+
#### **`newline_separator`**
|
188
|
+
(Default: `None`) The character used to separate lines.
|
189
|
+
- `None`: Uses standard newline as the separator.
|
190
|
+
- `str`: Regex separator in split form.
|
191
|
+
- `Tuple[str, int]`: Regex separator in split form with flags.
|
171
192
|
|
172
193
|
<h1></h1>
|
173
194
|
|
@@ -179,7 +200,7 @@ as each attribute has type checking, which may reduce performance.
|
|
179
200
|
|
180
201
|
<h1></h1>
|
181
202
|
|
182
|
-
#### **`
|
203
|
+
#### **`empty_lines`**
|
183
204
|
(Default: `True`) Retains empty lines in the wrapped text.
|
184
205
|
|
185
206
|
<h1></h1>
|
@@ -190,8 +211,8 @@ enabling this attribute removes unnecessary empty space.
|
|
190
211
|
|
191
212
|
<h1></h1>
|
192
213
|
|
193
|
-
#### **`
|
194
|
-
(Default: `False`) Removes
|
214
|
+
#### **`excess_word_separator`**
|
215
|
+
(Default: `False`) Removes excess separators from between words one at a time.
|
195
216
|
|
196
217
|
<h1></h1>
|
197
218
|
|
@@ -206,7 +227,7 @@ enabling this attribute removes unnecessary empty space.
|
|
206
227
|
|
207
228
|
<h1></h1>
|
208
229
|
|
209
|
-
#### **`
|
230
|
+
#### **`size_function`**
|
210
231
|
(Default: `None`) A function used to calculate the width and height or only the width of each string.
|
211
232
|
|
212
233
|
If the function calculates both width and height, it must return a tuple containing two values:
|
@@ -233,39 +254,29 @@ is equivalent to:
|
|
233
254
|
<h1></h1>
|
234
255
|
|
235
256
|
#### **`copy`**
|
236
|
-
Creates and returns a copy of the [`TextWrapper`](#textwrapper) object.
|
237
|
-
|
238
|
-
<h1></h1>
|
239
|
-
|
240
|
-
#### **`sanitize(text)`**
|
241
|
-
Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
|
242
|
-
character.
|
243
|
-
|
244
|
-
For example:
|
245
|
-
```py
|
246
|
-
>>> TextWrapper().sanitize("\tHello \nWorld!\r ")
|
247
|
-
'Hello World!'
|
248
|
-
```
|
257
|
+
Creates and returns a copy of the [`TextWrapper`](#textwrapper) object. (External function using `copy.copy`)
|
249
258
|
|
250
259
|
<h1></h1>
|
251
260
|
|
252
261
|
#### **`wrap(text, return_details=False)`**
|
253
262
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
254
263
|
- `'wrapped'`: A list of wrapped text fragments.
|
255
|
-
- `'start_lines'`: A
|
256
|
-
- `'end_lines'`: A
|
264
|
+
- `'start_lines'`: A list of indices marking the start of line.
|
265
|
+
- `'end_lines'`: A list of indices marking the end of line.
|
257
266
|
|
258
267
|
For example:
|
259
268
|
```py
|
260
|
-
>>>
|
261
|
-
['Lorem ipsum', '
|
262
|
-
>>>
|
263
|
-
>>>
|
264
|
-
|
265
|
-
>>>
|
266
|
-
'Lorem ipsum'
|
267
|
-
>>>
|
268
|
-
'
|
269
|
+
>>> txtwrap.wrap(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
270
|
+
['Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.']
|
271
|
+
>>> wrapped_info = txtwrap.wrap(txtwrap.LOREM_IPSUM_WORDS, width=20, return_details=True)
|
272
|
+
>>> start_lines = wrapped_info['start_lines']
|
273
|
+
>>> end_lines = wrapped_info['end_lines']
|
274
|
+
>>> wrapped_info
|
275
|
+
{'wrapped': ['Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': [1], 'end_lines': [3]}
|
276
|
+
>>> wrapped_info['wrapped'][start_lines[0] - 1]
|
277
|
+
'Lorem ipsum odor'
|
278
|
+
>>> wrapped_info['wrapped'][end_lines[0] - 1]
|
279
|
+
'adipiscing elit.'
|
269
280
|
```
|
270
281
|
|
271
282
|
<h1></h1>
|
@@ -273,26 +284,28 @@ For example:
|
|
273
284
|
#### **`align(text, return_details=False)`**
|
274
285
|
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
275
286
|
with its coordinates.
|
276
|
-
> Note: [`
|
287
|
+
> Note: [`size_function`](#size_function) must return both width and height.
|
277
288
|
|
278
289
|
If `return_details=True`, returns a dictionary containing:
|
279
290
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
280
291
|
- `'wrapped'`: A list of wrapped text fragments.
|
281
|
-
- `'start_lines'`: A
|
282
|
-
- `'end_lines'`: A
|
292
|
+
- `'start_lines'`: A list of indices marking the start of line.
|
293
|
+
- `'end_lines'`: A list of indices marking the end of line.
|
283
294
|
- `'size'`: A calculated text size.
|
284
295
|
|
285
296
|
For example:
|
286
297
|
```py
|
287
|
-
>>>
|
298
|
+
>>> txtwrap.align(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
288
299
|
[(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
|
289
|
-
>>>
|
290
|
-
>>>
|
300
|
+
>>> aligned_info = txtwrap.align(txtwrap.LOREM_IPSUM_WORDS, width=20, return_details=True)
|
301
|
+
>>> start_lines = aligned_info['start_lines']
|
302
|
+
>>> end_lines = aligned_info['end_lines']
|
303
|
+
>>> aligned_info
|
291
304
|
{'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
|
292
|
-
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines':
|
293
|
-
>>>
|
305
|
+
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': [1], 'end_lines': [3], 'size': (18, 3)}
|
306
|
+
>>> aligned_info['wrapped'][start_lines[0] - 1]
|
294
307
|
'Lorem ipsum odor'
|
295
|
-
>>>
|
308
|
+
>>> aligned_info['wrapped'][end_lines[0] - 1]
|
296
309
|
'adipiscing elit.'
|
297
310
|
```
|
298
311
|
|
@@ -300,12 +313,12 @@ For example:
|
|
300
313
|
|
301
314
|
#### **`fillstr(text)`**
|
302
315
|
Returns a string with wrapped text formatted for monospace fonts.
|
303
|
-
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`
|
316
|
+
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`size_function`](#size_function)
|
304
317
|
(size or just length) must return `int`, not `float`!
|
305
318
|
|
306
319
|
For example:
|
307
320
|
```py
|
308
|
-
>>> s =
|
321
|
+
>>> s = txtwrap.fillstr(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
309
322
|
>>> s
|
310
323
|
'Lorem ipsum odor \namet, consectetuer\nadipiscing elit. '
|
311
324
|
>>> print(s)
|
@@ -322,7 +335,7 @@ if truncated.
|
|
322
335
|
|
323
336
|
For example:
|
324
337
|
```py
|
325
|
-
>>>
|
338
|
+
>>> txtwrap.shorten(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
326
339
|
'Lorem ipsum odor...'
|
327
340
|
```
|
328
341
|
|
@@ -334,6 +347,7 @@ For example:
|
|
334
347
|
```py
|
335
348
|
from typing import Literal, Optional
|
336
349
|
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
350
|
+
|
337
351
|
import pygame
|
338
352
|
|
339
353
|
def render_wrap(
|
@@ -349,9 +363,9 @@ def render_wrap(
|
|
349
363
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
350
364
|
placeholder: str = '...',
|
351
365
|
max_lines: Optional[int] = None,
|
352
|
-
|
366
|
+
empty_lines: bool = True,
|
353
367
|
minimum_width: bool = True,
|
354
|
-
|
368
|
+
excess_word_separator: bool = False,
|
355
369
|
justify_last_line: bool = False,
|
356
370
|
break_on_hyphens: bool = True
|
357
371
|
|
@@ -365,13 +379,13 @@ def render_wrap(
|
|
365
379
|
alignment=alignment,
|
366
380
|
placeholder=placeholder,
|
367
381
|
max_lines=max_lines,
|
368
|
-
|
382
|
+
empty_lines=empty_lines,
|
369
383
|
minimum_width=minimum_width,
|
370
|
-
|
384
|
+
excess_word_separator=excess_word_separator,
|
371
385
|
justify_last_line=justify_last_line,
|
372
386
|
break_on_hyphens=break_on_hyphens,
|
373
387
|
return_details=True,
|
374
|
-
|
388
|
+
size_function=font.size
|
375
389
|
)
|
376
390
|
|
377
391
|
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
@@ -420,5 +434,5 @@ while running:
|
|
420
434
|
```py
|
421
435
|
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
422
436
|
|
423
|
-
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='
|
437
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='\u2026'))
|
424
438
|
```
|
@@ -1,21 +1,22 @@
|
|
1
1
|
# TxTWrap🔡
|
2
2
|
A tool for wrapping and filling text.🔨
|
3
3
|
|
4
|
-
Package version: **3.
|
4
|
+
Package version: **3.1.0** <br>
|
5
5
|
Python requires version: **>=3.0.0** <br>
|
6
6
|
Python stub file requires version: **>=3.5.0** <br>
|
7
7
|
|
8
8
|
- [`LOREM_IPSUM_WORDS`](#lorem-ipsum)
|
9
9
|
- [`LOREM_IPSUM_SENTENCES`](#lorem-ipsum)
|
10
10
|
- [`LOREM_IPSUM_PARAGRAPHS`](#lorem-ipsum)
|
11
|
-
- [`SEPARATOR_WHITESPACE`](#separators) (
|
12
|
-
- [`SEPARATOR_ESCAPE`](#separators) (
|
11
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (✅ Updated)
|
12
|
+
- [`SEPARATOR_ESCAPE`](#separators) (✅ Updated)
|
13
|
+
- [`SEPARATOR_NEWLINE`](#separators) (➕ New)
|
14
|
+
- [`SEPARATOR_NEWLINE_AND_BREAK`](#separators) (➕ New)
|
13
15
|
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
14
|
-
- [`sanitize`](#sanitizetext) (🛠️ Fixed)
|
15
16
|
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
16
|
-
- [`align`](#aligntext-return_detailsfalse) (
|
17
|
-
- [`fillstr`](#fillstrtext) (
|
18
|
-
- [`shorten`](#shortentext) (
|
17
|
+
- [`align`](#aligntext-return_detailsfalse) (✅ Updated)
|
18
|
+
- [`fillstr`](#fillstrtext) (✅ Updated)
|
19
|
+
- [`shorten`](#shortentext) (✅ Updated)
|
19
20
|
|
20
21
|
# Documents📄
|
21
22
|
This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
|
@@ -28,6 +29,7 @@ filling _monospace fonts_ but also for other font types, such as _Arial_, _Times
|
|
28
29
|
<h1></h1>
|
29
30
|
|
30
31
|
## Constants
|
32
|
+
_File: **txtwrap.constants**_
|
31
33
|
|
32
34
|
### Lorem ipsum
|
33
35
|
```py
|
@@ -46,16 +48,20 @@ A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used
|
|
46
48
|
```py
|
47
49
|
SEPARATOR_WHITESPACE
|
48
50
|
SEPARATOR_ESCAPE
|
51
|
+
SEPARATOR_NEWLINE
|
52
|
+
SEPARATOR_NEWLINE_AND_BREAK
|
49
53
|
```
|
50
54
|
A collection of separators that can be used to separate text.
|
51
|
-
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
52
|
-
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\
|
53
|
-
|
54
|
-
|
55
|
+
- `SEPARATOR_WHITESPACE` (for [`word_separator`](#word_separator)) regex contains whitespace characters.
|
56
|
+
- `SEPARATOR_ESCAPE` (for [`word_separator`](#word_separator)) regex contains whitespace characters including `'\a'`, `'\b'`, and `'\0'`.
|
57
|
+
- `SEPARATOR_NEWLINE` (for [`newline_separator`](#newline_separator)) regex contains newline characters.
|
58
|
+
- `SEPARATOR_NEWLINE_AND_BREAK` (for [`newline_separator`](#newline_separator)) regex contains newline characters including breaks tag `<br>`.
|
55
59
|
|
56
60
|
<h1></h1>
|
57
61
|
|
58
62
|
## `TextWrapper`
|
63
|
+
_File: **txtwrap.wrapper**_
|
64
|
+
|
59
65
|
```py
|
60
66
|
class TextWrapper:
|
61
67
|
|
@@ -66,15 +72,17 @@ class TextWrapper:
|
|
66
72
|
mode: Literal['mono', 'word'] = 'word',
|
67
73
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
68
74
|
placeholder: str = '...',
|
69
|
-
|
70
|
-
|
75
|
+
space_character: str = ' ',
|
76
|
+
newline_character: str = '\n',
|
77
|
+
word_separator: Optional[Union[str, Tuple[str, int]]] = None,
|
78
|
+
newline_separator: Optional[Union[str, Tuple[str, int]]] = None,
|
71
79
|
max_lines: Optional[int] = None,
|
72
|
-
|
80
|
+
empty_lines: bool = True,
|
73
81
|
minimum_width: bool = True,
|
74
|
-
|
82
|
+
excess_word_separator: bool = False,
|
75
83
|
justify_last_line: bool = False,
|
76
84
|
break_on_hyphens: bool = True,
|
77
|
-
|
85
|
+
size_function: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
78
86
|
) -> None
|
79
87
|
```
|
80
88
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -87,9 +95,6 @@ is equivalent to:
|
|
87
95
|
wrapper = TextWrapper()
|
88
96
|
wrapper.width = 100
|
89
97
|
```
|
90
|
-
You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
|
91
|
-
attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
|
92
|
-
as each attribute has type checking, which may reduce performance.
|
93
98
|
|
94
99
|
<h1></h1>
|
95
100
|
|
@@ -131,16 +136,29 @@ as each attribute has type checking, which may reduce performance.
|
|
131
136
|
|
132
137
|
<h1></h1>
|
133
138
|
|
134
|
-
#### **`
|
135
|
-
(Default: `' '`) The character used for padding.
|
139
|
+
#### **`space_character`**
|
140
|
+
(Default: `' '`) The character used for padding and for the word separator character.
|
141
|
+
|
142
|
+
<h1></h1>
|
143
|
+
|
144
|
+
#### **`newline_character`**
|
145
|
+
(Default: `'\n'`) The character used for line separator.
|
136
146
|
|
137
147
|
<h1></h1>
|
138
148
|
|
139
|
-
#### **`
|
149
|
+
#### **`word_separator`**
|
140
150
|
(Default: `None`) The character used to separate words.
|
141
151
|
- `None`: Uses whitespace as the separator.
|
142
|
-
- `str`:
|
143
|
-
- `
|
152
|
+
- `str`: Regex separator in split form.
|
153
|
+
- `Tuple[str, int]`: Regex separator in split form with flags.
|
154
|
+
|
155
|
+
<h1></h1>
|
156
|
+
|
157
|
+
#### **`newline_separator`**
|
158
|
+
(Default: `None`) The character used to separate lines.
|
159
|
+
- `None`: Uses standard newline as the separator.
|
160
|
+
- `str`: Regex separator in split form.
|
161
|
+
- `Tuple[str, int]`: Regex separator in split form with flags.
|
144
162
|
|
145
163
|
<h1></h1>
|
146
164
|
|
@@ -152,7 +170,7 @@ as each attribute has type checking, which may reduce performance.
|
|
152
170
|
|
153
171
|
<h1></h1>
|
154
172
|
|
155
|
-
#### **`
|
173
|
+
#### **`empty_lines`**
|
156
174
|
(Default: `True`) Retains empty lines in the wrapped text.
|
157
175
|
|
158
176
|
<h1></h1>
|
@@ -163,8 +181,8 @@ enabling this attribute removes unnecessary empty space.
|
|
163
181
|
|
164
182
|
<h1></h1>
|
165
183
|
|
166
|
-
#### **`
|
167
|
-
(Default: `False`) Removes
|
184
|
+
#### **`excess_word_separator`**
|
185
|
+
(Default: `False`) Removes excess separators from between words one at a time.
|
168
186
|
|
169
187
|
<h1></h1>
|
170
188
|
|
@@ -179,7 +197,7 @@ enabling this attribute removes unnecessary empty space.
|
|
179
197
|
|
180
198
|
<h1></h1>
|
181
199
|
|
182
|
-
#### **`
|
200
|
+
#### **`size_function`**
|
183
201
|
(Default: `None`) A function used to calculate the width and height or only the width of each string.
|
184
202
|
|
185
203
|
If the function calculates both width and height, it must return a tuple containing two values:
|
@@ -206,39 +224,29 @@ is equivalent to:
|
|
206
224
|
<h1></h1>
|
207
225
|
|
208
226
|
#### **`copy`**
|
209
|
-
Creates and returns a copy of the [`TextWrapper`](#textwrapper) object.
|
210
|
-
|
211
|
-
<h1></h1>
|
212
|
-
|
213
|
-
#### **`sanitize(text)`**
|
214
|
-
Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
|
215
|
-
character.
|
216
|
-
|
217
|
-
For example:
|
218
|
-
```py
|
219
|
-
>>> TextWrapper().sanitize("\tHello \nWorld!\r ")
|
220
|
-
'Hello World!'
|
221
|
-
```
|
227
|
+
Creates and returns a copy of the [`TextWrapper`](#textwrapper) object. (External function using `copy.copy`)
|
222
228
|
|
223
229
|
<h1></h1>
|
224
230
|
|
225
231
|
#### **`wrap(text, return_details=False)`**
|
226
232
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
227
233
|
- `'wrapped'`: A list of wrapped text fragments.
|
228
|
-
- `'start_lines'`: A
|
229
|
-
- `'end_lines'`: A
|
234
|
+
- `'start_lines'`: A list of indices marking the start of line.
|
235
|
+
- `'end_lines'`: A list of indices marking the end of line.
|
230
236
|
|
231
237
|
For example:
|
232
238
|
```py
|
233
|
-
>>>
|
234
|
-
['Lorem ipsum', '
|
235
|
-
>>>
|
236
|
-
>>>
|
237
|
-
|
238
|
-
>>>
|
239
|
-
'Lorem ipsum'
|
240
|
-
>>>
|
241
|
-
'
|
239
|
+
>>> txtwrap.wrap(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
240
|
+
['Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.']
|
241
|
+
>>> wrapped_info = txtwrap.wrap(txtwrap.LOREM_IPSUM_WORDS, width=20, return_details=True)
|
242
|
+
>>> start_lines = wrapped_info['start_lines']
|
243
|
+
>>> end_lines = wrapped_info['end_lines']
|
244
|
+
>>> wrapped_info
|
245
|
+
{'wrapped': ['Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': [1], 'end_lines': [3]}
|
246
|
+
>>> wrapped_info['wrapped'][start_lines[0] - 1]
|
247
|
+
'Lorem ipsum odor'
|
248
|
+
>>> wrapped_info['wrapped'][end_lines[0] - 1]
|
249
|
+
'adipiscing elit.'
|
242
250
|
```
|
243
251
|
|
244
252
|
<h1></h1>
|
@@ -246,26 +254,28 @@ For example:
|
|
246
254
|
#### **`align(text, return_details=False)`**
|
247
255
|
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
248
256
|
with its coordinates.
|
249
|
-
> Note: [`
|
257
|
+
> Note: [`size_function`](#size_function) must return both width and height.
|
250
258
|
|
251
259
|
If `return_details=True`, returns a dictionary containing:
|
252
260
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
253
261
|
- `'wrapped'`: A list of wrapped text fragments.
|
254
|
-
- `'start_lines'`: A
|
255
|
-
- `'end_lines'`: A
|
262
|
+
- `'start_lines'`: A list of indices marking the start of line.
|
263
|
+
- `'end_lines'`: A list of indices marking the end of line.
|
256
264
|
- `'size'`: A calculated text size.
|
257
265
|
|
258
266
|
For example:
|
259
267
|
```py
|
260
|
-
>>>
|
268
|
+
>>> txtwrap.align(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
261
269
|
[(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
|
262
|
-
>>>
|
263
|
-
>>>
|
270
|
+
>>> aligned_info = txtwrap.align(txtwrap.LOREM_IPSUM_WORDS, width=20, return_details=True)
|
271
|
+
>>> start_lines = aligned_info['start_lines']
|
272
|
+
>>> end_lines = aligned_info['end_lines']
|
273
|
+
>>> aligned_info
|
264
274
|
{'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
|
265
|
-
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines':
|
266
|
-
>>>
|
275
|
+
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': [1], 'end_lines': [3], 'size': (18, 3)}
|
276
|
+
>>> aligned_info['wrapped'][start_lines[0] - 1]
|
267
277
|
'Lorem ipsum odor'
|
268
|
-
>>>
|
278
|
+
>>> aligned_info['wrapped'][end_lines[0] - 1]
|
269
279
|
'adipiscing elit.'
|
270
280
|
```
|
271
281
|
|
@@ -273,12 +283,12 @@ For example:
|
|
273
283
|
|
274
284
|
#### **`fillstr(text)`**
|
275
285
|
Returns a string with wrapped text formatted for monospace fonts.
|
276
|
-
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`
|
286
|
+
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`size_function`](#size_function)
|
277
287
|
(size or just length) must return `int`, not `float`!
|
278
288
|
|
279
289
|
For example:
|
280
290
|
```py
|
281
|
-
>>> s =
|
291
|
+
>>> s = txtwrap.fillstr(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
282
292
|
>>> s
|
283
293
|
'Lorem ipsum odor \namet, consectetuer\nadipiscing elit. '
|
284
294
|
>>> print(s)
|
@@ -295,7 +305,7 @@ if truncated.
|
|
295
305
|
|
296
306
|
For example:
|
297
307
|
```py
|
298
|
-
>>>
|
308
|
+
>>> txtwrap.shorten(txtwrap.LOREM_IPSUM_WORDS, width=20)
|
299
309
|
'Lorem ipsum odor...'
|
300
310
|
```
|
301
311
|
|
@@ -307,6 +317,7 @@ For example:
|
|
307
317
|
```py
|
308
318
|
from typing import Literal, Optional
|
309
319
|
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
320
|
+
|
310
321
|
import pygame
|
311
322
|
|
312
323
|
def render_wrap(
|
@@ -322,9 +333,9 @@ def render_wrap(
|
|
322
333
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
323
334
|
placeholder: str = '...',
|
324
335
|
max_lines: Optional[int] = None,
|
325
|
-
|
336
|
+
empty_lines: bool = True,
|
326
337
|
minimum_width: bool = True,
|
327
|
-
|
338
|
+
excess_word_separator: bool = False,
|
328
339
|
justify_last_line: bool = False,
|
329
340
|
break_on_hyphens: bool = True
|
330
341
|
|
@@ -338,13 +349,13 @@ def render_wrap(
|
|
338
349
|
alignment=alignment,
|
339
350
|
placeholder=placeholder,
|
340
351
|
max_lines=max_lines,
|
341
|
-
|
352
|
+
empty_lines=empty_lines,
|
342
353
|
minimum_width=minimum_width,
|
343
|
-
|
354
|
+
excess_word_separator=excess_word_separator,
|
344
355
|
justify_last_line=justify_last_line,
|
345
356
|
break_on_hyphens=break_on_hyphens,
|
346
357
|
return_details=True,
|
347
|
-
|
358
|
+
size_function=font.size
|
348
359
|
)
|
349
360
|
|
350
361
|
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
@@ -393,5 +404,5 @@ while running:
|
|
393
404
|
```py
|
394
405
|
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
395
406
|
|
396
|
-
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='
|
407
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='\u2026'))
|
397
408
|
```
|