txtwrap 1.2.0__tar.gz → 2.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-2.1.0/PKG-INFO +292 -0
- txtwrap-2.1.0/README.md +277 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/setup.py +5 -4
- {txtwrap-1.2.0 → txtwrap-2.1.0}/txtwrap/__init__.py +4 -2
- txtwrap-2.1.0/txtwrap/__init__.pyi +242 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/txtwrap/__main__.py +61 -39
- txtwrap-2.1.0/txtwrap/txtwrap.py +811 -0
- txtwrap-2.1.0/txtwrap.egg-info/PKG-INFO +292 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/txtwrap.egg-info/SOURCES.txt +1 -0
- txtwrap-1.2.0/PKG-INFO +0 -162
- txtwrap-1.2.0/README.md +0 -148
- txtwrap-1.2.0/txtwrap/txtwrap.py +0 -630
- txtwrap-1.2.0/txtwrap.egg-info/PKG-INFO +0 -162
- {txtwrap-1.2.0 → txtwrap-2.1.0}/MANIFEST.in +0 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/setup.cfg +0 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/txtwrap.egg-info/dependency_links.txt +0 -0
- {txtwrap-1.2.0 → txtwrap-2.1.0}/txtwrap.egg-info/top_level.txt +0 -0
txtwrap-2.1.0/PKG-INFO
ADDED
@@ -0,0 +1,292 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: txtwrap
|
3
|
+
Version: 2.1.0
|
4
|
+
Summary: A simple text wrapping tool.
|
5
|
+
Author: azzammuhyala
|
6
|
+
Author-email: azzammuhyala@gmail.com
|
7
|
+
License: MIT
|
8
|
+
Keywords: wrap,wrapper,wrapping,wrapped,wrapping tool,text wrap,text wrapper,simple wrap,align,aligner,aligning,aligned
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
10
|
+
Classifier: Programming Language :: Python :: 3.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
|
+
|
16
|
+
# TxTWrap🔡
|
17
|
+
A tool for wrapping a text.🔨
|
18
|
+
|
19
|
+
## All constants, functions, and classes❕
|
20
|
+
- `LOREM_IPSUM_WORDS`
|
21
|
+
- `LOREM_IPSUM_SENTENCES`
|
22
|
+
- `LOREM_IPSUM_PARAGRAPHS`
|
23
|
+
- `TextWrapper` (Updated)
|
24
|
+
- `mono`
|
25
|
+
- `word` (Updated)
|
26
|
+
- `wrap` (Updated)
|
27
|
+
- `align` (Updated)
|
28
|
+
- `fillstr` (Updated)
|
29
|
+
- `printwrap` (Updated)
|
30
|
+
- `indent`
|
31
|
+
- `dedent`
|
32
|
+
- `shorten` (Updated)
|
33
|
+
|
34
|
+
## Documents📄
|
35
|
+
|
36
|
+
### Wrapper📦
|
37
|
+
```py
|
38
|
+
class TextWrapper:
|
39
|
+
|
40
|
+
def __init__(
|
41
|
+
|
42
|
+
self,
|
43
|
+
width: Union[int, float] = 70,
|
44
|
+
start: SupportsIndex = 0,
|
45
|
+
linegap: Union[int, float] = 0,
|
46
|
+
method: Literal['mono', 'word'] = 'word',
|
47
|
+
alignment: Literal['left', 'center', 'right', 'fill',
|
48
|
+
'fill-left', 'fill-center', 'fill-right'] = 'left',
|
49
|
+
fillchar: str = ' ',
|
50
|
+
placeholder: str = '...',
|
51
|
+
prefix: Optional[str] = None,
|
52
|
+
separator: Optional[str] = None,
|
53
|
+
preserve_empty: bool = True,
|
54
|
+
use_minimum_width: bool = True,
|
55
|
+
justify_last_line: bool = False,
|
56
|
+
break_on_hyphens: bool = True,
|
57
|
+
sizefunc: Callable[[str], Tuple[Union[int, float],
|
58
|
+
Union[int, float]]] = lambda s : (len(s), 1),
|
59
|
+
predicate: Callable[[str], bool] = lambda line: line.strip()
|
60
|
+
|
61
|
+
) -> None
|
62
|
+
```
|
63
|
+
|
64
|
+
> **width** : _int | float_
|
65
|
+
=> The maximum width of the wrapped line. Default: 70.
|
66
|
+
|
67
|
+
> **start** : _SupportsIndex_
|
68
|
+
=> The starting index of the text (specific to the shorten method). Default: 0.
|
69
|
+
|
70
|
+
> **linegap** : _int | float_
|
71
|
+
=> The gap between lines (specific to the align method). Default: 0.
|
72
|
+
|
73
|
+
> **method** : _Literal['mono', 'word']_
|
74
|
+
=> The text wrapping method. Default: 'word'.
|
75
|
+
|
76
|
+
> **alignment** : _Literal['left', 'center', 'right', 'fill',
|
77
|
+
'fill-left', 'fill-center', 'fill-right']_
|
78
|
+
=> The text alignment (specific to align and fillstr method). Default: 'left'.
|
79
|
+
|
80
|
+
> **fillchar** : _str_
|
81
|
+
=> The character used for padding. Default: ' '.
|
82
|
+
|
83
|
+
> **placeholder** : _str_
|
84
|
+
=> The placeholder used when shortening text (specific to the shorten method). Default: '...'.
|
85
|
+
|
86
|
+
> **prefix** : _Optional[str]_
|
87
|
+
=> A prefix to add or remove (for the indent method, it must be a string and will be added at the
|
88
|
+
beginning of each line. For dedent, it is optional and removes the prefix from the beginning of each
|
89
|
+
line). Default: None.
|
90
|
+
|
91
|
+
> **separator** : _Optional[str]_
|
92
|
+
=> The separator used between words. Default: None.
|
93
|
+
|
94
|
+
> **preserve_empty** : _bool_
|
95
|
+
=> Whether to retain empty lines. Default: True.
|
96
|
+
|
97
|
+
> **use_minimum_width** : _bool_
|
98
|
+
=> Whether to use the minimum detected width (specific to the align and fillstr methods).
|
99
|
+
Default: True.
|
100
|
+
|
101
|
+
> **justify_last_line** : _bool_
|
102
|
+
=> Whether to adjust the alignment of the last line (applies to align and fillstr methods, but only
|
103
|
+
for non-wrapped text and only for fill alignment). Default: False.
|
104
|
+
|
105
|
+
> **break_on_hyphens** : _bool_
|
106
|
+
=> Whether to allow breaking words at hyphens (-) (specific to align and fillstr methods).
|
107
|
+
Default: True.
|
108
|
+
|
109
|
+
> **sizefunc** : _(str) -> tuple[int | float, int | float]_
|
110
|
+
=> A function to calculate the width and height of the text. The default is a lambda function
|
111
|
+
returning the text length as width and 1 as height.
|
112
|
+
|
113
|
+
> **predicate** : _(str) -> bool_
|
114
|
+
=> A function to filter lines (specific to indent and dedent methods). The default is a lambda
|
115
|
+
function that trims empty lines from the left and right.
|
116
|
+
|
117
|
+
### Mod📦 `python -m txtwrap`
|
118
|
+
```
|
119
|
+
usage: txtwrap [-h] [-v] [-w <int>] [-s <int>] [-m {word|mono|indent|dedent|shorten}]
|
120
|
+
[-a {left|center|right|fill|fill-left|fill-center|fill-right}] [-f <str>] [-p <str>]
|
121
|
+
[-x <str>] [-r <str>] [-n] [-i] [-j] [-b] text
|
122
|
+
|
123
|
+
Command-line tool for wrapping, aligning, or shortening text.
|
124
|
+
|
125
|
+
positional arguments:
|
126
|
+
text Target text
|
127
|
+
|
128
|
+
options:
|
129
|
+
-h, --help show this help message and exit
|
130
|
+
-v, --version Show the version of the txtwrap
|
131
|
+
-w <int>, --width <int>
|
132
|
+
Width of the text wrapping (default: current width terminal or 70)
|
133
|
+
-s <int>, --start <int>
|
134
|
+
start index of the text to be shorten (default: 0)
|
135
|
+
-m {word|mono|indent|dedent|shorten}, --method {word|mono|indent|dedent|shorten}
|
136
|
+
Method to be applied to the text (default: "word")
|
137
|
+
-a {left|center|right|fill|fill-left|fill-center|fill-right},
|
138
|
+
--alignment {left|center|right|fill|fill-left|fill-center|fill-right}
|
139
|
+
Alignment of the text (default: "left")
|
140
|
+
-f <str>, --fillchar <str>
|
141
|
+
Fill character (default: " ")
|
142
|
+
-p <str>, --placeholder <str>
|
143
|
+
Placeholder to be used when shortening the text (default: "...")
|
144
|
+
-x <str>, --prefix <str>
|
145
|
+
Prefix to be added (indent) or remove (dedent) to the text
|
146
|
+
-r <str>, --separator <str>
|
147
|
+
The separator used between words
|
148
|
+
-n, --neglect-empty Neglect empty lines in the text
|
149
|
+
-i, --use-minimum-width
|
150
|
+
Whether to use the minimum detected width
|
151
|
+
-j, --justify-last-line
|
152
|
+
Whether to adjust the alignment of the last line
|
153
|
+
-b, --not-break-on-hyphens
|
154
|
+
Doesn't break on hyphens
|
155
|
+
|
156
|
+
for example: python|py -m txtwrap "Lorem ipsum odor amet, consectetuer adipiscing elit." -w 20 `
|
157
|
+
-m word -a center
|
158
|
+
```
|
159
|
+
|
160
|
+
## Examples❓
|
161
|
+
|
162
|
+
### Render a wrap text in PyGame🎮
|
163
|
+
```py
|
164
|
+
from typing import Literal, Optional
|
165
|
+
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
166
|
+
import pygame
|
167
|
+
|
168
|
+
def render_wrap(
|
169
|
+
|
170
|
+
font: pygame.Font,
|
171
|
+
text: str,
|
172
|
+
width: int,
|
173
|
+
antialias: bool,
|
174
|
+
color: pygame.Color,
|
175
|
+
background: Optional[pygame.Color] = None,
|
176
|
+
linegap: int = 0,
|
177
|
+
method: Literal['word', 'mono'] = 'word',
|
178
|
+
alignment: Literal['left', 'center', 'right', 'fill',
|
179
|
+
'fill-left', 'fill-center', 'fill-right'] = 'left',
|
180
|
+
preserve_empty: bool = True,
|
181
|
+
use_minimum_width: bool = True,
|
182
|
+
justify_last_line: bool = False,
|
183
|
+
break_on_hyphens: bool = True
|
184
|
+
|
185
|
+
) -> pygame.Surface:
|
186
|
+
|
187
|
+
align_info = align(
|
188
|
+
text=text,
|
189
|
+
width=width,
|
190
|
+
linegap=linegap,
|
191
|
+
method=method,
|
192
|
+
alignment=alignment,
|
193
|
+
preserve_empty=preserve_empty,
|
194
|
+
use_minimum_width=use_minimum_width,
|
195
|
+
justify_last_line=justify_last_line,
|
196
|
+
break_on_hyphens=break_on_hyphens,
|
197
|
+
return_details=True,
|
198
|
+
sizefunc=font.size
|
199
|
+
)
|
200
|
+
|
201
|
+
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
202
|
+
|
203
|
+
if background is not None:
|
204
|
+
surface.fill(background)
|
205
|
+
|
206
|
+
for x, y, text in align_info['aligned']:
|
207
|
+
surface.blit(font.render(text, antialias, color), (x, y))
|
208
|
+
|
209
|
+
return surface
|
210
|
+
|
211
|
+
# Example usage:
|
212
|
+
pygame.init()
|
213
|
+
pygame.display.set_caption("Lorem Ipsum")
|
214
|
+
|
215
|
+
running = True
|
216
|
+
width, height = 800, 600
|
217
|
+
screen = pygame.display.set_mode((width, height))
|
218
|
+
clock = pygame.time.Clock()
|
219
|
+
|
220
|
+
surface = render_wrap(
|
221
|
+
font=pygame.font.SysFont('Arial', 18),
|
222
|
+
text=LOREM_IPSUM_PARAGRAPHS,
|
223
|
+
width=width,
|
224
|
+
antialias=True,
|
225
|
+
color='#ffffff',
|
226
|
+
background='#303030',
|
227
|
+
alignment='fill'
|
228
|
+
)
|
229
|
+
|
230
|
+
wsurf, hsurf = surface.get_size()
|
231
|
+
pos = ((width - wsurf) / 2, (height - hsurf) / 2)
|
232
|
+
|
233
|
+
while running:
|
234
|
+
for event in pygame.event.get():
|
235
|
+
if event.type == pygame.QUIT:
|
236
|
+
running = False
|
237
|
+
screen.fill('#000000')
|
238
|
+
screen.blit(surface, pos)
|
239
|
+
pygame.display.flip()
|
240
|
+
clock.tick(60)
|
241
|
+
```
|
242
|
+
|
243
|
+
### Print a wrap text to terminal🔡
|
244
|
+
```py
|
245
|
+
from txtwrap import printwrap, LOREM_IPSUM_WORDS
|
246
|
+
|
247
|
+
width = 20
|
248
|
+
|
249
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='left')
|
250
|
+
print('=' * width)
|
251
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='center')
|
252
|
+
print('=' * width)
|
253
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='right')
|
254
|
+
print('=' * width)
|
255
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill') # or alignment='fill-left'
|
256
|
+
print('=' * width)
|
257
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill-center')
|
258
|
+
print('=' * width)
|
259
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill-right')
|
260
|
+
```
|
261
|
+
|
262
|
+
### Short a long text🔤
|
263
|
+
```py
|
264
|
+
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
265
|
+
|
266
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=20, placeholder='…'))
|
267
|
+
```
|
268
|
+
|
269
|
+
### Bonus🎁 - Print a colorfull text to terminal🔥
|
270
|
+
```py
|
271
|
+
# Run this code in a terminal that supports ansi characters
|
272
|
+
|
273
|
+
from re import compile
|
274
|
+
from random import randint
|
275
|
+
from txtwrap import printwrap, LOREM_IPSUM_PARAGRAPHS
|
276
|
+
|
277
|
+
# Set the text to be printed here
|
278
|
+
text = LOREM_IPSUM_PARAGRAPHS
|
279
|
+
|
280
|
+
remove_ansi_regex = compile(r'\x1b\[(K|.*?m)').sub
|
281
|
+
|
282
|
+
def len_no_ansi(s: str):
|
283
|
+
return len(remove_ansi_regex('', s))
|
284
|
+
|
285
|
+
while True:
|
286
|
+
printwrap(
|
287
|
+
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
|
288
|
+
end='\x1b[H\x1b[J',
|
289
|
+
alignment='fill',
|
290
|
+
lenfunc=len_no_ansi
|
291
|
+
)
|
292
|
+
```
|
txtwrap-2.1.0/README.md
ADDED
@@ -0,0 +1,277 @@
|
|
1
|
+
# TxTWrap🔡
|
2
|
+
A tool for wrapping a text.🔨
|
3
|
+
|
4
|
+
## All constants, functions, and classes❕
|
5
|
+
- `LOREM_IPSUM_WORDS`
|
6
|
+
- `LOREM_IPSUM_SENTENCES`
|
7
|
+
- `LOREM_IPSUM_PARAGRAPHS`
|
8
|
+
- `TextWrapper` (Updated)
|
9
|
+
- `mono`
|
10
|
+
- `word` (Updated)
|
11
|
+
- `wrap` (Updated)
|
12
|
+
- `align` (Updated)
|
13
|
+
- `fillstr` (Updated)
|
14
|
+
- `printwrap` (Updated)
|
15
|
+
- `indent`
|
16
|
+
- `dedent`
|
17
|
+
- `shorten` (Updated)
|
18
|
+
|
19
|
+
## Documents📄
|
20
|
+
|
21
|
+
### Wrapper📦
|
22
|
+
```py
|
23
|
+
class TextWrapper:
|
24
|
+
|
25
|
+
def __init__(
|
26
|
+
|
27
|
+
self,
|
28
|
+
width: Union[int, float] = 70,
|
29
|
+
start: SupportsIndex = 0,
|
30
|
+
linegap: Union[int, float] = 0,
|
31
|
+
method: Literal['mono', 'word'] = 'word',
|
32
|
+
alignment: Literal['left', 'center', 'right', 'fill',
|
33
|
+
'fill-left', 'fill-center', 'fill-right'] = 'left',
|
34
|
+
fillchar: str = ' ',
|
35
|
+
placeholder: str = '...',
|
36
|
+
prefix: Optional[str] = None,
|
37
|
+
separator: Optional[str] = None,
|
38
|
+
preserve_empty: bool = True,
|
39
|
+
use_minimum_width: bool = True,
|
40
|
+
justify_last_line: bool = False,
|
41
|
+
break_on_hyphens: bool = True,
|
42
|
+
sizefunc: Callable[[str], Tuple[Union[int, float],
|
43
|
+
Union[int, float]]] = lambda s : (len(s), 1),
|
44
|
+
predicate: Callable[[str], bool] = lambda line: line.strip()
|
45
|
+
|
46
|
+
) -> None
|
47
|
+
```
|
48
|
+
|
49
|
+
> **width** : _int | float_
|
50
|
+
=> The maximum width of the wrapped line. Default: 70.
|
51
|
+
|
52
|
+
> **start** : _SupportsIndex_
|
53
|
+
=> The starting index of the text (specific to the shorten method). Default: 0.
|
54
|
+
|
55
|
+
> **linegap** : _int | float_
|
56
|
+
=> The gap between lines (specific to the align method). Default: 0.
|
57
|
+
|
58
|
+
> **method** : _Literal['mono', 'word']_
|
59
|
+
=> The text wrapping method. Default: 'word'.
|
60
|
+
|
61
|
+
> **alignment** : _Literal['left', 'center', 'right', 'fill',
|
62
|
+
'fill-left', 'fill-center', 'fill-right']_
|
63
|
+
=> The text alignment (specific to align and fillstr method). Default: 'left'.
|
64
|
+
|
65
|
+
> **fillchar** : _str_
|
66
|
+
=> The character used for padding. Default: ' '.
|
67
|
+
|
68
|
+
> **placeholder** : _str_
|
69
|
+
=> The placeholder used when shortening text (specific to the shorten method). Default: '...'.
|
70
|
+
|
71
|
+
> **prefix** : _Optional[str]_
|
72
|
+
=> A prefix to add or remove (for the indent method, it must be a string and will be added at the
|
73
|
+
beginning of each line. For dedent, it is optional and removes the prefix from the beginning of each
|
74
|
+
line). Default: None.
|
75
|
+
|
76
|
+
> **separator** : _Optional[str]_
|
77
|
+
=> The separator used between words. Default: None.
|
78
|
+
|
79
|
+
> **preserve_empty** : _bool_
|
80
|
+
=> Whether to retain empty lines. Default: True.
|
81
|
+
|
82
|
+
> **use_minimum_width** : _bool_
|
83
|
+
=> Whether to use the minimum detected width (specific to the align and fillstr methods).
|
84
|
+
Default: True.
|
85
|
+
|
86
|
+
> **justify_last_line** : _bool_
|
87
|
+
=> Whether to adjust the alignment of the last line (applies to align and fillstr methods, but only
|
88
|
+
for non-wrapped text and only for fill alignment). Default: False.
|
89
|
+
|
90
|
+
> **break_on_hyphens** : _bool_
|
91
|
+
=> Whether to allow breaking words at hyphens (-) (specific to align and fillstr methods).
|
92
|
+
Default: True.
|
93
|
+
|
94
|
+
> **sizefunc** : _(str) -> tuple[int | float, int | float]_
|
95
|
+
=> A function to calculate the width and height of the text. The default is a lambda function
|
96
|
+
returning the text length as width and 1 as height.
|
97
|
+
|
98
|
+
> **predicate** : _(str) -> bool_
|
99
|
+
=> A function to filter lines (specific to indent and dedent methods). The default is a lambda
|
100
|
+
function that trims empty lines from the left and right.
|
101
|
+
|
102
|
+
### Mod📦 `python -m txtwrap`
|
103
|
+
```
|
104
|
+
usage: txtwrap [-h] [-v] [-w <int>] [-s <int>] [-m {word|mono|indent|dedent|shorten}]
|
105
|
+
[-a {left|center|right|fill|fill-left|fill-center|fill-right}] [-f <str>] [-p <str>]
|
106
|
+
[-x <str>] [-r <str>] [-n] [-i] [-j] [-b] text
|
107
|
+
|
108
|
+
Command-line tool for wrapping, aligning, or shortening text.
|
109
|
+
|
110
|
+
positional arguments:
|
111
|
+
text Target text
|
112
|
+
|
113
|
+
options:
|
114
|
+
-h, --help show this help message and exit
|
115
|
+
-v, --version Show the version of the txtwrap
|
116
|
+
-w <int>, --width <int>
|
117
|
+
Width of the text wrapping (default: current width terminal or 70)
|
118
|
+
-s <int>, --start <int>
|
119
|
+
start index of the text to be shorten (default: 0)
|
120
|
+
-m {word|mono|indent|dedent|shorten}, --method {word|mono|indent|dedent|shorten}
|
121
|
+
Method to be applied to the text (default: "word")
|
122
|
+
-a {left|center|right|fill|fill-left|fill-center|fill-right},
|
123
|
+
--alignment {left|center|right|fill|fill-left|fill-center|fill-right}
|
124
|
+
Alignment of the text (default: "left")
|
125
|
+
-f <str>, --fillchar <str>
|
126
|
+
Fill character (default: " ")
|
127
|
+
-p <str>, --placeholder <str>
|
128
|
+
Placeholder to be used when shortening the text (default: "...")
|
129
|
+
-x <str>, --prefix <str>
|
130
|
+
Prefix to be added (indent) or remove (dedent) to the text
|
131
|
+
-r <str>, --separator <str>
|
132
|
+
The separator used between words
|
133
|
+
-n, --neglect-empty Neglect empty lines in the text
|
134
|
+
-i, --use-minimum-width
|
135
|
+
Whether to use the minimum detected width
|
136
|
+
-j, --justify-last-line
|
137
|
+
Whether to adjust the alignment of the last line
|
138
|
+
-b, --not-break-on-hyphens
|
139
|
+
Doesn't break on hyphens
|
140
|
+
|
141
|
+
for example: python|py -m txtwrap "Lorem ipsum odor amet, consectetuer adipiscing elit." -w 20 `
|
142
|
+
-m word -a center
|
143
|
+
```
|
144
|
+
|
145
|
+
## Examples❓
|
146
|
+
|
147
|
+
### Render a wrap text in PyGame🎮
|
148
|
+
```py
|
149
|
+
from typing import Literal, Optional
|
150
|
+
from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
|
151
|
+
import pygame
|
152
|
+
|
153
|
+
def render_wrap(
|
154
|
+
|
155
|
+
font: pygame.Font,
|
156
|
+
text: str,
|
157
|
+
width: int,
|
158
|
+
antialias: bool,
|
159
|
+
color: pygame.Color,
|
160
|
+
background: Optional[pygame.Color] = None,
|
161
|
+
linegap: int = 0,
|
162
|
+
method: Literal['word', 'mono'] = 'word',
|
163
|
+
alignment: Literal['left', 'center', 'right', 'fill',
|
164
|
+
'fill-left', 'fill-center', 'fill-right'] = 'left',
|
165
|
+
preserve_empty: bool = True,
|
166
|
+
use_minimum_width: bool = True,
|
167
|
+
justify_last_line: bool = False,
|
168
|
+
break_on_hyphens: bool = True
|
169
|
+
|
170
|
+
) -> pygame.Surface:
|
171
|
+
|
172
|
+
align_info = align(
|
173
|
+
text=text,
|
174
|
+
width=width,
|
175
|
+
linegap=linegap,
|
176
|
+
method=method,
|
177
|
+
alignment=alignment,
|
178
|
+
preserve_empty=preserve_empty,
|
179
|
+
use_minimum_width=use_minimum_width,
|
180
|
+
justify_last_line=justify_last_line,
|
181
|
+
break_on_hyphens=break_on_hyphens,
|
182
|
+
return_details=True,
|
183
|
+
sizefunc=font.size
|
184
|
+
)
|
185
|
+
|
186
|
+
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
|
187
|
+
|
188
|
+
if background is not None:
|
189
|
+
surface.fill(background)
|
190
|
+
|
191
|
+
for x, y, text in align_info['aligned']:
|
192
|
+
surface.blit(font.render(text, antialias, color), (x, y))
|
193
|
+
|
194
|
+
return surface
|
195
|
+
|
196
|
+
# Example usage:
|
197
|
+
pygame.init()
|
198
|
+
pygame.display.set_caption("Lorem Ipsum")
|
199
|
+
|
200
|
+
running = True
|
201
|
+
width, height = 800, 600
|
202
|
+
screen = pygame.display.set_mode((width, height))
|
203
|
+
clock = pygame.time.Clock()
|
204
|
+
|
205
|
+
surface = render_wrap(
|
206
|
+
font=pygame.font.SysFont('Arial', 18),
|
207
|
+
text=LOREM_IPSUM_PARAGRAPHS,
|
208
|
+
width=width,
|
209
|
+
antialias=True,
|
210
|
+
color='#ffffff',
|
211
|
+
background='#303030',
|
212
|
+
alignment='fill'
|
213
|
+
)
|
214
|
+
|
215
|
+
wsurf, hsurf = surface.get_size()
|
216
|
+
pos = ((width - wsurf) / 2, (height - hsurf) / 2)
|
217
|
+
|
218
|
+
while running:
|
219
|
+
for event in pygame.event.get():
|
220
|
+
if event.type == pygame.QUIT:
|
221
|
+
running = False
|
222
|
+
screen.fill('#000000')
|
223
|
+
screen.blit(surface, pos)
|
224
|
+
pygame.display.flip()
|
225
|
+
clock.tick(60)
|
226
|
+
```
|
227
|
+
|
228
|
+
### Print a wrap text to terminal🔡
|
229
|
+
```py
|
230
|
+
from txtwrap import printwrap, LOREM_IPSUM_WORDS
|
231
|
+
|
232
|
+
width = 20
|
233
|
+
|
234
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='left')
|
235
|
+
print('=' * width)
|
236
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='center')
|
237
|
+
print('=' * width)
|
238
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='right')
|
239
|
+
print('=' * width)
|
240
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill') # or alignment='fill-left'
|
241
|
+
print('=' * width)
|
242
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill-center')
|
243
|
+
print('=' * width)
|
244
|
+
printwrap(LOREM_IPSUM_WORDS, width=width, alignment='fill-right')
|
245
|
+
```
|
246
|
+
|
247
|
+
### Short a long text🔤
|
248
|
+
```py
|
249
|
+
from txtwrap import shorten, LOREM_IPSUM_SENTENCES
|
250
|
+
|
251
|
+
print(shorten(LOREM_IPSUM_SENTENCES, width=20, placeholder='…'))
|
252
|
+
```
|
253
|
+
|
254
|
+
### Bonus🎁 - Print a colorfull text to terminal🔥
|
255
|
+
```py
|
256
|
+
# Run this code in a terminal that supports ansi characters
|
257
|
+
|
258
|
+
from re import compile
|
259
|
+
from random import randint
|
260
|
+
from txtwrap import printwrap, LOREM_IPSUM_PARAGRAPHS
|
261
|
+
|
262
|
+
# Set the text to be printed here
|
263
|
+
text = LOREM_IPSUM_PARAGRAPHS
|
264
|
+
|
265
|
+
remove_ansi_regex = compile(r'\x1b\[(K|.*?m)').sub
|
266
|
+
|
267
|
+
def len_no_ansi(s: str):
|
268
|
+
return len(remove_ansi_regex('', s))
|
269
|
+
|
270
|
+
while True:
|
271
|
+
printwrap(
|
272
|
+
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
|
273
|
+
end='\x1b[H\x1b[J',
|
274
|
+
alignment='fill',
|
275
|
+
lenfunc=len_no_ansi
|
276
|
+
)
|
277
|
+
```
|
@@ -5,21 +5,22 @@ with open('README.md', encoding='utf-8') as readme:
|
|
5
5
|
|
6
6
|
setup(
|
7
7
|
name='txtwrap',
|
8
|
-
version='1.
|
8
|
+
version='2.1.0',
|
9
9
|
description='A simple text wrapping tool.',
|
10
10
|
long_description=long_description,
|
11
11
|
long_description_content_type='text/markdown',
|
12
12
|
author='azzammuhyala',
|
13
|
+
author_email='azzammuhyala@gmail.com',
|
13
14
|
license='MIT',
|
14
|
-
python_requires='>=3.
|
15
|
+
python_requires='>=3.3',
|
15
16
|
packages=find_packages(),
|
16
17
|
include_package_data=True,
|
17
18
|
keywords=['wrap', 'wrapper', 'wrapping', 'wrapped', 'wrapping tool', 'text wrap',
|
18
19
|
'text wrapper', 'simple wrap', 'align', 'aligner', 'aligning', 'aligned'],
|
19
20
|
classifiers=[
|
20
21
|
'Programming Language :: Python :: 3',
|
21
|
-
'Programming Language :: Python :: 3.
|
22
|
+
'Programming Language :: Python :: 3.3',
|
22
23
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
23
|
-
'License :: OSI Approved :: MIT License'
|
24
|
+
'License :: OSI Approved :: MIT License'
|
24
25
|
]
|
25
26
|
)
|
@@ -1,12 +1,13 @@
|
|
1
1
|
"""
|
2
|
-
A simple text wrapping tool
|
2
|
+
A simple text wrapping tool.
|
3
3
|
"""
|
4
4
|
|
5
|
-
# Supports only in Python 3.
|
5
|
+
# Supports only in Python 3.3+
|
6
6
|
|
7
7
|
from .txtwrap import (
|
8
8
|
version,
|
9
9
|
LOREM_IPSUM_WORDS, LOREM_IPSUM_SENTENCES, LOREM_IPSUM_PARAGRAPHS,
|
10
|
+
TextWrapper,
|
10
11
|
mono, word, wrap, align, fillstr, printwrap,
|
11
12
|
indent, dedent,
|
12
13
|
shorten
|
@@ -19,6 +20,7 @@ __all__ = [
|
|
19
20
|
'LOREM_IPSUM_WORDS',
|
20
21
|
'LOREM_IPSUM_SENTENCES',
|
21
22
|
'LOREM_IPSUM_PARAGRAPHS',
|
23
|
+
'TextWrapper',
|
22
24
|
'mono',
|
23
25
|
'word',
|
24
26
|
'wrap',
|