txtwrap 2.2.0__tar.gz → 2.3.1__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/PKG-INFO ADDED
@@ -0,0 +1,369 @@
1
+ Metadata-Version: 2.4
2
+ Name: txtwrap
3
+ Version: 2.3.1
4
+ Summary: A tool for wrapping and filling text.
5
+ Home-page: https://github.com/azzammuhyala/txtwrap
6
+ Author: azzammuhyala
7
+ Author-email: azzammuhyala@gmail.com
8
+ License: MIT
9
+ Keywords: wrap,wrapper,wrapping,wrapped,text wrap,text wrapper,text wrapping,text wrapped
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Requires-Python: >=3.3
14
+ Description-Content-Type: text/markdown
15
+ Dynamic: author
16
+ Dynamic: author-email
17
+ Dynamic: classifier
18
+ Dynamic: description
19
+ Dynamic: description-content-type
20
+ Dynamic: home-page
21
+ Dynamic: keywords
22
+ Dynamic: license
23
+ Dynamic: requires-python
24
+ Dynamic: summary
25
+
26
+ # TxTWrap🔡
27
+ A tool for wrapping and filling text.🔨
28
+
29
+ - `LOREM_IPSUM_WORDS`
30
+ - `LOREM_IPSUM_SENTENCES`
31
+ - `LOREM_IPSUM_PARAGRAPHS`
32
+ - `TextWrapper` (❇️ Fixed)
33
+ - `sanitize`
34
+ - `wrap`
35
+ - `align`
36
+ - `fillstr`
37
+ - `shorten`
38
+
39
+ # Documents📄
40
+ This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
41
+ several useful functions, along with the [`TextWrapper`](#textwrapper), class that handles all available functions.
42
+
43
+ The difference between [`txtwrap`](https://pypi.org/project/txtwrap) and
44
+ [`textwrap`](https://docs.python.org/3/library/textwrap.html) is that this module is designed not only for wrapping and
45
+ filling monospace fonts but also for other font types, such as Arial, Times New Roman, and more.
46
+
47
+ <h1></h1>
48
+
49
+ ```py
50
+ LOREM_IPSUM_WORDS
51
+ LOREM_IPSUM_SENTENCES
52
+ LOREM_IPSUM_PARAGRAPHS
53
+ ```
54
+ A collection of words, sentences, and paragraphs that can be used as examples.
55
+ - `LOREM_IPSUM_WORDS` contains a short Lorem Ipsum sentence.
56
+ - `LOREM_IPSUM_SENTENCES` contains a slightly longer paragraph.
57
+ - `LOREM_IPSUM_PARAGRAPHS` contains several longer paragraphs.
58
+
59
+ <h1></h1>
60
+
61
+ ## `TextWrapper`
62
+ ```py
63
+ class TextWrapper:
64
+ def __init__(
65
+ self,
66
+ width: Union[int, float] = 70,
67
+ line_padding: Union[int, float] = 0,
68
+ method: Literal['mono', 'word'] = 'word',
69
+ alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
70
+ placeholder: str = '...',
71
+ fillchar: str = ' ',
72
+ separator: Optional[Union[str, Iterable[str]]] = None,
73
+ max_lines: Optional[int] = None,
74
+ preserve_empty: bool = True,
75
+ minimum_width: bool = True,
76
+ justify_last_line: bool = False,
77
+ break_on_hyphens: bool = True,
78
+ sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None,
79
+ ) -> None
80
+ ```
81
+ A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
82
+ For example:
83
+ ```py
84
+ wrapper = TextWrapper(width=100)
85
+ ```
86
+ is equivalent to:
87
+ ```py
88
+ wrapper = TextWrapper()
89
+ wrapper.width = 100
90
+ ```
91
+ You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
92
+ attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
93
+ as each attribute has type checking, which may reduce performance.
94
+
95
+ <h1></h1>
96
+
97
+ ### Attributes of [`TextWrapper`](#textwrapper):
98
+
99
+ <h1></h1>
100
+
101
+ #### **`width`**
102
+ (Default: `70`) The maximum line length for wrapped text.
103
+
104
+ <h1></h1>
105
+
106
+ #### **`line_padding`**
107
+ (Default: `0`) The spacing between wrapped lines.
108
+
109
+ <h1></h1>
110
+
111
+ #### **`method`**
112
+ (Default: `'word'`) The wrapping method. Available options: `'mono'` and `'word'`.
113
+ - `'mono'` method wraps text character by character.
114
+ - `'word'` method wraps text word by word.
115
+
116
+ <h1></h1>
117
+
118
+ #### **`alignment`**
119
+ (Default: `'left'`) The alignment of the wrapped text. Available options: `'left'`, `'center'`, `'right'`,
120
+ (`'fill'` or `'fill-left'`), `'fill-center'`, and `'fill-right'`.
121
+ - `'left'`: Aligns text to the start of the line.
122
+ - `'center'`: Centers text within the line.
123
+ - `'right'`: Aligns text to the end of the line.
124
+ - `'fill'` or `'fill-left'`: Justifies text across the width but aligns single-word lines or the last line
125
+ (if [`justify_last_line`](#justify_last_line) is `False`) to the left.
126
+ - `'fill-center'` and `'fill-right'` work the same way as `'fill-left'`, aligning text according to their respective
127
+ names.
128
+
129
+ <h1></h1>
130
+
131
+ #### **`placeholder`**
132
+ (Default: `'...'`) The ellipsis used for truncating long lines.
133
+
134
+ <h1></h1>
135
+
136
+ #### **`fillchar`**
137
+ (Default: `' '`) The character used for padding.
138
+
139
+ <h1></h1>
140
+
141
+ #### **`separator`**
142
+ (Default: `None`) The character used to separate words.
143
+ - `None`: Uses whitespace as the separator.
144
+ - `str`: Uses the specified character.
145
+ - `Iterable`: Uses multiple specified characters.
146
+
147
+ <h1></h1>
148
+
149
+ #### **`max_lines`**
150
+ (Default: `None`) The maximum number of wrapped lines.
151
+ - `None`: No limit on the number of wrapped lines.
152
+ - `int`: Limits the number of wrapped lines to the specified value. (Ensure that [`width`](#width) is not smaller than
153
+ the length of [`placeholder`](#placeholder)).
154
+
155
+ <h1></h1>
156
+
157
+ #### **`preserve_empty`**
158
+ (Default: `True`) Retains empty lines in the wrapped text.
159
+
160
+ <h1></h1>
161
+
162
+ #### **`minimum_width`**
163
+ (Default: `True`) Uses the minimum required line width. Some wrapped lines may be shorter than the specified width, so
164
+ enabling this attribute removes unnecessary empty space.
165
+
166
+ <h1></h1>
167
+
168
+ #### **`justify_last_line`**
169
+ (Default: `False`) Determines whether the last line should also be justified
170
+ (applies only to `fill-...` alignments).
171
+
172
+ <h1></h1>
173
+
174
+ #### **`break_on_hyphens`**
175
+ (Default: `True`) Breaks words at hyphens (-). Example `'self-organization'` becomes `'self-'` and `'organization'`.
176
+
177
+ <h1></h1>
178
+
179
+ #### **`sizefunc`**
180
+ (Default: `None`) A function used to calculate the width and height or only the width of each string.
181
+
182
+ If the function calculates both width and height, it must return a tuple containing two values:
183
+ - The width and height of the string.
184
+ - Both values must be of type `int` or `float`.
185
+
186
+ If the function calculates only the width, it must return a single value of type `int` or `float`.
187
+
188
+ <h1></h1>
189
+
190
+ ### Methods of [`TextWrapper`](#textwrapper):
191
+
192
+ <h1></h1>
193
+
194
+ #### **`copy`**
195
+ Creates and returns a copy of the [`TextWrapper`](#textwrapper) object.
196
+
197
+ <h1></h1>
198
+
199
+ #### **`sanitize(text)`**
200
+ Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
201
+ character.
202
+
203
+ For example:
204
+ ```py
205
+ >>> TextWrapper().sanitize("\tHello World! ")
206
+ 'Hello World!'
207
+ ```
208
+
209
+ <h1></h1>
210
+
211
+ #### **`wrap(text, return_details=False)`**
212
+ Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
213
+ - `'wrapped'`: A list of wrapped text fragments.
214
+ - `'indiced'`: A set of indices marking line breaks (starting from `0`, like programming indices).
215
+
216
+ For example:
217
+ ```py
218
+ >>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS)
219
+ ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.']
220
+ >>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
221
+ {'wrapped': ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.'], 'indiced': {4}}
222
+ ```
223
+
224
+ <h1></h1>
225
+
226
+ #### **`align(text, return_details=False)`**
227
+ Returns a list of tuples, where each tuple contains `(x, y, text)`, representing the wrapped text along with its
228
+ coordinates.
229
+ > Note: [`sizefunc`](#sizefunc) must return both width and height.
230
+
231
+ If `return_details=True`, returns a dictionary containing:
232
+ - `'aligned'`: A list of wrapped text with coordinate data.
233
+ - `'wrapped'`: The result from wrap.
234
+ - `'indiced'`: The indices of line breaks.
235
+ - `'size'`: The calculated text size.
236
+
237
+ For example:
238
+ ```py
239
+ >>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS)
240
+ [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
241
+ >>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
242
+ {'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
243
+ 'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'indiced': {2}, 'size': (18, 3)}
244
+ ```
245
+
246
+ <h1></h1>
247
+
248
+ #### **`fillstr(text)`**
249
+ Returns a string with wrapped text formatted for monospace fonts.
250
+ > Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc) must return `int`,
251
+ not `float`!
252
+
253
+ For example:
254
+ ```py
255
+ >>> s = TextWrapper(width=20).fillstr(LOREM_IPSUM_WORDS)
256
+ >>> s
257
+ 'Lorem ipsum odor \namet, consectetuer\nadipiscing elit. '
258
+ >>> print(s)
259
+ Lorem ipsum odor
260
+ amet, consectetuer
261
+ adipiscing elit.
262
+ ```
263
+
264
+ <h1></h1>
265
+
266
+ #### **`shorten(text)`**
267
+ Returns a truncated string if its length exceeds [`width`](#width), appending [`placeholder`](#placeholder) at the end
268
+ if truncated.
269
+
270
+ For example:
271
+ ```py
272
+ >>> TextWrapper(width=20).shorten(LOREM_IPSUM_WORDS)
273
+ 'Lorem ipsum odor...'
274
+ ```
275
+
276
+ <h1></h1>
277
+
278
+ # Another examples❓
279
+
280
+ ## Render a wrap text in PyGame🎮
281
+ ```py
282
+ from typing import Literal, Optional
283
+ from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
284
+ import pygame
285
+
286
+ def render_wrap(
287
+
288
+ font: pygame.Font,
289
+ text: str,
290
+ width: int,
291
+ antialias: bool,
292
+ color: pygame.Color,
293
+ background: Optional[pygame.Color] = None,
294
+ line_padding: int = 0,
295
+ method: Literal['word', 'mono'] = 'word',
296
+ alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
297
+ placeholder: str = '...',
298
+ max_lines: Optional[int] = None,
299
+ preserve_empty: bool = True,
300
+ minimum_width: bool = True,
301
+ justify_last_line: bool = False,
302
+ break_on_hyphens: bool = True
303
+
304
+ ) -> pygame.Surface:
305
+
306
+ align_info = align(
307
+ text=text,
308
+ width=width,
309
+ line_padding=line_padding,
310
+ method=method,
311
+ alignment=alignment,
312
+ placeholder=placeholder,
313
+ max_lines=max_lines,
314
+ preserve_empty=preserve_empty,
315
+ minimum_width=minimum_width,
316
+ justify_last_line=justify_last_line,
317
+ break_on_hyphens=break_on_hyphens,
318
+ return_details=True,
319
+ sizefunc=font.size
320
+ )
321
+
322
+ surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
323
+
324
+ if background is not None:
325
+ surface.fill(background)
326
+
327
+ for x, y, text in align_info['aligned']:
328
+ surface.blit(font.render(text, antialias, color), (x, y))
329
+
330
+ return surface
331
+
332
+ # Example usage:
333
+ pygame.init()
334
+ pygame.display.set_caption("Lorem Ipsum")
335
+
336
+ running = True
337
+ width, height = 800, 600
338
+ screen = pygame.display.set_mode((width, height))
339
+ clock = pygame.time.Clock()
340
+
341
+ surface = render_wrap(
342
+ font=pygame.font.SysFont('Arial', 18),
343
+ text=LOREM_IPSUM_PARAGRAPHS,
344
+ width=width,
345
+ antialias=True,
346
+ color='#ffffff',
347
+ background='#303030',
348
+ alignment='fill'
349
+ )
350
+
351
+ width_surface, height_surface = surface.get_size()
352
+ pos = ((width - width_surface) / 2, (height - height_surface) / 2)
353
+
354
+ while running:
355
+ for event in pygame.event.get():
356
+ if event.type == pygame.QUIT:
357
+ running = False
358
+ screen.fill('#000000')
359
+ screen.blit(surface, pos)
360
+ pygame.display.flip()
361
+ clock.tick(60)
362
+ ```
363
+
364
+ ## Short a long text🔤
365
+ ```py
366
+ from txtwrap import shorten, LOREM_IPSUM_SENTENCES
367
+
368
+ print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='…'))
369
+ ```
@@ -0,0 +1,344 @@
1
+ # TxTWrap🔡
2
+ A tool for wrapping and filling text.🔨
3
+
4
+ - `LOREM_IPSUM_WORDS`
5
+ - `LOREM_IPSUM_SENTENCES`
6
+ - `LOREM_IPSUM_PARAGRAPHS`
7
+ - `TextWrapper` (❇️ Fixed)
8
+ - `sanitize`
9
+ - `wrap`
10
+ - `align`
11
+ - `fillstr`
12
+ - `shorten`
13
+
14
+ # Documents📄
15
+ This module is inspired by the [`textwrap`](https://docs.python.org/3/library/textwrap.html) module, which provides
16
+ several useful functions, along with the [`TextWrapper`](#textwrapper), class that handles all available functions.
17
+
18
+ The difference between [`txtwrap`](https://pypi.org/project/txtwrap) and
19
+ [`textwrap`](https://docs.python.org/3/library/textwrap.html) is that this module is designed not only for wrapping and
20
+ filling monospace fonts but also for other font types, such as Arial, Times New Roman, and more.
21
+
22
+ <h1></h1>
23
+
24
+ ```py
25
+ LOREM_IPSUM_WORDS
26
+ LOREM_IPSUM_SENTENCES
27
+ LOREM_IPSUM_PARAGRAPHS
28
+ ```
29
+ A collection of words, sentences, and paragraphs that can be used as examples.
30
+ - `LOREM_IPSUM_WORDS` contains a short Lorem Ipsum sentence.
31
+ - `LOREM_IPSUM_SENTENCES` contains a slightly longer paragraph.
32
+ - `LOREM_IPSUM_PARAGRAPHS` contains several longer paragraphs.
33
+
34
+ <h1></h1>
35
+
36
+ ## `TextWrapper`
37
+ ```py
38
+ class TextWrapper:
39
+ def __init__(
40
+ self,
41
+ width: Union[int, float] = 70,
42
+ line_padding: Union[int, float] = 0,
43
+ method: Literal['mono', 'word'] = 'word',
44
+ alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
45
+ placeholder: str = '...',
46
+ fillchar: str = ' ',
47
+ separator: Optional[Union[str, Iterable[str]]] = None,
48
+ max_lines: Optional[int] = None,
49
+ preserve_empty: bool = True,
50
+ minimum_width: bool = True,
51
+ justify_last_line: bool = False,
52
+ break_on_hyphens: bool = True,
53
+ sizefunc: Optional[Callable[[str], Union[Tuple[Union[int, float], Union[int, float]], int, float]]] = None,
54
+ ) -> None
55
+ ```
56
+ A class that handles all functions available in this module. Each keyword argument corresponds to its attribute.
57
+ For example:
58
+ ```py
59
+ wrapper = TextWrapper(width=100)
60
+ ```
61
+ is equivalent to:
62
+ ```py
63
+ wrapper = TextWrapper()
64
+ wrapper.width = 100
65
+ ```
66
+ You can reuse [`TextWrapper`](#textwrapper) multiple times or modify its options by assigning new values to its
67
+ attributes. However, it is recommended not to reuse [`TextWrapper`](#textwrapper) too frequently inside a specific loop,
68
+ as each attribute has type checking, which may reduce performance.
69
+
70
+ <h1></h1>
71
+
72
+ ### Attributes of [`TextWrapper`](#textwrapper):
73
+
74
+ <h1></h1>
75
+
76
+ #### **`width`**
77
+ (Default: `70`) The maximum line length for wrapped text.
78
+
79
+ <h1></h1>
80
+
81
+ #### **`line_padding`**
82
+ (Default: `0`) The spacing between wrapped lines.
83
+
84
+ <h1></h1>
85
+
86
+ #### **`method`**
87
+ (Default: `'word'`) The wrapping method. Available options: `'mono'` and `'word'`.
88
+ - `'mono'` method wraps text character by character.
89
+ - `'word'` method wraps text word by word.
90
+
91
+ <h1></h1>
92
+
93
+ #### **`alignment`**
94
+ (Default: `'left'`) The alignment of the wrapped text. Available options: `'left'`, `'center'`, `'right'`,
95
+ (`'fill'` or `'fill-left'`), `'fill-center'`, and `'fill-right'`.
96
+ - `'left'`: Aligns text to the start of the line.
97
+ - `'center'`: Centers text within the line.
98
+ - `'right'`: Aligns text to the end of the line.
99
+ - `'fill'` or `'fill-left'`: Justifies text across the width but aligns single-word lines or the last line
100
+ (if [`justify_last_line`](#justify_last_line) is `False`) to the left.
101
+ - `'fill-center'` and `'fill-right'` work the same way as `'fill-left'`, aligning text according to their respective
102
+ names.
103
+
104
+ <h1></h1>
105
+
106
+ #### **`placeholder`**
107
+ (Default: `'...'`) The ellipsis used for truncating long lines.
108
+
109
+ <h1></h1>
110
+
111
+ #### **`fillchar`**
112
+ (Default: `' '`) The character used for padding.
113
+
114
+ <h1></h1>
115
+
116
+ #### **`separator`**
117
+ (Default: `None`) The character used to separate words.
118
+ - `None`: Uses whitespace as the separator.
119
+ - `str`: Uses the specified character.
120
+ - `Iterable`: Uses multiple specified characters.
121
+
122
+ <h1></h1>
123
+
124
+ #### **`max_lines`**
125
+ (Default: `None`) The maximum number of wrapped lines.
126
+ - `None`: No limit on the number of wrapped lines.
127
+ - `int`: Limits the number of wrapped lines to the specified value. (Ensure that [`width`](#width) is not smaller than
128
+ the length of [`placeholder`](#placeholder)).
129
+
130
+ <h1></h1>
131
+
132
+ #### **`preserve_empty`**
133
+ (Default: `True`) Retains empty lines in the wrapped text.
134
+
135
+ <h1></h1>
136
+
137
+ #### **`minimum_width`**
138
+ (Default: `True`) Uses the minimum required line width. Some wrapped lines may be shorter than the specified width, so
139
+ enabling this attribute removes unnecessary empty space.
140
+
141
+ <h1></h1>
142
+
143
+ #### **`justify_last_line`**
144
+ (Default: `False`) Determines whether the last line should also be justified
145
+ (applies only to `fill-...` alignments).
146
+
147
+ <h1></h1>
148
+
149
+ #### **`break_on_hyphens`**
150
+ (Default: `True`) Breaks words at hyphens (-). Example `'self-organization'` becomes `'self-'` and `'organization'`.
151
+
152
+ <h1></h1>
153
+
154
+ #### **`sizefunc`**
155
+ (Default: `None`) A function used to calculate the width and height or only the width of each string.
156
+
157
+ If the function calculates both width and height, it must return a tuple containing two values:
158
+ - The width and height of the string.
159
+ - Both values must be of type `int` or `float`.
160
+
161
+ If the function calculates only the width, it must return a single value of type `int` or `float`.
162
+
163
+ <h1></h1>
164
+
165
+ ### Methods of [`TextWrapper`](#textwrapper):
166
+
167
+ <h1></h1>
168
+
169
+ #### **`copy`**
170
+ Creates and returns a copy of the [`TextWrapper`](#textwrapper) object.
171
+
172
+ <h1></h1>
173
+
174
+ #### **`sanitize(text)`**
175
+ Removes excessive characters from [`separator`](#separator) and replaces them with the [`fillchar`](#fillchar)
176
+ character.
177
+
178
+ For example:
179
+ ```py
180
+ >>> TextWrapper().sanitize("\tHello World! ")
181
+ 'Hello World!'
182
+ ```
183
+
184
+ <h1></h1>
185
+
186
+ #### **`wrap(text, return_details=False)`**
187
+ Returns a list of wrapped text strings. If `return_details=True`, returns a dictionary containing:
188
+ - `'wrapped'`: A list of wrapped text fragments.
189
+ - `'indiced'`: A set of indices marking line breaks (starting from `0`, like programming indices).
190
+
191
+ For example:
192
+ ```py
193
+ >>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS)
194
+ ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.']
195
+ >>> TextWrapper(width=15).wrap(LOREM_IPSUM_WORDS, return_details=True)
196
+ {'wrapped': ['Lorem ipsum', 'odor amet,', 'consectetuer', 'adipiscing', 'elit.'], 'indiced': {4}}
197
+ ```
198
+
199
+ <h1></h1>
200
+
201
+ #### **`align(text, return_details=False)`**
202
+ Returns a list of tuples, where each tuple contains `(x, y, text)`, representing the wrapped text along with its
203
+ coordinates.
204
+ > Note: [`sizefunc`](#sizefunc) must return both width and height.
205
+
206
+ If `return_details=True`, returns a dictionary containing:
207
+ - `'aligned'`: A list of wrapped text with coordinate data.
208
+ - `'wrapped'`: The result from wrap.
209
+ - `'indiced'`: The indices of line breaks.
210
+ - `'size'`: The calculated text size.
211
+
212
+ For example:
213
+ ```py
214
+ >>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS)
215
+ [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')]
216
+ >>> TextWrapper(width=20).align(LOREM_IPSUM_WORDS, return_details=True)
217
+ {'aligned': [(0, 0, 'Lorem ipsum odor'), (0, 1, 'amet, consectetuer'), (0, 2, 'adipiscing elit.')], 'wrapped': [
218
+ 'Lorem ipsum odor', 'amet, consectetuer', 'adipiscing elit.'], 'indiced': {2}, 'size': (18, 3)}
219
+ ```
220
+
221
+ <h1></h1>
222
+
223
+ #### **`fillstr(text)`**
224
+ Returns a string with wrapped text formatted for monospace fonts.
225
+ > Note: [`width`](#width), [`line_padding`](#line_padding), and the output of [`sizefunc`](#sizefunc) must return `int`,
226
+ not `float`!
227
+
228
+ For example:
229
+ ```py
230
+ >>> s = TextWrapper(width=20).fillstr(LOREM_IPSUM_WORDS)
231
+ >>> s
232
+ 'Lorem ipsum odor \namet, consectetuer\nadipiscing elit. '
233
+ >>> print(s)
234
+ Lorem ipsum odor
235
+ amet, consectetuer
236
+ adipiscing elit.
237
+ ```
238
+
239
+ <h1></h1>
240
+
241
+ #### **`shorten(text)`**
242
+ Returns a truncated string if its length exceeds [`width`](#width), appending [`placeholder`](#placeholder) at the end
243
+ if truncated.
244
+
245
+ For example:
246
+ ```py
247
+ >>> TextWrapper(width=20).shorten(LOREM_IPSUM_WORDS)
248
+ 'Lorem ipsum odor...'
249
+ ```
250
+
251
+ <h1></h1>
252
+
253
+ # Another examples❓
254
+
255
+ ## Render a wrap text in PyGame🎮
256
+ ```py
257
+ from typing import Literal, Optional
258
+ from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
259
+ import pygame
260
+
261
+ def render_wrap(
262
+
263
+ font: pygame.Font,
264
+ text: str,
265
+ width: int,
266
+ antialias: bool,
267
+ color: pygame.Color,
268
+ background: Optional[pygame.Color] = None,
269
+ line_padding: int = 0,
270
+ method: Literal['word', 'mono'] = 'word',
271
+ alignment: Literal['left', 'center', 'right', 'fill', 'fill-left', 'fill-center', 'fill-right'] = 'left',
272
+ placeholder: str = '...',
273
+ max_lines: Optional[int] = None,
274
+ preserve_empty: bool = True,
275
+ minimum_width: bool = True,
276
+ justify_last_line: bool = False,
277
+ break_on_hyphens: bool = True
278
+
279
+ ) -> pygame.Surface:
280
+
281
+ align_info = align(
282
+ text=text,
283
+ width=width,
284
+ line_padding=line_padding,
285
+ method=method,
286
+ alignment=alignment,
287
+ placeholder=placeholder,
288
+ max_lines=max_lines,
289
+ preserve_empty=preserve_empty,
290
+ minimum_width=minimum_width,
291
+ justify_last_line=justify_last_line,
292
+ break_on_hyphens=break_on_hyphens,
293
+ return_details=True,
294
+ sizefunc=font.size
295
+ )
296
+
297
+ surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
298
+
299
+ if background is not None:
300
+ surface.fill(background)
301
+
302
+ for x, y, text in align_info['aligned']:
303
+ surface.blit(font.render(text, antialias, color), (x, y))
304
+
305
+ return surface
306
+
307
+ # Example usage:
308
+ pygame.init()
309
+ pygame.display.set_caption("Lorem Ipsum")
310
+
311
+ running = True
312
+ width, height = 800, 600
313
+ screen = pygame.display.set_mode((width, height))
314
+ clock = pygame.time.Clock()
315
+
316
+ surface = render_wrap(
317
+ font=pygame.font.SysFont('Arial', 18),
318
+ text=LOREM_IPSUM_PARAGRAPHS,
319
+ width=width,
320
+ antialias=True,
321
+ color='#ffffff',
322
+ background='#303030',
323
+ alignment='fill'
324
+ )
325
+
326
+ width_surface, height_surface = surface.get_size()
327
+ pos = ((width - width_surface) / 2, (height - height_surface) / 2)
328
+
329
+ while running:
330
+ for event in pygame.event.get():
331
+ if event.type == pygame.QUIT:
332
+ running = False
333
+ screen.fill('#000000')
334
+ screen.blit(surface, pos)
335
+ pygame.display.flip()
336
+ clock.tick(60)
337
+ ```
338
+
339
+ ## Short a long text🔤
340
+ ```py
341
+ from txtwrap import shorten, LOREM_IPSUM_SENTENCES
342
+
343
+ print(shorten(LOREM_IPSUM_SENTENCES, width=50, placeholder='…'))
344
+ ```