txtwrap 3.0.1__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.1/txtwrap.egg-info → txtwrap-3.1.0}/PKG-INFO +54 -50
- {txtwrap-3.0.1 → txtwrap-3.1.0}/README.md +53 -49
- {txtwrap-3.0.1 → txtwrap-3.1.0}/setup.py +1 -1
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap/__init__.py +5 -3
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap/__init__.pyi +3 -2
- txtwrap-3.1.0/txtwrap/_utils.py +78 -0
- {txtwrap-3.0.1 → 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.1 → txtwrap-3.1.0}/txtwrap/wrapper.pyi +71 -59
- {txtwrap-3.0.1 → txtwrap-3.1.0/txtwrap.egg-info}/PKG-INFO +54 -50
- txtwrap-3.0.1/txtwrap/_utils.py +0 -34
- txtwrap-3.0.1/txtwrap/constants.pyi +0 -8
- txtwrap-3.0.1/txtwrap/identities.py +0 -3
- txtwrap-3.0.1/txtwrap/wrapper.py +0 -638
- {txtwrap-3.0.1 → txtwrap-3.1.0}/MANIFEST.in +0 -0
- {txtwrap-3.0.1 → txtwrap-3.1.0}/setup.cfg +0 -0
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap/identities.pyi +0 -0
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap.egg-info/SOURCES.txt +0 -0
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap.egg-info/dependency_links.txt +0 -0
- {txtwrap-3.0.1 → txtwrap-3.1.0}/txtwrap.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: txtwrap
|
3
|
-
Version: 3.0
|
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
|
@@ -31,21 +31,22 @@ Dynamic: summary
|
|
31
31
|
# TxTWrap🔡
|
32
32
|
A tool for wrapping and filling text.🔨
|
33
33
|
|
34
|
-
Package version: **3.0
|
34
|
+
Package version: **3.1.0** <br>
|
35
35
|
Python requires version: **>=3.0.0** <br>
|
36
36
|
Python stub file requires version: **>=3.5.0** <br>
|
37
37
|
|
38
38
|
- [`LOREM_IPSUM_WORDS`](#lorem-ipsum)
|
39
39
|
- [`LOREM_IPSUM_SENTENCES`](#lorem-ipsum)
|
40
40
|
- [`LOREM_IPSUM_PARAGRAPHS`](#lorem-ipsum)
|
41
|
-
- [`SEPARATOR_WHITESPACE`](#separators)
|
42
|
-
- [`SEPARATOR_ESCAPE`](#separators)
|
43
|
-
- [`
|
44
|
-
- [`
|
45
|
-
- [`
|
46
|
-
- [`
|
47
|
-
- [`
|
48
|
-
- [`
|
41
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (✅ Updated)
|
42
|
+
- [`SEPARATOR_ESCAPE`](#separators) (✅ Updated)
|
43
|
+
- [`SEPARATOR_NEWLINE`](#separators) (➕ New)
|
44
|
+
- [`SEPARATOR_NEWLINE_AND_BREAK`](#separators) (➕ New)
|
45
|
+
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
46
|
+
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
47
|
+
- [`align`](#aligntext-return_detailsfalse) (✅ Updated)
|
48
|
+
- [`fillstr`](#fillstrtext) (✅ Updated)
|
49
|
+
- [`shorten`](#shortentext) (✅ Updated)
|
49
50
|
|
50
51
|
# Documents📄
|
51
52
|
This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
|
@@ -77,12 +78,14 @@ A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used
|
|
77
78
|
```py
|
78
79
|
SEPARATOR_WHITESPACE
|
79
80
|
SEPARATOR_ESCAPE
|
81
|
+
SEPARATOR_NEWLINE
|
82
|
+
SEPARATOR_NEWLINE_AND_BREAK
|
80
83
|
```
|
81
84
|
A collection of separators that can be used to separate text.
|
82
|
-
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
83
|
-
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\
|
84
|
-
|
85
|
-
|
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>`.
|
86
89
|
|
87
90
|
<h1></h1>
|
88
91
|
|
@@ -99,15 +102,17 @@ class TextWrapper:
|
|
99
102
|
mode: Literal['mono', 'word'] = 'word',
|
100
103
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
101
104
|
placeholder: str = '...',
|
102
|
-
|
103
|
-
|
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,
|
104
109
|
max_lines: Optional[int] = None,
|
105
|
-
|
110
|
+
empty_lines: bool = True,
|
106
111
|
minimum_width: bool = True,
|
107
|
-
|
112
|
+
excess_word_separator: bool = False,
|
108
113
|
justify_last_line: bool = False,
|
109
114
|
break_on_hyphens: bool = True,
|
110
|
-
|
115
|
+
size_function: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
111
116
|
) -> None
|
112
117
|
```
|
113
118
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -120,9 +125,6 @@ is equivalent to:
|
|
120
125
|
wrapper = TextWrapper()
|
121
126
|
wrapper.width = 100
|
122
127
|
```
|
123
|
-
You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
|
124
|
-
attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
|
125
|
-
as each attribute has type checking, which may reduce performance.
|
126
128
|
|
127
129
|
<h1></h1>
|
128
130
|
|
@@ -164,16 +166,29 @@ as each attribute has type checking, which may reduce performance.
|
|
164
166
|
|
165
167
|
<h1></h1>
|
166
168
|
|
167
|
-
#### **`
|
168
|
-
(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.
|
169
176
|
|
170
177
|
<h1></h1>
|
171
178
|
|
172
|
-
#### **`
|
179
|
+
#### **`word_separator`**
|
173
180
|
(Default: `None`) The character used to separate words.
|
174
181
|
- `None`: Uses whitespace as the separator.
|
175
|
-
- `str`:
|
176
|
-
- `
|
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.
|
177
192
|
|
178
193
|
<h1></h1>
|
179
194
|
|
@@ -185,7 +200,7 @@ as each attribute has type checking, which may reduce performance.
|
|
185
200
|
|
186
201
|
<h1></h1>
|
187
202
|
|
188
|
-
#### **`
|
203
|
+
#### **`empty_lines`**
|
189
204
|
(Default: `True`) Retains empty lines in the wrapped text.
|
190
205
|
|
191
206
|
<h1></h1>
|
@@ -196,7 +211,7 @@ enabling this attribute removes unnecessary empty space.
|
|
196
211
|
|
197
212
|
<h1></h1>
|
198
213
|
|
199
|
-
#### **`
|
214
|
+
#### **`excess_word_separator`**
|
200
215
|
(Default: `False`) Removes excess separators from between words one at a time.
|
201
216
|
|
202
217
|
<h1></h1>
|
@@ -212,7 +227,7 @@ enabling this attribute removes unnecessary empty space.
|
|
212
227
|
|
213
228
|
<h1></h1>
|
214
229
|
|
215
|
-
#### **`
|
230
|
+
#### **`size_function`**
|
216
231
|
(Default: `None`) A function used to calculate the width and height or only the width of each string.
|
217
232
|
|
218
233
|
If the function calculates both width and height, it must return a tuple containing two values:
|
@@ -243,18 +258,6 @@ Creates and returns a copy of the [`TextWrapper`](#textwrapper) object. (Externa
|
|
243
258
|
|
244
259
|
<h1></h1>
|
245
260
|
|
246
|
-
#### **`sanitize(text)`**
|
247
|
-
Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
|
248
|
-
character. (It doesn't matter whether [`drop_separator`](#drop_separator) is `False` or `True`)
|
249
|
-
|
250
|
-
For example:
|
251
|
-
```py
|
252
|
-
>>> txtwrap.sanitize("\tHello \nWorld!\r ")
|
253
|
-
'Hello World!'
|
254
|
-
```
|
255
|
-
|
256
|
-
<h1></h1>
|
257
|
-
|
258
261
|
#### **`wrap(text, return_details=False)`**
|
259
262
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
260
263
|
- `'wrapped'`: A list of wrapped text fragments.
|
@@ -281,7 +284,7 @@ For example:
|
|
281
284
|
#### **`align(text, return_details=False)`**
|
282
285
|
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
283
286
|
with its coordinates.
|
284
|
-
> Note: [`
|
287
|
+
> Note: [`size_function`](#size_function) must return both width and height.
|
285
288
|
|
286
289
|
If `return_details=True`, returns a dictionary containing:
|
287
290
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
@@ -310,7 +313,7 @@ For example:
|
|
310
313
|
|
311
314
|
#### **`fillstr(text)`**
|
312
315
|
Returns a string with wrapped text formatted for monospace fonts.
|
313
|
-
> 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)
|
314
317
|
(size or just length) must return `int`, not `float`!
|
315
318
|
|
316
319
|
For example:
|
@@ -344,6 +347,7 @@ For example:
|
|
344
347
|
```py
|
345
348
|
from typing import Literal, Optional
|
346
349
|
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
350
|
+
|
347
351
|
import pygame
|
348
352
|
|
349
353
|
def render_wrap(
|
@@ -359,9 +363,9 @@ def render_wrap(
|
|
359
363
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
360
364
|
placeholder: str = '...',
|
361
365
|
max_lines: Optional[int] = None,
|
362
|
-
|
366
|
+
empty_lines: bool = True,
|
363
367
|
minimum_width: bool = True,
|
364
|
-
|
368
|
+
excess_word_separator: bool = False,
|
365
369
|
justify_last_line: bool = False,
|
366
370
|
break_on_hyphens: bool = True
|
367
371
|
|
@@ -375,13 +379,13 @@ def render_wrap(
|
|
375
379
|
alignment=alignment,
|
376
380
|
placeholder=placeholder,
|
377
381
|
max_lines=max_lines,
|
378
|
-
|
382
|
+
empty_lines=empty_lines,
|
379
383
|
minimum_width=minimum_width,
|
380
|
-
|
384
|
+
excess_word_separator=excess_word_separator,
|
381
385
|
justify_last_line=justify_last_line,
|
382
386
|
break_on_hyphens=break_on_hyphens,
|
383
387
|
return_details=True,
|
384
|
-
|
388
|
+
size_function=font.size
|
385
389
|
)
|
386
390
|
|
387
391
|
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
@@ -430,5 +434,5 @@ while running:
|
|
430
434
|
```py
|
431
435
|
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
432
436
|
|
433
|
-
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='
|
437
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='\u2026'))
|
434
438
|
```
|
@@ -1,21 +1,22 @@
|
|
1
1
|
# TxTWrap🔡
|
2
2
|
A tool for wrapping and filling text.🔨
|
3
3
|
|
4
|
-
Package version: **3.0
|
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)
|
13
|
-
- [`
|
14
|
-
- [`
|
15
|
-
- [`
|
16
|
-
- [`
|
17
|
-
- [`
|
18
|
-
- [`
|
11
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (✅ Updated)
|
12
|
+
- [`SEPARATOR_ESCAPE`](#separators) (✅ Updated)
|
13
|
+
- [`SEPARATOR_NEWLINE`](#separators) (➕ New)
|
14
|
+
- [`SEPARATOR_NEWLINE_AND_BREAK`](#separators) (➕ New)
|
15
|
+
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
16
|
+
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
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
|
@@ -47,12 +48,14 @@ A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used
|
|
47
48
|
```py
|
48
49
|
SEPARATOR_WHITESPACE
|
49
50
|
SEPARATOR_ESCAPE
|
51
|
+
SEPARATOR_NEWLINE
|
52
|
+
SEPARATOR_NEWLINE_AND_BREAK
|
50
53
|
```
|
51
54
|
A collection of separators that can be used to separate text.
|
52
|
-
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
53
|
-
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\
|
54
|
-
|
55
|
-
|
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>`.
|
56
59
|
|
57
60
|
<h1></h1>
|
58
61
|
|
@@ -69,15 +72,17 @@ class TextWrapper:
|
|
69
72
|
mode: Literal['mono', 'word'] = 'word',
|
70
73
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
71
74
|
placeholder: str = '...',
|
72
|
-
|
73
|
-
|
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,
|
74
79
|
max_lines: Optional[int] = None,
|
75
|
-
|
80
|
+
empty_lines: bool = True,
|
76
81
|
minimum_width: bool = True,
|
77
|
-
|
82
|
+
excess_word_separator: bool = False,
|
78
83
|
justify_last_line: bool = False,
|
79
84
|
break_on_hyphens: bool = True,
|
80
|
-
|
85
|
+
size_function: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
81
86
|
) -> None
|
82
87
|
```
|
83
88
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -90,9 +95,6 @@ is equivalent to:
|
|
90
95
|
wrapper = TextWrapper()
|
91
96
|
wrapper.width = 100
|
92
97
|
```
|
93
|
-
You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
|
94
|
-
attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
|
95
|
-
as each attribute has type checking, which may reduce performance.
|
96
98
|
|
97
99
|
<h1></h1>
|
98
100
|
|
@@ -134,16 +136,29 @@ as each attribute has type checking, which may reduce performance.
|
|
134
136
|
|
135
137
|
<h1></h1>
|
136
138
|
|
137
|
-
#### **`
|
138
|
-
(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.
|
139
146
|
|
140
147
|
<h1></h1>
|
141
148
|
|
142
|
-
#### **`
|
149
|
+
#### **`word_separator`**
|
143
150
|
(Default: `None`) The character used to separate words.
|
144
151
|
- `None`: Uses whitespace as the separator.
|
145
|
-
- `str`:
|
146
|
-
- `
|
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.
|
147
162
|
|
148
163
|
<h1></h1>
|
149
164
|
|
@@ -155,7 +170,7 @@ as each attribute has type checking, which may reduce performance.
|
|
155
170
|
|
156
171
|
<h1></h1>
|
157
172
|
|
158
|
-
#### **`
|
173
|
+
#### **`empty_lines`**
|
159
174
|
(Default: `True`) Retains empty lines in the wrapped text.
|
160
175
|
|
161
176
|
<h1></h1>
|
@@ -166,7 +181,7 @@ enabling this attribute removes unnecessary empty space.
|
|
166
181
|
|
167
182
|
<h1></h1>
|
168
183
|
|
169
|
-
#### **`
|
184
|
+
#### **`excess_word_separator`**
|
170
185
|
(Default: `False`) Removes excess separators from between words one at a time.
|
171
186
|
|
172
187
|
<h1></h1>
|
@@ -182,7 +197,7 @@ enabling this attribute removes unnecessary empty space.
|
|
182
197
|
|
183
198
|
<h1></h1>
|
184
199
|
|
185
|
-
#### **`
|
200
|
+
#### **`size_function`**
|
186
201
|
(Default: `None`) A function used to calculate the width and height or only the width of each string.
|
187
202
|
|
188
203
|
If the function calculates both width and height, it must return a tuple containing two values:
|
@@ -213,18 +228,6 @@ Creates and returns a copy of the [`TextWrapper`](#textwrapper) object. (Externa
|
|
213
228
|
|
214
229
|
<h1></h1>
|
215
230
|
|
216
|
-
#### **`sanitize(text)`**
|
217
|
-
Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
|
218
|
-
character. (It doesn't matter whether [`drop_separator`](#drop_separator) is `False` or `True`)
|
219
|
-
|
220
|
-
For example:
|
221
|
-
```py
|
222
|
-
>>> txtwrap.sanitize("\tHello \nWorld!\r ")
|
223
|
-
'Hello World!'
|
224
|
-
```
|
225
|
-
|
226
|
-
<h1></h1>
|
227
|
-
|
228
231
|
#### **`wrap(text, return_details=False)`**
|
229
232
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
230
233
|
- `'wrapped'`: A list of wrapped text fragments.
|
@@ -251,7 +254,7 @@ For example:
|
|
251
254
|
#### **`align(text, return_details=False)`**
|
252
255
|
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
253
256
|
with its coordinates.
|
254
|
-
> Note: [`
|
257
|
+
> Note: [`size_function`](#size_function) must return both width and height.
|
255
258
|
|
256
259
|
If `return_details=True`, returns a dictionary containing:
|
257
260
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
@@ -280,7 +283,7 @@ For example:
|
|
280
283
|
|
281
284
|
#### **`fillstr(text)`**
|
282
285
|
Returns a string with wrapped text formatted for monospace fonts.
|
283
|
-
> 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)
|
284
287
|
(size or just length) must return `int`, not `float`!
|
285
288
|
|
286
289
|
For example:
|
@@ -314,6 +317,7 @@ For example:
|
|
314
317
|
```py
|
315
318
|
from typing import Literal, Optional
|
316
319
|
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
320
|
+
|
317
321
|
import pygame
|
318
322
|
|
319
323
|
def render_wrap(
|
@@ -329,9 +333,9 @@ def render_wrap(
|
|
329
333
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
330
334
|
placeholder: str = '...',
|
331
335
|
max_lines: Optional[int] = None,
|
332
|
-
|
336
|
+
empty_lines: bool = True,
|
333
337
|
minimum_width: bool = True,
|
334
|
-
|
338
|
+
excess_word_separator: bool = False,
|
335
339
|
justify_last_line: bool = False,
|
336
340
|
break_on_hyphens: bool = True
|
337
341
|
|
@@ -345,13 +349,13 @@ def render_wrap(
|
|
345
349
|
alignment=alignment,
|
346
350
|
placeholder=placeholder,
|
347
351
|
max_lines=max_lines,
|
348
|
-
|
352
|
+
empty_lines=empty_lines,
|
349
353
|
minimum_width=minimum_width,
|
350
|
-
|
354
|
+
excess_word_separator=excess_word_separator,
|
351
355
|
justify_last_line=justify_last_line,
|
352
356
|
break_on_hyphens=break_on_hyphens,
|
353
357
|
return_details=True,
|
354
|
-
|
358
|
+
size_function=font.size
|
355
359
|
)
|
356
360
|
|
357
361
|
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
@@ -400,5 +404,5 @@ while running:
|
|
400
404
|
```py
|
401
405
|
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
402
406
|
|
403
|
-
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='
|
407
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='\u2026'))
|
404
408
|
```
|
@@ -15,12 +15,13 @@ from .identities import __version__, __author__, __license__
|
|
15
15
|
|
16
16
|
from .constants import (
|
17
17
|
LOREM_IPSUM_WORDS, LOREM_IPSUM_SENTENCES, LOREM_IPSUM_PARAGRAPHS,
|
18
|
-
SEPARATOR_WHITESPACE, SEPARATOR_ESCAPE
|
18
|
+
SEPARATOR_WHITESPACE, SEPARATOR_ESCAPE,
|
19
|
+
SEPARATOR_NEWLINE, SEPARATOR_NEWLINE_AND_BREAK
|
19
20
|
)
|
20
21
|
|
21
22
|
from .wrapper import (
|
22
23
|
TextWrapper,
|
23
|
-
|
24
|
+
wrap, align, fillstr, shorten
|
24
25
|
)
|
25
26
|
|
26
27
|
__all__ = [
|
@@ -29,8 +30,9 @@ __all__ = [
|
|
29
30
|
'LOREM_IPSUM_PARAGRAPHS',
|
30
31
|
'SEPARATOR_WHITESPACE',
|
31
32
|
'SEPARATOR_ESCAPE',
|
33
|
+
'SEPARATOR_NEWLINE',
|
34
|
+
'SEPARATOR_NEWLINE_AND_BREAK',
|
32
35
|
'TextWrapper',
|
33
|
-
'sanitize',
|
34
36
|
'wrap',
|
35
37
|
'align',
|
36
38
|
'fillstr',
|
@@ -11,7 +11,9 @@ from .constants import (
|
|
11
11
|
LOREM_IPSUM_SENTENCES as LOREM_IPSUM_SENTENCES,
|
12
12
|
LOREM_IPSUM_PARAGRAPHS as LOREM_IPSUM_PARAGRAPHS,
|
13
13
|
SEPARATOR_WHITESPACE as SEPARATOR_WHITESPACE,
|
14
|
-
SEPARATOR_ESCAPE as SEPARATOR_ESCAPE
|
14
|
+
SEPARATOR_ESCAPE as SEPARATOR_ESCAPE,
|
15
|
+
SEPARATOR_NEWLINE as SEPARATOR_NEWLINE,
|
16
|
+
SEPARATOR_NEWLINE_AND_BREAK as SEPARATOR_NEWLINE_AND_BREAK
|
15
17
|
)
|
16
18
|
|
17
19
|
from .identities import (
|
@@ -22,7 +24,6 @@ from .identities import (
|
|
22
24
|
|
23
25
|
from .wrapper import (
|
24
26
|
TextWrapper as TextWrapper,
|
25
|
-
sanitize as sanitize,
|
26
27
|
wrap as wrap,
|
27
28
|
align as align,
|
28
29
|
fillstr as fillstr,
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import re
|
2
|
+
|
3
|
+
pdict = type('pdict', (dict,), {
|
4
|
+
'__repr__': lambda self : '{0}({1})'.format(type(self).__name__, dict.__repr__(self)),
|
5
|
+
'__getattr__': lambda self, key: self.get(key, None),
|
6
|
+
'__setattr__': dict.__setitem__,
|
7
|
+
'__delattr__': dict.__delitem__
|
8
|
+
})
|
9
|
+
|
10
|
+
unknown = type('unknown', (), {
|
11
|
+
'__repr__': lambda self : '<unknown>',
|
12
|
+
'__str__': lambda self : '<unknown>',
|
13
|
+
'__bool__': lambda self : False
|
14
|
+
})()
|
15
|
+
|
16
|
+
def align_left(aligned, text, width, text_width, offset_y):
|
17
|
+
aligned.append((0, offset_y, text))
|
18
|
+
|
19
|
+
def align_center(aligned, text, width, text_width, offset_y):
|
20
|
+
aligned.append(((width - text_width) / 2, offset_y, text))
|
21
|
+
|
22
|
+
def align_right(aligned, text, width, text_width, offset_y):
|
23
|
+
aligned.append((width - text_width, offset_y, text))
|
24
|
+
|
25
|
+
def fillstr_left(justified, text, width, text_width, fillchar):
|
26
|
+
justified.append(text + fillchar * (width - text_width))
|
27
|
+
|
28
|
+
def fillstr_center(justified, text, width, text_width, fillchar):
|
29
|
+
extra_space = width - text_width
|
30
|
+
left_space = extra_space // 2
|
31
|
+
justified.append(fillchar * left_space + text + fillchar * (extra_space - left_space))
|
32
|
+
|
33
|
+
def fillstr_right(justified, text, width, text_width, fillchar):
|
34
|
+
justified.append(fillchar * (width - text_width) + text)
|
35
|
+
|
36
|
+
def validate_size_function(func, test):
|
37
|
+
result = func(test)
|
38
|
+
|
39
|
+
if isinstance(result, tuple):
|
40
|
+
|
41
|
+
def wrapper(string):
|
42
|
+
result = func(string)
|
43
|
+
if not isinstance(result, tuple):
|
44
|
+
raise TypeError("size_function must be returned a tuple for size")
|
45
|
+
if len(result) != 2:
|
46
|
+
raise ValueError("size_function must be returned a tuple of length 2")
|
47
|
+
width, height = result
|
48
|
+
if not isinstance(width, (int, float)):
|
49
|
+
raise TypeError("size_function returned width must be a tuple of two integers or floats")
|
50
|
+
if not isinstance(height, (int, float)):
|
51
|
+
raise TypeError("size_function returned height must be a tuple of two integers or floats")
|
52
|
+
if width < 0:
|
53
|
+
raise ValueError("size_function returned width must be equal to or greater than 0")
|
54
|
+
if height < 0:
|
55
|
+
raise ValueError("size_function returned height must be equal to or greater than 0")
|
56
|
+
return result
|
57
|
+
|
58
|
+
return wrapper, lambda string : wrapper(string)[0]
|
59
|
+
|
60
|
+
elif isinstance(result, (int, float)):
|
61
|
+
|
62
|
+
def wrapper(string):
|
63
|
+
result = func(string)
|
64
|
+
if not isinstance(result, (int, float)):
|
65
|
+
raise TypeError("size_function (length) must be returned a integers or floats for width")
|
66
|
+
if result < 0:
|
67
|
+
raise ValueError("size_function (length) must be equal to or greater than 0")
|
68
|
+
return result
|
69
|
+
|
70
|
+
return None, wrapper
|
71
|
+
|
72
|
+
raise TypeError("size_function must be returned a tuple for size or a single value for width (length)")
|
73
|
+
|
74
|
+
hyphenate_pattern = r'''
|
75
|
+
(?<=-) # positive lookbehind: make sure there is a '-' before the current position
|
76
|
+
(?=(?!-).) # positive lookahead: make sure the character after is NOT '-' (avoid '--'), but still have one character
|
77
|
+
'''
|
78
|
+
split_hyphenated = re.compile(hyphenate_pattern, re.VERBOSE).split
|
@@ -1,3 +1,5 @@
|
|
1
|
+
import re
|
2
|
+
|
1
3
|
LOREM_IPSUM_WORDS = 'Lorem ipsum odor amet, consectetuer adipiscing elit.'
|
2
4
|
LOREM_IPSUM_SENTENCES = (
|
3
5
|
'Lorem ipsum odor amet, consectetuer adipiscing elit. In malesuada eros natoque urna felis diam aptent donec. Cubil'
|
@@ -29,5 +31,7 @@ LOREM_IPSUM_PARAGRAPHS = (
|
|
29
31
|
'aliquet proin. Commodo neque nibh; tempus ad tortor netus. Mattis ultricies nec maximus porttitor non mauris?'
|
30
32
|
)
|
31
33
|
|
32
|
-
SEPARATOR_WHITESPACE =
|
33
|
-
SEPARATOR_ESCAPE = SEPARATOR_WHITESPACE +
|
34
|
+
SEPARATOR_WHITESPACE = r' |\t|\n|\r|\v|\f'
|
35
|
+
SEPARATOR_ESCAPE = SEPARATOR_WHITESPACE + r'|\a|\x08|\0' # \x08 -> \b
|
36
|
+
SEPARATOR_NEWLINE = r'\r\n|\n|\r'
|
37
|
+
SEPARATOR_NEWLINE_AND_BREAK = (r'</?br\s*/?>|' + SEPARATOR_NEWLINE, re.IGNORECASE)
|