txtwrap 1.1.0__tar.gz → 1.1.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-1.1.0/txtwrap.egg-info → txtwrap-1.1.1}/PKG-INFO +45 -13
- {txtwrap-1.1.0 → txtwrap-1.1.1}/README.md +43 -11
- {txtwrap-1.1.0 → txtwrap-1.1.1}/setup.py +3 -3
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap/__init__.py +3 -2
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap/__main__.py +38 -9
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap/txtwrap.py +32 -35
- {txtwrap-1.1.0 → txtwrap-1.1.1/txtwrap.egg-info}/PKG-INFO +45 -13
- {txtwrap-1.1.0 → txtwrap-1.1.1}/MANIFEST.in +0 -0
- {txtwrap-1.1.0 → txtwrap-1.1.1}/setup.cfg +0 -0
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap.egg-info/SOURCES.txt +0 -0
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap.egg-info/dependency_links.txt +0 -0
- {txtwrap-1.1.0 → txtwrap-1.1.1}/txtwrap.egg-info/top_level.txt +0 -0
@@ -1,10 +1,10 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: txtwrap
|
3
|
-
Version: 1.1.
|
3
|
+
Version: 1.1.1
|
4
4
|
Summary: A simple text wrapping tool.
|
5
5
|
Author: azzammuhyala
|
6
6
|
License: MIT
|
7
|
-
Keywords: wrap,wrapper,wrapping,wrapping tool,text wrap,text wrapper,simple wrap,align,aligner,aligning
|
7
|
+
Keywords: wrap,wrapper,wrapping,wrapped,wrapping tool,text wrap,text wrapper,simple wrap,align,aligner,aligning,aligned
|
8
8
|
Classifier: Programming Language :: Python :: 3
|
9
9
|
Classifier: Programming Language :: Python :: 3.8
|
10
10
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
@@ -25,9 +25,9 @@ All constants and functions❕:
|
|
25
25
|
- `word`
|
26
26
|
- `wrap`
|
27
27
|
- `align` (Updated!)
|
28
|
-
- `fillstr`
|
29
|
-
- `printwrap`
|
30
|
-
- `shorten`
|
28
|
+
- `fillstr`
|
29
|
+
- `printwrap`
|
30
|
+
- `shorten`
|
31
31
|
|
32
32
|
Mod `python -m txtwrap` Commands❗:
|
33
33
|
```shell
|
@@ -43,7 +43,7 @@ import pygame
|
|
43
43
|
|
44
44
|
def render_wrap(
|
45
45
|
|
46
|
-
font: pygame.
|
46
|
+
font: pygame.Font,
|
47
47
|
text: str,
|
48
48
|
width: int,
|
49
49
|
antialias: bool,
|
@@ -52,12 +52,12 @@ def render_wrap(
|
|
52
52
|
linegap: int = 0,
|
53
53
|
alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
|
54
54
|
method: Literal['word', 'mono'] = 'word',
|
55
|
-
|
56
|
-
|
55
|
+
preserve_empty: bool = True,
|
56
|
+
use_min_width: bool = True
|
57
57
|
|
58
58
|
) -> pygame.Surface:
|
59
59
|
|
60
|
-
# Only supports in txtwrap 1.1.
|
60
|
+
# Only supports in txtwrap 1.1.1+
|
61
61
|
align_info = align(
|
62
62
|
text=text,
|
63
63
|
width=width,
|
@@ -65,8 +65,8 @@ def render_wrap(
|
|
65
65
|
sizefunc=font.size,
|
66
66
|
method=method,
|
67
67
|
alignment=alignment,
|
68
|
-
use_max_width=use_max_width,
|
69
68
|
preserve_empty=preserve_empty,
|
69
|
+
use_min_width=use_min_width,
|
70
70
|
return_details=True
|
71
71
|
)
|
72
72
|
|
@@ -85,30 +85,35 @@ pygame.init()
|
|
85
85
|
pygame.display.set_caption("Lorem Ipsum")
|
86
86
|
|
87
87
|
running = True
|
88
|
-
|
88
|
+
wscrn, hscrn = 600, 600
|
89
|
+
screen = pygame.display.set_mode((wscrn, hscrn))
|
89
90
|
clock = pygame.time.Clock()
|
90
91
|
surface = render_wrap(
|
91
92
|
font=pygame.font.Font(None, 20),
|
92
93
|
text=LOREM_IPSUM_P,
|
93
|
-
width=
|
94
|
+
width=wscrn,
|
94
95
|
antialias=True,
|
95
96
|
color='#ffffff',
|
96
97
|
background='#303030',
|
97
98
|
alignment='fill'
|
98
99
|
)
|
99
100
|
|
101
|
+
wsurf, hsurf = surface.get_size()
|
102
|
+
pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
|
103
|
+
|
100
104
|
while running:
|
101
105
|
for event in pygame.event.get():
|
102
106
|
if event.type == pygame.QUIT:
|
103
107
|
running = False
|
104
108
|
screen.fill('#000000')
|
105
|
-
screen.blit(surface,
|
109
|
+
screen.blit(surface, pos)
|
106
110
|
pygame.display.flip()
|
107
111
|
clock.tick(60)
|
108
112
|
```
|
109
113
|
|
110
114
|
## Print a wrap text to terminal🔡
|
111
115
|
```py
|
116
|
+
# Only supports in txtwrap 1.1.0+
|
112
117
|
from txtwrap import printwrap, LOREM_IPSUM_W
|
113
118
|
|
114
119
|
printwrap(LOREM_IPSUM_W, width=20, alignment='left')
|
@@ -125,8 +130,35 @@ printwrap(LOREM_IPSUM_W, width=20, alignment='fill')
|
|
125
130
|
from txtwrap import shorten, LOREM_IPSUM_S
|
126
131
|
|
127
132
|
short_lorem = shorten(LOREM_IPSUM_S, width=20, placeholder='…')
|
133
|
+
# Only supports in txtwrap 1.1.0+
|
128
134
|
test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
|
129
135
|
|
130
136
|
print(short_lorem)
|
131
137
|
print(test)
|
132
138
|
```
|
139
|
+
|
140
|
+
## Bonus🎁 - Print a colorfull text to terminal🔥
|
141
|
+
```py
|
142
|
+
# Run this code in a terminal that supports ansi characters
|
143
|
+
|
144
|
+
from re import compile
|
145
|
+
from random import randint
|
146
|
+
from txtwrap import printwrap, LOREM_IPSUM_P
|
147
|
+
|
148
|
+
# Set the text to be printed here
|
149
|
+
text = LOREM_IPSUM_P
|
150
|
+
|
151
|
+
remove_ansi_regex = compile(r'\x1b\[[0-9;]*[mK]').sub
|
152
|
+
|
153
|
+
def ralen(s: str) -> int:
|
154
|
+
return len(remove_ansi_regex('', s))
|
155
|
+
|
156
|
+
while True:
|
157
|
+
# Only supports in txtwrap 1.1.0+
|
158
|
+
printwrap(
|
159
|
+
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
|
160
|
+
end='\x1b[H\x1b[J',
|
161
|
+
alignment='fill',
|
162
|
+
lenfunc=ralen
|
163
|
+
)
|
164
|
+
```
|
@@ -11,9 +11,9 @@ All constants and functions❕:
|
|
11
11
|
- `word`
|
12
12
|
- `wrap`
|
13
13
|
- `align` (Updated!)
|
14
|
-
- `fillstr`
|
15
|
-
- `printwrap`
|
16
|
-
- `shorten`
|
14
|
+
- `fillstr`
|
15
|
+
- `printwrap`
|
16
|
+
- `shorten`
|
17
17
|
|
18
18
|
Mod `python -m txtwrap` Commands❗:
|
19
19
|
```shell
|
@@ -29,7 +29,7 @@ import pygame
|
|
29
29
|
|
30
30
|
def render_wrap(
|
31
31
|
|
32
|
-
font: pygame.
|
32
|
+
font: pygame.Font,
|
33
33
|
text: str,
|
34
34
|
width: int,
|
35
35
|
antialias: bool,
|
@@ -38,12 +38,12 @@ def render_wrap(
|
|
38
38
|
linegap: int = 0,
|
39
39
|
alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
|
40
40
|
method: Literal['word', 'mono'] = 'word',
|
41
|
-
|
42
|
-
|
41
|
+
preserve_empty: bool = True,
|
42
|
+
use_min_width: bool = True
|
43
43
|
|
44
44
|
) -> pygame.Surface:
|
45
45
|
|
46
|
-
# Only supports in txtwrap 1.1.
|
46
|
+
# Only supports in txtwrap 1.1.1+
|
47
47
|
align_info = align(
|
48
48
|
text=text,
|
49
49
|
width=width,
|
@@ -51,8 +51,8 @@ def render_wrap(
|
|
51
51
|
sizefunc=font.size,
|
52
52
|
method=method,
|
53
53
|
alignment=alignment,
|
54
|
-
use_max_width=use_max_width,
|
55
54
|
preserve_empty=preserve_empty,
|
55
|
+
use_min_width=use_min_width,
|
56
56
|
return_details=True
|
57
57
|
)
|
58
58
|
|
@@ -71,30 +71,35 @@ pygame.init()
|
|
71
71
|
pygame.display.set_caption("Lorem Ipsum")
|
72
72
|
|
73
73
|
running = True
|
74
|
-
|
74
|
+
wscrn, hscrn = 600, 600
|
75
|
+
screen = pygame.display.set_mode((wscrn, hscrn))
|
75
76
|
clock = pygame.time.Clock()
|
76
77
|
surface = render_wrap(
|
77
78
|
font=pygame.font.Font(None, 20),
|
78
79
|
text=LOREM_IPSUM_P,
|
79
|
-
width=
|
80
|
+
width=wscrn,
|
80
81
|
antialias=True,
|
81
82
|
color='#ffffff',
|
82
83
|
background='#303030',
|
83
84
|
alignment='fill'
|
84
85
|
)
|
85
86
|
|
87
|
+
wsurf, hsurf = surface.get_size()
|
88
|
+
pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
|
89
|
+
|
86
90
|
while running:
|
87
91
|
for event in pygame.event.get():
|
88
92
|
if event.type == pygame.QUIT:
|
89
93
|
running = False
|
90
94
|
screen.fill('#000000')
|
91
|
-
screen.blit(surface,
|
95
|
+
screen.blit(surface, pos)
|
92
96
|
pygame.display.flip()
|
93
97
|
clock.tick(60)
|
94
98
|
```
|
95
99
|
|
96
100
|
## Print a wrap text to terminal🔡
|
97
101
|
```py
|
102
|
+
# Only supports in txtwrap 1.1.0+
|
98
103
|
from txtwrap import printwrap, LOREM_IPSUM_W
|
99
104
|
|
100
105
|
printwrap(LOREM_IPSUM_W, width=20, alignment='left')
|
@@ -111,8 +116,35 @@ printwrap(LOREM_IPSUM_W, width=20, alignment='fill')
|
|
111
116
|
from txtwrap import shorten, LOREM_IPSUM_S
|
112
117
|
|
113
118
|
short_lorem = shorten(LOREM_IPSUM_S, width=20, placeholder='…')
|
119
|
+
# Only supports in txtwrap 1.1.0+
|
114
120
|
test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
|
115
121
|
|
116
122
|
print(short_lorem)
|
117
123
|
print(test)
|
124
|
+
```
|
125
|
+
|
126
|
+
## Bonus🎁 - Print a colorfull text to terminal🔥
|
127
|
+
```py
|
128
|
+
# Run this code in a terminal that supports ansi characters
|
129
|
+
|
130
|
+
from re import compile
|
131
|
+
from random import randint
|
132
|
+
from txtwrap import printwrap, LOREM_IPSUM_P
|
133
|
+
|
134
|
+
# Set the text to be printed here
|
135
|
+
text = LOREM_IPSUM_P
|
136
|
+
|
137
|
+
remove_ansi_regex = compile(r'\x1b\[[0-9;]*[mK]').sub
|
138
|
+
|
139
|
+
def ralen(s: str) -> int:
|
140
|
+
return len(remove_ansi_regex('', s))
|
141
|
+
|
142
|
+
while True:
|
143
|
+
# Only supports in txtwrap 1.1.0+
|
144
|
+
printwrap(
|
145
|
+
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
|
146
|
+
end='\x1b[H\x1b[J',
|
147
|
+
alignment='fill',
|
148
|
+
lenfunc=ralen
|
149
|
+
)
|
118
150
|
```
|
@@ -5,7 +5,7 @@ with open('README.md', encoding='utf-8') as readme:
|
|
5
5
|
|
6
6
|
setup(
|
7
7
|
name='txtwrap',
|
8
|
-
version='1.1.
|
8
|
+
version='1.1.1',
|
9
9
|
description='A simple text wrapping tool.',
|
10
10
|
long_description=long_description,
|
11
11
|
long_description_content_type='text/markdown',
|
@@ -14,8 +14,8 @@ setup(
|
|
14
14
|
python_requires='>=3.8',
|
15
15
|
packages=find_packages(),
|
16
16
|
include_package_data=True,
|
17
|
-
keywords=['wrap', 'wrapper', 'wrapping', '
|
18
|
-
'simple wrap', 'align', 'aligner', 'aligning'],
|
17
|
+
keywords=['wrap', 'wrapper', 'wrapping', 'wrapped', 'wrapping tool', 'text wrap',
|
18
|
+
'text wrapper', 'simple wrap', 'align', 'aligner', 'aligning', 'aligned'],
|
19
19
|
classifiers=[
|
20
20
|
'Programming Language :: Python :: 3',
|
21
21
|
'Programming Language :: Python :: 3.8',
|
@@ -5,14 +5,15 @@ A simple text wrapping tool
|
|
5
5
|
# Supports only in Python 3.8+
|
6
6
|
|
7
7
|
from .txtwrap import (
|
8
|
+
version,
|
8
9
|
LOREM_IPSUM_W, LOREM_IPSUM_S, LOREM_IPSUM_P,
|
9
10
|
mono, word, wrap, align, fillstr, printwrap,
|
10
11
|
shorten
|
11
12
|
)
|
12
13
|
|
13
|
-
__version__ =
|
14
|
+
__version__ = version
|
15
|
+
__author__ = 'azzammuhyala'
|
14
16
|
__license__ = 'MIT'
|
15
|
-
__author__ = 'Azzam Muhyala'
|
16
17
|
__all__ = [
|
17
18
|
'LOREM_IPSUM_W',
|
18
19
|
'LOREM_IPSUM_S',
|
@@ -1,5 +1,5 @@
|
|
1
1
|
from argparse import ArgumentParser
|
2
|
-
from txtwrap import printwrap, shorten
|
2
|
+
from txtwrap import version, printwrap, shorten
|
3
3
|
|
4
4
|
parser = ArgumentParser(
|
5
5
|
description='Command-line tool for wrapping, aligning, or shortening text.'
|
@@ -11,19 +11,26 @@ parser.add_argument(
|
|
11
11
|
help='Text to be wrapped, aligned, or shorted'
|
12
12
|
)
|
13
13
|
|
14
|
+
parser.add_argument(
|
15
|
+
'-v', '--version',
|
16
|
+
action='version',
|
17
|
+
version=version,
|
18
|
+
help='Show the version of the txtwrap'
|
19
|
+
)
|
20
|
+
|
14
21
|
parser.add_argument(
|
15
22
|
'-f', '--fill',
|
16
23
|
type=str,
|
17
24
|
default=' ',
|
18
|
-
metavar='<str 1
|
19
|
-
help='Fill character (default:
|
25
|
+
metavar='<str (1 character)>',
|
26
|
+
help='Fill character (default: " ")'
|
20
27
|
)
|
21
28
|
|
22
29
|
parser.add_argument(
|
23
30
|
'-w', '--width',
|
24
31
|
type=int,
|
25
32
|
default=None,
|
26
|
-
metavar='<
|
33
|
+
metavar='<int>',
|
27
34
|
help='Width of the text wrapping (default: current width terminal or 70)'
|
28
35
|
)
|
29
36
|
|
@@ -33,7 +40,7 @@ parser.add_argument(
|
|
33
40
|
choices={'word', 'mono', 'shorten'},
|
34
41
|
default='word',
|
35
42
|
metavar='{word|mono|shorten}',
|
36
|
-
help='Method to be applied to the text (default: word)'
|
43
|
+
help='Method to be applied to the text (default: "word")'
|
37
44
|
)
|
38
45
|
|
39
46
|
parser.add_argument(
|
@@ -42,7 +49,7 @@ parser.add_argument(
|
|
42
49
|
choices={'left', 'center', 'right', 'fill'},
|
43
50
|
default='left',
|
44
51
|
metavar='{left|center|right|fill}',
|
45
|
-
help='Alignment of the text (default: left)'
|
52
|
+
help='Alignment of the text (default: "left")'
|
46
53
|
)
|
47
54
|
|
48
55
|
parser.add_argument(
|
@@ -55,7 +62,7 @@ parser.add_argument(
|
|
55
62
|
'-s', '--start',
|
56
63
|
type=int,
|
57
64
|
default=0,
|
58
|
-
metavar='<
|
65
|
+
metavar='<int>',
|
59
66
|
help='start index of the text to be shorten (default: 0)'
|
60
67
|
)
|
61
68
|
|
@@ -64,13 +71,35 @@ parser.add_argument(
|
|
64
71
|
type=str,
|
65
72
|
default='...',
|
66
73
|
metavar='<str>',
|
67
|
-
help='Placeholder to be used when shortening the text (default: ...)'
|
74
|
+
help='Placeholder to be used when shortening the text (default: "...")'
|
75
|
+
)
|
76
|
+
|
77
|
+
parser.add_argument(
|
78
|
+
'-r', '--no-strip',
|
79
|
+
action='store_false',
|
80
|
+
help='Do not strip the space in the text'
|
68
81
|
)
|
69
82
|
|
70
83
|
args = parser.parse_args()
|
71
84
|
|
72
85
|
if args.method == 'shorten':
|
73
|
-
|
86
|
+
from os import get_terminal_size
|
87
|
+
|
88
|
+
if args.width is None:
|
89
|
+
try:
|
90
|
+
args.width = get_terminal_size().columns
|
91
|
+
except:
|
92
|
+
args.width = 70
|
93
|
+
|
94
|
+
print(
|
95
|
+
shorten(
|
96
|
+
text=args.text,
|
97
|
+
width=args.width,
|
98
|
+
start=args.start,
|
99
|
+
placeholder=args.placeholder,
|
100
|
+
strip_space=args.no_strip
|
101
|
+
)
|
102
|
+
)
|
74
103
|
else:
|
75
104
|
printwrap(
|
76
105
|
args.text,
|
@@ -3,6 +3,7 @@ from os import get_terminal_size
|
|
3
3
|
from re import compile
|
4
4
|
|
5
5
|
hyphenated_regex = compile(r'(?<=-)(?=(?!-).)')
|
6
|
+
version = '1.1.1'
|
6
7
|
|
7
8
|
LOREM_IPSUM_W = 'Lorem ipsum odor amet, consectetuer adipiscing elit.'
|
8
9
|
LOREM_IPSUM_S = ('Lorem ipsum odor amet, consectetuer adipiscing elit. In malesuada eros natoque '
|
@@ -198,8 +199,8 @@ def align(
|
|
198
199
|
sizefunc: Callable[[str], Tuple[Union[int, float], Union[int, float]]] = lambda s : (len(s), 1),
|
199
200
|
method: Literal['mono', 'word'] = 'word',
|
200
201
|
alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
|
201
|
-
use_max_width: bool = True,
|
202
202
|
preserve_empty: bool = True,
|
203
|
+
use_min_width: bool = True,
|
203
204
|
return_details: bool = False
|
204
205
|
|
205
206
|
) -> List[Union[Tuple[Union[int, float], Union[int, float], str],
|
@@ -226,11 +227,11 @@ def align(
|
|
226
227
|
'left', 'center', 'right',
|
227
228
|
or 'fill'.
|
228
229
|
Defaults to 'left'.
|
229
|
-
use_max_width (bool, optional): Whether to use the maximum width of the wrapped text.
|
230
|
-
Defaults to True.
|
231
230
|
preserve_empty (bool, optional): Whether to preserve empty lines. Defaults to True.
|
232
|
-
|
233
|
-
|
231
|
+
use_min_width (bool, optional): Whether to use the manimum width of the wrapped text.
|
232
|
+
Defaults to True.
|
233
|
+
return_details (bool, optional): Whether to return the aligned text, wrapped text, and
|
234
|
+
the size.
|
234
235
|
Defaults to False.
|
235
236
|
|
236
237
|
Returns:
|
@@ -238,9 +239,8 @@ def align(
|
|
238
239
|
dict[Literal['aligned', 'wrapped', 'size'], Any]]: A list of tuples containing the
|
239
240
|
position and content of each line.
|
240
241
|
If return_details, a dictionary
|
241
|
-
containing the wrapped text,
|
242
|
-
the
|
243
|
-
the maximum height is returned.
|
242
|
+
containing the wrapped text, and
|
243
|
+
the size is returned.
|
244
244
|
"""
|
245
245
|
|
246
246
|
assert isinstance(linegap, (int, float)), "linegap must be an integer or float"
|
@@ -253,54 +253,49 @@ def align(
|
|
253
253
|
aligned_positions = []
|
254
254
|
offset_y = 0
|
255
255
|
|
256
|
-
if
|
257
|
-
max_width = max(
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
'wrapped': wrapped
|
262
|
-
}
|
256
|
+
if use_min_width:
|
257
|
+
max_width = max(size[0] for size in size_wrapped.values())
|
258
|
+
use_width = max_width
|
259
|
+
else:
|
260
|
+
use_width = width
|
263
261
|
|
264
262
|
if alignment == 'left':
|
265
263
|
for i, line in enumerate(wrapped):
|
266
|
-
|
264
|
+
height_line = size_wrapped[i][1]
|
267
265
|
aligned_positions.append((0, offset_y, line))
|
268
266
|
offset_y += height_line + linegap
|
269
267
|
|
270
268
|
elif alignment == 'center':
|
271
269
|
for i, line in enumerate(wrapped):
|
272
270
|
width_line, height_line = size_wrapped[i]
|
273
|
-
aligned_positions.append(
|
274
|
-
(((max_width if use_max_width else width) - width_line) / 2, offset_y, line)
|
275
|
-
)
|
271
|
+
aligned_positions.append(((use_width - width_line) / 2, offset_y, line))
|
276
272
|
offset_y += height_line + linegap
|
277
273
|
|
278
274
|
elif alignment == 'right':
|
279
275
|
for i, line in enumerate(wrapped):
|
280
276
|
width_line, height_line = size_wrapped[i]
|
281
|
-
aligned_positions.append(
|
282
|
-
((max_width if use_max_width else width) - width_line, offset_y, line)
|
283
|
-
)
|
277
|
+
aligned_positions.append((use_width - width_line, offset_y, line))
|
284
278
|
offset_y += height_line + linegap
|
285
279
|
|
286
280
|
elif alignment == 'fill':
|
281
|
+
no_spaces = True
|
287
282
|
for i, line in enumerate(wrapped):
|
288
|
-
|
289
|
-
offset_x = 0
|
283
|
+
height_line = size_wrapped[i][1]
|
290
284
|
words = line.split()
|
291
285
|
total_words = len(words)
|
292
|
-
|
293
|
-
|
294
|
-
|
286
|
+
word_widths = {i: sizefunc(w)[0] for i, w in enumerate(words)}
|
287
|
+
extra_space = width - sum(word_widths.values())
|
288
|
+
offset_x = 0
|
295
289
|
|
296
290
|
if total_words > 1:
|
297
291
|
space_between_words = extra_space / (total_words - 1)
|
292
|
+
no_spaces = False
|
298
293
|
else:
|
299
294
|
space_between_words = extra_space
|
300
295
|
|
301
296
|
for i, w in enumerate(words):
|
302
297
|
aligned_positions.append((offset_x, offset_y, w))
|
303
|
-
offset_x +=
|
298
|
+
offset_x += word_widths[i] + space_between_words
|
304
299
|
|
305
300
|
offset_y += height_line + linegap
|
306
301
|
|
@@ -308,19 +303,21 @@ def align(
|
|
308
303
|
raise ValueError(f"{alignment=} is invalid, must be 'left', 'center', 'right', or 'fill'")
|
309
304
|
|
310
305
|
if return_details:
|
311
|
-
if alignment == 'fill':
|
312
|
-
if
|
313
|
-
size_width =
|
306
|
+
if use_min_width and alignment == 'fill':
|
307
|
+
if no_spaces:
|
308
|
+
size_width = max_width
|
314
309
|
elif text:
|
315
310
|
size_width = width
|
316
311
|
else:
|
317
312
|
size_width = 0
|
318
313
|
else:
|
319
|
-
size_width =
|
320
|
-
|
321
|
-
inforesult['size'] = (size_width, offset_y - linegap)
|
314
|
+
size_width = use_width
|
322
315
|
|
323
|
-
return
|
316
|
+
return {
|
317
|
+
'aligned': aligned_positions,
|
318
|
+
'wrapped': wrapped,
|
319
|
+
'size': (size_width, offset_y - linegap)
|
320
|
+
}
|
324
321
|
|
325
322
|
return aligned_positions
|
326
323
|
|
@@ -1,10 +1,10 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: txtwrap
|
3
|
-
Version: 1.1.
|
3
|
+
Version: 1.1.1
|
4
4
|
Summary: A simple text wrapping tool.
|
5
5
|
Author: azzammuhyala
|
6
6
|
License: MIT
|
7
|
-
Keywords: wrap,wrapper,wrapping,wrapping tool,text wrap,text wrapper,simple wrap,align,aligner,aligning
|
7
|
+
Keywords: wrap,wrapper,wrapping,wrapped,wrapping tool,text wrap,text wrapper,simple wrap,align,aligner,aligning,aligned
|
8
8
|
Classifier: Programming Language :: Python :: 3
|
9
9
|
Classifier: Programming Language :: Python :: 3.8
|
10
10
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
@@ -25,9 +25,9 @@ All constants and functions❕:
|
|
25
25
|
- `word`
|
26
26
|
- `wrap`
|
27
27
|
- `align` (Updated!)
|
28
|
-
- `fillstr`
|
29
|
-
- `printwrap`
|
30
|
-
- `shorten`
|
28
|
+
- `fillstr`
|
29
|
+
- `printwrap`
|
30
|
+
- `shorten`
|
31
31
|
|
32
32
|
Mod `python -m txtwrap` Commands❗:
|
33
33
|
```shell
|
@@ -43,7 +43,7 @@ import pygame
|
|
43
43
|
|
44
44
|
def render_wrap(
|
45
45
|
|
46
|
-
font: pygame.
|
46
|
+
font: pygame.Font,
|
47
47
|
text: str,
|
48
48
|
width: int,
|
49
49
|
antialias: bool,
|
@@ -52,12 +52,12 @@ def render_wrap(
|
|
52
52
|
linegap: int = 0,
|
53
53
|
alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
|
54
54
|
method: Literal['word', 'mono'] = 'word',
|
55
|
-
|
56
|
-
|
55
|
+
preserve_empty: bool = True,
|
56
|
+
use_min_width: bool = True
|
57
57
|
|
58
58
|
) -> pygame.Surface:
|
59
59
|
|
60
|
-
# Only supports in txtwrap 1.1.
|
60
|
+
# Only supports in txtwrap 1.1.1+
|
61
61
|
align_info = align(
|
62
62
|
text=text,
|
63
63
|
width=width,
|
@@ -65,8 +65,8 @@ def render_wrap(
|
|
65
65
|
sizefunc=font.size,
|
66
66
|
method=method,
|
67
67
|
alignment=alignment,
|
68
|
-
use_max_width=use_max_width,
|
69
68
|
preserve_empty=preserve_empty,
|
69
|
+
use_min_width=use_min_width,
|
70
70
|
return_details=True
|
71
71
|
)
|
72
72
|
|
@@ -85,30 +85,35 @@ pygame.init()
|
|
85
85
|
pygame.display.set_caption("Lorem Ipsum")
|
86
86
|
|
87
87
|
running = True
|
88
|
-
|
88
|
+
wscrn, hscrn = 600, 600
|
89
|
+
screen = pygame.display.set_mode((wscrn, hscrn))
|
89
90
|
clock = pygame.time.Clock()
|
90
91
|
surface = render_wrap(
|
91
92
|
font=pygame.font.Font(None, 20),
|
92
93
|
text=LOREM_IPSUM_P,
|
93
|
-
width=
|
94
|
+
width=wscrn,
|
94
95
|
antialias=True,
|
95
96
|
color='#ffffff',
|
96
97
|
background='#303030',
|
97
98
|
alignment='fill'
|
98
99
|
)
|
99
100
|
|
101
|
+
wsurf, hsurf = surface.get_size()
|
102
|
+
pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
|
103
|
+
|
100
104
|
while running:
|
101
105
|
for event in pygame.event.get():
|
102
106
|
if event.type == pygame.QUIT:
|
103
107
|
running = False
|
104
108
|
screen.fill('#000000')
|
105
|
-
screen.blit(surface,
|
109
|
+
screen.blit(surface, pos)
|
106
110
|
pygame.display.flip()
|
107
111
|
clock.tick(60)
|
108
112
|
```
|
109
113
|
|
110
114
|
## Print a wrap text to terminal🔡
|
111
115
|
```py
|
116
|
+
# Only supports in txtwrap 1.1.0+
|
112
117
|
from txtwrap import printwrap, LOREM_IPSUM_W
|
113
118
|
|
114
119
|
printwrap(LOREM_IPSUM_W, width=20, alignment='left')
|
@@ -125,8 +130,35 @@ printwrap(LOREM_IPSUM_W, width=20, alignment='fill')
|
|
125
130
|
from txtwrap import shorten, LOREM_IPSUM_S
|
126
131
|
|
127
132
|
short_lorem = shorten(LOREM_IPSUM_S, width=20, placeholder='…')
|
133
|
+
# Only supports in txtwrap 1.1.0+
|
128
134
|
test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
|
129
135
|
|
130
136
|
print(short_lorem)
|
131
137
|
print(test)
|
132
138
|
```
|
139
|
+
|
140
|
+
## Bonus🎁 - Print a colorfull text to terminal🔥
|
141
|
+
```py
|
142
|
+
# Run this code in a terminal that supports ansi characters
|
143
|
+
|
144
|
+
from re import compile
|
145
|
+
from random import randint
|
146
|
+
from txtwrap import printwrap, LOREM_IPSUM_P
|
147
|
+
|
148
|
+
# Set the text to be printed here
|
149
|
+
text = LOREM_IPSUM_P
|
150
|
+
|
151
|
+
remove_ansi_regex = compile(r'\x1b\[[0-9;]*[mK]').sub
|
152
|
+
|
153
|
+
def ralen(s: str) -> int:
|
154
|
+
return len(remove_ansi_regex('', s))
|
155
|
+
|
156
|
+
while True:
|
157
|
+
# Only supports in txtwrap 1.1.0+
|
158
|
+
printwrap(
|
159
|
+
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
|
160
|
+
end='\x1b[H\x1b[J',
|
161
|
+
alignment='fill',
|
162
|
+
lenfunc=ralen
|
163
|
+
)
|
164
|
+
```
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|