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