txtwrap 2.3.1__tar.gz → 3.0.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-2.3.1/txtwrap.egg-info → txtwrap-3.0.0}/PKG-INFO +98 -43
- {txtwrap-2.3.1 → txtwrap-3.0.0}/README.md +93 -40
- {txtwrap-2.3.1 → txtwrap-3.0.0}/setup.py +5 -3
- txtwrap-3.0.0/txtwrap/__init__.py +35 -0
- txtwrap-3.0.0/txtwrap/__init__.pyi +30 -0
- txtwrap-3.0.0/txtwrap/_utils.py +34 -0
- txtwrap-3.0.0/txtwrap/constants.py +33 -0
- txtwrap-3.0.0/txtwrap/constants.pyi +8 -0
- txtwrap-3.0.0/txtwrap/identities.py +3 -0
- txtwrap-3.0.0/txtwrap/identities.pyi +3 -0
- txtwrap-3.0.0/txtwrap/wrapper.py +640 -0
- txtwrap-2.3.1/txtwrap/__init__.pyi → txtwrap-3.0.0/txtwrap/wrapper.pyi +49 -51
- {txtwrap-2.3.1 → txtwrap-3.0.0/txtwrap.egg-info}/PKG-INFO +98 -43
- txtwrap-3.0.0/txtwrap.egg-info/SOURCES.txt +16 -0
- txtwrap-2.3.1/txtwrap/__init__.py +0 -24
- txtwrap-2.3.1/txtwrap/_txtwrap.py +0 -648
- txtwrap-2.3.1/txtwrap.egg-info/SOURCES.txt +0 -10
- {txtwrap-2.3.1 → txtwrap-3.0.0}/MANIFEST.in +0 -0
- {txtwrap-2.3.1 → txtwrap-3.0.0}/setup.cfg +0 -0
- {txtwrap-2.3.1 → txtwrap-3.0.0}/txtwrap.egg-info/dependency_links.txt +0 -0
- {txtwrap-2.3.1 → txtwrap-3.0.0}/txtwrap.egg-info/top_level.txt +0 -0
@@ -1,16 +1,18 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: txtwrap
|
3
|
-
Version:
|
3
|
+
Version: 3.0.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
9
|
Keywords: wrap,wrapper,wrapping,wrapped,text wrap,text wrapper,text wrapping,text wrapped
|
10
|
+
Classifier: Programming Language :: Python
|
10
11
|
Classifier: Programming Language :: Python :: 3
|
11
12
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
12
|
-
Classifier:
|
13
|
-
|
13
|
+
Classifier: Topic :: Text Processing
|
14
|
+
Classifier: Topic :: Text Processing :: Filters
|
15
|
+
Requires-Python: >=3.0
|
14
16
|
Description-Content-Type: text/markdown
|
15
17
|
Dynamic: author
|
16
18
|
Dynamic: author-email
|
@@ -26,15 +28,21 @@ Dynamic: summary
|
|
26
28
|
# TxTWrap🔡
|
27
29
|
A tool for wrapping and filling text.🔨
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
- `
|
34
|
-
- `
|
35
|
-
- `
|
36
|
-
- `
|
37
|
-
- `
|
31
|
+
Package version: **3.0.0** <br>
|
32
|
+
Python requires version: **>=3.0.0** <br>
|
33
|
+
Python stub file requires version: **>=3.5.0** <br>
|
34
|
+
|
35
|
+
- [`LOREM_IPSUM_WORDS`](#lorem-ipsum)
|
36
|
+
- [`LOREM_IPSUM_SENTENCES`](#lorem-ipsum)
|
37
|
+
- [`LOREM_IPSUM_PARAGRAPHS`](#lorem-ipsum)
|
38
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (➕ New)
|
39
|
+
- [`SEPARATOR_ESCAPE`](#separators) (➕ New)
|
40
|
+
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
41
|
+
- [`sanitize`](#sanitizetext) (🛠️ Fixed)
|
42
|
+
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
43
|
+
- [`align`](#aligntext-return_detailsfalse) (🛠️ Fixed)
|
44
|
+
- [`fillstr`](#fillstrtext) (🛠️ Fixed)
|
45
|
+
- [`shorten`](#shortentext) (🛠️ Fixed)
|
38
46
|
|
39
47
|
# Documents📄
|
40
48
|
This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
|
@@ -42,40 +50,58 @@ several useful functions, along with the [`TextWrapper`](#textwrapper), class th
|
|
42
50
|
|
43
51
|
The difference between [`txtwrap`](https://pypi.org/project/txtwrap) and
|
44
52
|
[`textwrap`](https://docs.python.org/3/library/textwrap.html) is that this module is designed not only for wrapping and
|
45
|
-
filling
|
53
|
+
filling _monospace fonts_ but also for other font types, such as _Arial_, _Times New Roman_, and more.
|
46
54
|
|
47
55
|
<h1></h1>
|
48
56
|
|
57
|
+
## Constants
|
58
|
+
|
59
|
+
### Lorem ipsum
|
49
60
|
```py
|
50
61
|
LOREM_IPSUM_WORDS
|
51
62
|
LOREM_IPSUM_SENTENCES
|
52
63
|
LOREM_IPSUM_PARAGRAPHS
|
53
64
|
```
|
54
|
-
A collection of words, sentences, and paragraphs that can be used as examples.
|
55
|
-
- `LOREM_IPSUM_WORDS` contains a short
|
65
|
+
A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used as examples.
|
66
|
+
- `LOREM_IPSUM_WORDS` contains a short sentence.
|
56
67
|
- `LOREM_IPSUM_SENTENCES` contains a slightly longer paragraph.
|
57
68
|
- `LOREM_IPSUM_PARAGRAPHS` contains several longer paragraphs.
|
58
69
|
|
59
70
|
<h1></h1>
|
60
71
|
|
72
|
+
### Separators
|
73
|
+
```py
|
74
|
+
SEPARATOR_WHITESPACE
|
75
|
+
SEPARATOR_ESCAPE
|
76
|
+
```
|
77
|
+
A collection of separators that can be used to separate text.
|
78
|
+
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
79
|
+
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\0'`, `'\a'`, and `'\b'`.
|
80
|
+
|
81
|
+
To use this, assign this constant to the [`separator`](#separator) parameter.
|
82
|
+
|
83
|
+
<h1></h1>
|
84
|
+
|
61
85
|
## `TextWrapper`
|
62
86
|
```py
|
63
87
|
class TextWrapper:
|
88
|
+
|
64
89
|
def __init__(
|
65
90
|
self,
|
66
91
|
width: Union[int, float] = 70,
|
67
92
|
line_padding: Union[int, float] = 0,
|
68
|
-
|
93
|
+
mode: Literal['mono', 'word'] = 'word',
|
69
94
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
70
95
|
placeholder: str = '...',
|
71
96
|
fillchar: str = ' ',
|
72
97
|
separator: Optional[Union[str, Iterable[str]]] = None,
|
73
98
|
max_lines: Optional[int] = None,
|
74
|
-
|
99
|
+
preserve_empty_lines: bool = True,
|
75
100
|
minimum_width: bool = True,
|
101
|
+
drop_separator: bool = False,
|
76
102
|
justify_last_line: bool = False,
|
77
103
|
break_on_hyphens: bool = True,
|
78
|
-
sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
104
|
+
sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
79
105
|
) -> None
|
80
106
|
```
|
81
107
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -108,16 +134,15 @@ as each attribute has type checking, which may reduce performance.
|
|
108
134
|
|
109
135
|
<h1></h1>
|
110
136
|
|
111
|
-
#### **`
|
112
|
-
(Default: `'word'`) The wrapping
|
113
|
-
- `'mono'`
|
114
|
-
- `'word'`
|
137
|
+
#### **`mode`**
|
138
|
+
(Default: `'word'`) The wrapping mode. Available options:
|
139
|
+
- `'mono'` make text wraps character by character.
|
140
|
+
- `'word'` make text wraps word by word.
|
115
141
|
|
116
142
|
<h1></h1>
|
117
143
|
|
118
144
|
#### **`alignment`**
|
119
|
-
(Default: `'left'`) The alignment of the wrapped text. Available options:
|
120
|
-
(`'fill'` or `'fill-left'`), `'fill-center'`, and `'fill-right'`.
|
145
|
+
(Default: `'left'`) The alignment of the wrapped text. Available options:
|
121
146
|
- `'left'`: Aligns text to the start of the line.
|
122
147
|
- `'center'`: Centers text within the line.
|
123
148
|
- `'right'`: Aligns text to the end of the line.
|
@@ -142,7 +167,7 @@ as each attribute has type checking, which may reduce performance.
|
|
142
167
|
(Default: `None`) The character used to separate words.
|
143
168
|
- `None`: Uses whitespace as the separator.
|
144
169
|
- `str`: Uses the specified character.
|
145
|
-
- `Iterable`: Uses multiple specified characters.
|
170
|
+
- `Iterable[str]`: Uses multiple specified characters.
|
146
171
|
|
147
172
|
<h1></h1>
|
148
173
|
|
@@ -154,7 +179,7 @@ as each attribute has type checking, which may reduce performance.
|
|
154
179
|
|
155
180
|
<h1></h1>
|
156
181
|
|
157
|
-
#### **`
|
182
|
+
#### **`preserve_empty_lines`**
|
158
183
|
(Default: `True`) Retains empty lines in the wrapped text.
|
159
184
|
|
160
185
|
<h1></h1>
|
@@ -165,6 +190,11 @@ enabling this attribute removes unnecessary empty space.
|
|
165
190
|
|
166
191
|
<h1></h1>
|
167
192
|
|
193
|
+
#### **`drop_separator`**
|
194
|
+
(Default: `False`) Removes the separator between more than one word at a time.
|
195
|
+
|
196
|
+
<h1></h1>
|
197
|
+
|
168
198
|
#### **`justify_last_line`**
|
169
199
|
(Default: `False`) Determines whether the last line should also be justified
|
170
200
|
(applies only to `fill-...` alignments).
|
@@ -189,6 +219,17 @@ If the function calculates only the width, it must return a single value of type
|
|
189
219
|
|
190
220
|
### Methods of [`TextWrapper`](#textwrapper):
|
191
221
|
|
222
|
+
> Note: All methods can be called outside the [`TextWrapper`](#textwrapper) like external functions.
|
223
|
+
|
224
|
+
For example:
|
225
|
+
```py
|
226
|
+
>>> txtwrap.TextWrapper(20, placeholder='[...]').shorten("Hello World!")
|
227
|
+
```
|
228
|
+
is equivalent to:
|
229
|
+
```py
|
230
|
+
>>> txtwrap.shorten("Hello World!", 20, placeholder='[...]')
|
231
|
+
```
|
232
|
+
|
192
233
|
<h1></h1>
|
193
234
|
|
194
235
|
#### **`copy`**
|
@@ -202,7 +243,7 @@ character.
|
|
202
243
|
|
203
244
|
For example:
|
204
245
|
```py
|
205
|
-
>>> TextWrapper().sanitize("\tHello
|
246
|
+
>>> TextWrapper().sanitize("\tHello \nWorld!\r ")
|
206
247
|
'Hello World!'
|
207
248
|
```
|
208
249
|
|
@@ -211,44 +252,56 @@ For example:
|
|
211
252
|
#### **`wrap(text, return_details=False)`**
|
212
253
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
213
254
|
- `'wrapped'`: A list of wrapped text fragments.
|
214
|
-
- `'
|
255
|
+
- `'start_lines'`: A set of indices marking the start of line.
|
256
|
+
- `'end_lines'`: A set of indices marking the end of line.
|
215
257
|
|
216
258
|
For example:
|
217
259
|
```py
|
218
260
|
>>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS)
|
219
261
|
['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.']
|
220
|
-
>>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
|
221
|
-
|
262
|
+
>>> info = TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
|
263
|
+
>>> info
|
264
|
+
{'wrapped': ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.'], 'start_lines': {1}, 'end_lines': {5}}
|
265
|
+
>>> info['wrapped'][next(iter(info['start_lines'])) - 1]
|
266
|
+
'Lorem ipsum'
|
267
|
+
>>> info['wrapped'][next(iter(info['end_lines'])) - 1]
|
268
|
+
'elit.'
|
222
269
|
```
|
223
270
|
|
224
271
|
<h1></h1>
|
225
272
|
|
226
273
|
#### **`align(text, return_details=False)`**
|
227
|
-
Returns a list of tuples, where each tuple contains `(
|
228
|
-
coordinates.
|
274
|
+
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
275
|
+
with its coordinates.
|
229
276
|
> Note: [`sizefunc`](#sizefunc) must return both width and height.
|
230
277
|
|
231
278
|
If `return_details=True`, returns a dictionary containing:
|
232
279
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
233
|
-
- `'wrapped'`:
|
234
|
-
- `'
|
235
|
-
- `'
|
280
|
+
- `'wrapped'`: A list of wrapped text fragments.
|
281
|
+
- `'start_lines'`: A set of indices marking the start of line.
|
282
|
+
- `'end_lines'`: A set of indices marking the end of line.
|
283
|
+
- `'size'`: A calculated text size.
|
236
284
|
|
237
285
|
For example:
|
238
286
|
```py
|
239
287
|
>>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS)
|
240
288
|
[(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
|
241
|
-
>>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
|
289
|
+
>>> info = TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
|
290
|
+
>>> info
|
242
291
|
{'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
|
243
|
-
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], '
|
292
|
+
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': {1}, 'end_lines': {3}, 'size': (18, 3)}
|
293
|
+
>>> info['wrapped'][next(iter(info['start_lines'])) - 1]
|
294
|
+
'Lorem ipsum odor'
|
295
|
+
>>> info['wrapped'][next(iter(info['end_lines'])) - 1]
|
296
|
+
'adipiscing elit.'
|
244
297
|
```
|
245
298
|
|
246
299
|
<h1></h1>
|
247
300
|
|
248
301
|
#### **`fillstr(text)`**
|
249
302
|
Returns a string with wrapped text formatted for monospace fonts.
|
250
|
-
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc)
|
251
|
-
not `float`!
|
303
|
+
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc)
|
304
|
+
(size or just length) must return `int`, not `float`!
|
252
305
|
|
253
306
|
For example:
|
254
307
|
```py
|
@@ -292,12 +345,13 @@ def render_wrap(
|
|
292
345
|
color: pygame.Color,
|
293
346
|
background: Optional[pygame.Color] = None,
|
294
347
|
line_padding: int = 0,
|
295
|
-
|
348
|
+
wrap_mode: Literal['word', 'mono'] = 'word',
|
296
349
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
297
350
|
placeholder: str = '...',
|
298
351
|
max_lines: Optional[int] = None,
|
299
|
-
|
352
|
+
preserve_empty_lines: bool = True,
|
300
353
|
minimum_width: bool = True,
|
354
|
+
drop_separator: bool = False,
|
301
355
|
justify_last_line: bool = False,
|
302
356
|
break_on_hyphens: bool = True
|
303
357
|
|
@@ -307,12 +361,13 @@ def render_wrap(
|
|
307
361
|
text=text,
|
308
362
|
width=width,
|
309
363
|
line_padding=line_padding,
|
310
|
-
|
364
|
+
mode=wrap_mode,
|
311
365
|
alignment=alignment,
|
312
366
|
placeholder=placeholder,
|
313
367
|
max_lines=max_lines,
|
314
|
-
|
368
|
+
preserve_empty_lines=preserve_empty_lines,
|
315
369
|
minimum_width=minimum_width,
|
370
|
+
drop_separator=drop_separator,
|
316
371
|
justify_last_line=justify_last_line,
|
317
372
|
break_on_hyphens=break_on_hyphens,
|
318
373
|
return_details=True,
|
@@ -1,15 +1,21 @@
|
|
1
1
|
# TxTWrap🔡
|
2
2
|
A tool for wrapping and filling text.🔨
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
- `
|
9
|
-
- `
|
10
|
-
- `
|
11
|
-
- `
|
12
|
-
- `
|
4
|
+
Package version: **3.0.0** <br>
|
5
|
+
Python requires version: **>=3.0.0** <br>
|
6
|
+
Python stub file requires version: **>=3.5.0** <br>
|
7
|
+
|
8
|
+
- [`LOREM_IPSUM_WORDS`](#lorem-ipsum)
|
9
|
+
- [`LOREM_IPSUM_SENTENCES`](#lorem-ipsum)
|
10
|
+
- [`LOREM_IPSUM_PARAGRAPHS`](#lorem-ipsum)
|
11
|
+
- [`SEPARATOR_WHITESPACE`](#separators) (➕ New)
|
12
|
+
- [`SEPARATOR_ESCAPE`](#separators) (➕ New)
|
13
|
+
- [`TextWrapper`](#textwrapper) (✅ Updated)
|
14
|
+
- [`sanitize`](#sanitizetext) (🛠️ Fixed)
|
15
|
+
- [`wrap`](#wraptext-return_detailsfalse) (✅ Updated)
|
16
|
+
- [`align`](#aligntext-return_detailsfalse) (🛠️ Fixed)
|
17
|
+
- [`fillstr`](#fillstrtext) (🛠️ Fixed)
|
18
|
+
- [`shorten`](#shortentext) (🛠️ Fixed)
|
13
19
|
|
14
20
|
# Documents📄
|
15
21
|
This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
|
@@ -17,40 +23,58 @@ several useful functions, along with the [`TextWrapper`](#textwrapper), class th
|
|
17
23
|
|
18
24
|
The difference between [`txtwrap`](https://pypi.org/project/txtwrap) and
|
19
25
|
[`textwrap`](https://docs.python.org/3/library/textwrap.html) is that this module is designed not only for wrapping and
|
20
|
-
filling
|
26
|
+
filling _monospace fonts_ but also for other font types, such as _Arial_, _Times New Roman_, and more.
|
21
27
|
|
22
28
|
<h1></h1>
|
23
29
|
|
30
|
+
## Constants
|
31
|
+
|
32
|
+
### Lorem ipsum
|
24
33
|
```py
|
25
34
|
LOREM_IPSUM_WORDS
|
26
35
|
LOREM_IPSUM_SENTENCES
|
27
36
|
LOREM_IPSUM_PARAGRAPHS
|
28
37
|
```
|
29
|
-
A collection of words, sentences, and paragraphs that can be used as examples.
|
30
|
-
- `LOREM_IPSUM_WORDS` contains a short
|
38
|
+
A _Lorem Ipsum_ collection of words, sentences, and paragraphs that can be used as examples.
|
39
|
+
- `LOREM_IPSUM_WORDS` contains a short sentence.
|
31
40
|
- `LOREM_IPSUM_SENTENCES` contains a slightly longer paragraph.
|
32
41
|
- `LOREM_IPSUM_PARAGRAPHS` contains several longer paragraphs.
|
33
42
|
|
34
43
|
<h1></h1>
|
35
44
|
|
45
|
+
### Separators
|
46
|
+
```py
|
47
|
+
SEPARATOR_WHITESPACE
|
48
|
+
SEPARATOR_ESCAPE
|
49
|
+
```
|
50
|
+
A collection of separators that can be used to separate text.
|
51
|
+
- `SEPARATOR_WHITESPACE` contains whitespace characters.
|
52
|
+
- `SEPARATOR_ESCAPE` contains whitespace characters including `'\0'`, `'\a'`, and `'\b'`.
|
53
|
+
|
54
|
+
To use this, assign this constant to the [`separator`](#separator) parameter.
|
55
|
+
|
56
|
+
<h1></h1>
|
57
|
+
|
36
58
|
## `TextWrapper`
|
37
59
|
```py
|
38
60
|
class TextWrapper:
|
61
|
+
|
39
62
|
def __init__(
|
40
63
|
self,
|
41
64
|
width: Union[int, float] = 70,
|
42
65
|
line_padding: Union[int, float] = 0,
|
43
|
-
|
66
|
+
mode: Literal['mono', 'word'] = 'word',
|
44
67
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
45
68
|
placeholder: str = '...',
|
46
69
|
fillchar: str = ' ',
|
47
70
|
separator: Optional[Union[str, Iterable[str]]] = None,
|
48
71
|
max_lines: Optional[int] = None,
|
49
|
-
|
72
|
+
preserve_empty_lines: bool = True,
|
50
73
|
minimum_width: bool = True,
|
74
|
+
drop_separator: bool = False,
|
51
75
|
justify_last_line: bool = False,
|
52
76
|
break_on_hyphens: bool = True,
|
53
|
-
sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
77
|
+
sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None
|
54
78
|
) -> None
|
55
79
|
```
|
56
80
|
A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
|
@@ -83,16 +107,15 @@ as each attribute has type checking, which may reduce performance.
|
|
83
107
|
|
84
108
|
<h1></h1>
|
85
109
|
|
86
|
-
#### **`
|
87
|
-
(Default: `'word'`) The wrapping
|
88
|
-
- `'mono'`
|
89
|
-
- `'word'`
|
110
|
+
#### **`mode`**
|
111
|
+
(Default: `'word'`) The wrapping mode. Available options:
|
112
|
+
- `'mono'` make text wraps character by character.
|
113
|
+
- `'word'` make text wraps word by word.
|
90
114
|
|
91
115
|
<h1></h1>
|
92
116
|
|
93
117
|
#### **`alignment`**
|
94
|
-
(Default: `'left'`) The alignment of the wrapped text. Available options:
|
95
|
-
(`'fill'` or `'fill-left'`), `'fill-center'`, and `'fill-right'`.
|
118
|
+
(Default: `'left'`) The alignment of the wrapped text. Available options:
|
96
119
|
- `'left'`: Aligns text to the start of the line.
|
97
120
|
- `'center'`: Centers text within the line.
|
98
121
|
- `'right'`: Aligns text to the end of the line.
|
@@ -117,7 +140,7 @@ as each attribute has type checking, which may reduce performance.
|
|
117
140
|
(Default: `None`) The character used to separate words.
|
118
141
|
- `None`: Uses whitespace as the separator.
|
119
142
|
- `str`: Uses the specified character.
|
120
|
-
- `Iterable`: Uses multiple specified characters.
|
143
|
+
- `Iterable[str]`: Uses multiple specified characters.
|
121
144
|
|
122
145
|
<h1></h1>
|
123
146
|
|
@@ -129,7 +152,7 @@ as each attribute has type checking, which may reduce performance.
|
|
129
152
|
|
130
153
|
<h1></h1>
|
131
154
|
|
132
|
-
#### **`
|
155
|
+
#### **`preserve_empty_lines`**
|
133
156
|
(Default: `True`) Retains empty lines in the wrapped text.
|
134
157
|
|
135
158
|
<h1></h1>
|
@@ -140,6 +163,11 @@ enabling this attribute removes unnecessary empty space.
|
|
140
163
|
|
141
164
|
<h1></h1>
|
142
165
|
|
166
|
+
#### **`drop_separator`**
|
167
|
+
(Default: `False`) Removes the separator between more than one word at a time.
|
168
|
+
|
169
|
+
<h1></h1>
|
170
|
+
|
143
171
|
#### **`justify_last_line`**
|
144
172
|
(Default: `False`) Determines whether the last line should also be justified
|
145
173
|
(applies only to `fill-...` alignments).
|
@@ -164,6 +192,17 @@ If the function calculates only the width, it must return a single value of type
|
|
164
192
|
|
165
193
|
### Methods of [`TextWrapper`](#textwrapper):
|
166
194
|
|
195
|
+
> Note: All methods can be called outside the [`TextWrapper`](#textwrapper) like external functions.
|
196
|
+
|
197
|
+
For example:
|
198
|
+
```py
|
199
|
+
>>> txtwrap.TextWrapper(20, placeholder='[...]').shorten("Hello World!")
|
200
|
+
```
|
201
|
+
is equivalent to:
|
202
|
+
```py
|
203
|
+
>>> txtwrap.shorten("Hello World!", 20, placeholder='[...]')
|
204
|
+
```
|
205
|
+
|
167
206
|
<h1></h1>
|
168
207
|
|
169
208
|
#### **`copy`**
|
@@ -177,7 +216,7 @@ character.
|
|
177
216
|
|
178
217
|
For example:
|
179
218
|
```py
|
180
|
-
>>> TextWrapper().sanitize("\tHello
|
219
|
+
>>> TextWrapper().sanitize("\tHello \nWorld!\r ")
|
181
220
|
'Hello World!'
|
182
221
|
```
|
183
222
|
|
@@ -186,44 +225,56 @@ For example:
|
|
186
225
|
#### **`wrap(text, return_details=False)`**
|
187
226
|
Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
|
188
227
|
- `'wrapped'`: A list of wrapped text fragments.
|
189
|
-
- `'
|
228
|
+
- `'start_lines'`: A set of indices marking the start of line.
|
229
|
+
- `'end_lines'`: A set of indices marking the end of line.
|
190
230
|
|
191
231
|
For example:
|
192
232
|
```py
|
193
233
|
>>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS)
|
194
234
|
['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.']
|
195
|
-
>>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
|
196
|
-
|
235
|
+
>>> info = TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
|
236
|
+
>>> info
|
237
|
+
{'wrapped': ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.'], 'start_lines': {1}, 'end_lines': {5}}
|
238
|
+
>>> info['wrapped'][next(iter(info['start_lines'])) - 1]
|
239
|
+
'Lorem ipsum'
|
240
|
+
>>> info['wrapped'][next(iter(info['end_lines'])) - 1]
|
241
|
+
'elit.'
|
197
242
|
```
|
198
243
|
|
199
244
|
<h1></h1>
|
200
245
|
|
201
246
|
#### **`align(text, return_details=False)`**
|
202
|
-
Returns a list of tuples, where each tuple contains `(
|
203
|
-
coordinates.
|
247
|
+
Returns a list of tuples, where each tuple contains `(xPosition, yPosition, text)`, representing the wrapped text along
|
248
|
+
with its coordinates.
|
204
249
|
> Note: [`sizefunc`](#sizefunc) must return both width and height.
|
205
250
|
|
206
251
|
If `return_details=True`, returns a dictionary containing:
|
207
252
|
- `'aligned'`: A list of wrapped text with coordinate data.
|
208
|
-
- `'wrapped'`:
|
209
|
-
- `'
|
210
|
-
- `'
|
253
|
+
- `'wrapped'`: A list of wrapped text fragments.
|
254
|
+
- `'start_lines'`: A set of indices marking the start of line.
|
255
|
+
- `'end_lines'`: A set of indices marking the end of line.
|
256
|
+
- `'size'`: A calculated text size.
|
211
257
|
|
212
258
|
For example:
|
213
259
|
```py
|
214
260
|
>>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS)
|
215
261
|
[(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
|
216
|
-
>>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
|
262
|
+
>>> info = TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
|
263
|
+
>>> info
|
217
264
|
{'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
|
218
|
-
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], '
|
265
|
+
'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'start_lines': {1}, 'end_lines': {3}, 'size': (18, 3)}
|
266
|
+
>>> info['wrapped'][next(iter(info['start_lines'])) - 1]
|
267
|
+
'Lorem ipsum odor'
|
268
|
+
>>> info['wrapped'][next(iter(info['end_lines'])) - 1]
|
269
|
+
'adipiscing elit.'
|
219
270
|
```
|
220
271
|
|
221
272
|
<h1></h1>
|
222
273
|
|
223
274
|
#### **`fillstr(text)`**
|
224
275
|
Returns a string with wrapped text formatted for monospace fonts.
|
225
|
-
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc)
|
226
|
-
not `float`!
|
276
|
+
> Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc)
|
277
|
+
(size or just length) must return `int`, not `float`!
|
227
278
|
|
228
279
|
For example:
|
229
280
|
```py
|
@@ -267,12 +318,13 @@ def render_wrap(
|
|
267
318
|
color: pygame.Color,
|
268
319
|
background: Optional[pygame.Color] = None,
|
269
320
|
line_padding: int = 0,
|
270
|
-
|
321
|
+
wrap_mode: Literal['word', 'mono'] = 'word',
|
271
322
|
alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
|
272
323
|
placeholder: str = '...',
|
273
324
|
max_lines: Optional[int] = None,
|
274
|
-
|
325
|
+
preserve_empty_lines: bool = True,
|
275
326
|
minimum_width: bool = True,
|
327
|
+
drop_separator: bool = False,
|
276
328
|
justify_last_line: bool = False,
|
277
329
|
break_on_hyphens: bool = True
|
278
330
|
|
@@ -282,12 +334,13 @@ def render_wrap(
|
|
282
334
|
text=text,
|
283
335
|
width=width,
|
284
336
|
line_padding=line_padding,
|
285
|
-
|
337
|
+
mode=wrap_mode,
|
286
338
|
alignment=alignment,
|
287
339
|
placeholder=placeholder,
|
288
340
|
max_lines=max_lines,
|
289
|
-
|
341
|
+
preserve_empty_lines=preserve_empty_lines,
|
290
342
|
minimum_width=minimum_width,
|
343
|
+
drop_separator=drop_separator,
|
291
344
|
justify_last_line=justify_last_line,
|
292
345
|
break_on_hyphens=break_on_hyphens,
|
293
346
|
return_details=True,
|
@@ -5,7 +5,7 @@ with open('README.md', encoding='utf-8') as readme:
|
|
5
5
|
|
6
6
|
setup(
|
7
7
|
name='txtwrap',
|
8
|
-
version='
|
8
|
+
version='3.0.0',
|
9
9
|
description='A tool for wrapping and filling text.',
|
10
10
|
long_description=long_description,
|
11
11
|
long_description_content_type='text/markdown',
|
@@ -13,13 +13,15 @@ setup(
|
|
13
13
|
author_email='azzammuhyala@gmail.com',
|
14
14
|
url='https://github.com/azzammuhyala/txtwrap',
|
15
15
|
license='MIT',
|
16
|
-
python_requires='>=3.
|
16
|
+
python_requires='>=3.0',
|
17
17
|
packages=find_packages(),
|
18
18
|
include_package_data=True,
|
19
19
|
keywords=['wrap', 'wrapper', 'wrapping', 'wrapped', 'text wrap', 'text wrapper', 'text wrapping', 'text wrapped'],
|
20
20
|
classifiers=[
|
21
|
+
'Programming Language :: Python',
|
21
22
|
'Programming Language :: Python :: 3',
|
22
23
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
23
|
-
'
|
24
|
+
'Topic :: Text Processing',
|
25
|
+
'Topic :: Text Processing :: Filters'
|
24
26
|
]
|
25
27
|
)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"""
|
2
|
+
A tool for wrapping and filling text.
|
3
|
+
"""
|
4
|
+
|
5
|
+
# Supports only in Python>=3.0.0
|
6
|
+
|
7
|
+
from . import constants as constants
|
8
|
+
from . import identities as identities
|
9
|
+
from . import wrapper as wrapper
|
10
|
+
|
11
|
+
from .identities import __version__, __author__, __license__
|
12
|
+
|
13
|
+
from .constants import (
|
14
|
+
LOREM_IPSUM_WORDS, LOREM_IPSUM_SENTENCES, LOREM_IPSUM_PARAGRAPHS,
|
15
|
+
SEPARATOR_WHITESPACE, SEPARATOR_ESCAPE
|
16
|
+
)
|
17
|
+
|
18
|
+
from .wrapper import (
|
19
|
+
TextWrapper,
|
20
|
+
sanitize, wrap, align, fillstr, shorten
|
21
|
+
)
|
22
|
+
|
23
|
+
__all__ = [
|
24
|
+
'LOREM_IPSUM_WORDS',
|
25
|
+
'LOREM_IPSUM_SENTENCES',
|
26
|
+
'LOREM_IPSUM_PARAGRAPHS',
|
27
|
+
'SEPARATOR_WHITESPACE',
|
28
|
+
'SEPARATOR_ESCAPE',
|
29
|
+
'TextWrapper',
|
30
|
+
'sanitize',
|
31
|
+
'wrap',
|
32
|
+
'align',
|
33
|
+
'fillstr',
|
34
|
+
'shorten'
|
35
|
+
]
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Define the packages
|
2
|
+
|
3
|
+
from . import constants as constants
|
4
|
+
from . import identities as identities
|
5
|
+
from . import wrapper as wrapper
|
6
|
+
|
7
|
+
# Define the main variables, functions, classes to be exported
|
8
|
+
|
9
|
+
from .constants import (
|
10
|
+
LOREM_IPSUM_WORDS as LOREM_IPSUM_WORDS,
|
11
|
+
LOREM_IPSUM_SENTENCES as LOREM_IPSUM_SENTENCES,
|
12
|
+
LOREM_IPSUM_PARAGRAPHS as LOREM_IPSUM_PARAGRAPHS,
|
13
|
+
SEPARATOR_WHITESPACE as SEPARATOR_WHITESPACE,
|
14
|
+
SEPARATOR_ESCAPE as SEPARATOR_ESCAPE
|
15
|
+
)
|
16
|
+
|
17
|
+
from .identities import (
|
18
|
+
__version__ as __version__,
|
19
|
+
__author__ as __author__,
|
20
|
+
__license__ as __license__
|
21
|
+
)
|
22
|
+
|
23
|
+
from .wrapper import (
|
24
|
+
TextWrapper as TextWrapper,
|
25
|
+
sanitize as sanitize,
|
26
|
+
wrap as wrap,
|
27
|
+
align as align,
|
28
|
+
fillstr as fillstr,
|
29
|
+
shorten as shorten
|
30
|
+
)
|