txtwrap 1.1.0__tar.gz → 1.2.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.
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: txtwrap
3
- Version: 1.1.0
3
+ Version: 1.2.0
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
@@ -18,16 +18,18 @@ A tool for wrapping a text.🔨
18
18
  > **⚠️All documents are in the each module.⚠️**
19
19
 
20
20
  All constants and functions❕:
21
- - `LOREM_IPSUM_W`
22
- - `LOREM_IPSUM_S`
23
- - `LOREM_IPSUM_P`
24
- - `mono`
25
- - `word`
26
- - `wrap`
27
- - `align` (Updated!)
28
- - `fillstr` (New!)
29
- - `printwrap` (New!)
30
- - `shorten` (Updated!)
21
+ - `LOREM_IPSUM_WORDS` (same as `LOREM_IPSUM_W`)
22
+ - `LOREM_IPSUM_SENTENCES` (same as `LOREM_IPSUM_S`)
23
+ - `LOREM_IPSUM_PARAGRAPHS` (same as `LOREM_IPSUM_P`)
24
+ - `mono` (Fixed)
25
+ - `word` (Fixed)
26
+ - `wrap` (Fixed)
27
+ - `align` (Updated)
28
+ - `fillstr` (Updated)
29
+ - `printwrap` (Updated)
30
+ - `indent` (New)
31
+ - `dedent` (New)
32
+ - `shorten` (Fixed)
31
33
 
32
34
  Mod `python -m txtwrap` Commands❗:
33
35
  ```shell
@@ -38,12 +40,12 @@ Examples❓:
38
40
  ## Render a wrap text in PyGame🎮
39
41
  ```py
40
42
  from typing import Literal, Optional
41
- from txtwrap import align, LOREM_IPSUM_P
43
+ from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
42
44
  import pygame
43
45
 
44
46
  def render_wrap(
45
47
 
46
- font: pygame.font.Font,
48
+ font: pygame.Font,
47
49
  text: str,
48
50
  width: int,
49
51
  antialias: bool,
@@ -52,21 +54,20 @@ def render_wrap(
52
54
  linegap: int = 0,
53
55
  alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
54
56
  method: Literal['word', 'mono'] = 'word',
55
- use_max_width: bool = True,
56
- preserve_empty: bool = True
57
+ preserve_empty: bool = True,
58
+ use_min_width: bool = True
57
59
 
58
60
  ) -> pygame.Surface:
59
61
 
60
- # Only supports in txtwrap 1.1.0+
61
62
  align_info = align(
62
- text=text,
63
+ text_or_wrapped=str(text),
63
64
  width=width,
64
65
  linegap=linegap,
65
66
  sizefunc=font.size,
66
67
  method=method,
67
68
  alignment=alignment,
68
- use_max_width=use_max_width,
69
69
  preserve_empty=preserve_empty,
70
+ use_min_width=use_min_width,
70
71
  return_details=True
71
72
  )
72
73
 
@@ -85,48 +86,77 @@ pygame.init()
85
86
  pygame.display.set_caption("Lorem Ipsum")
86
87
 
87
88
  running = True
88
- screen = pygame.display.set_mode((600, 510))
89
+ wscrn, hscrn = 600, 600
90
+ screen = pygame.display.set_mode((wscrn, hscrn))
89
91
  clock = pygame.time.Clock()
90
92
  surface = render_wrap(
91
93
  font=pygame.font.Font(None, 20),
92
- text=LOREM_IPSUM_P,
93
- width=screen.get_width(),
94
+ text=LOREM_IPSUM_PARAGRAPHS,
95
+ width=wscrn,
94
96
  antialias=True,
95
97
  color='#ffffff',
96
98
  background='#303030',
97
99
  alignment='fill'
98
100
  )
99
101
 
102
+ wsurf, hsurf = surface.get_size()
103
+ pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
104
+
100
105
  while running:
101
106
  for event in pygame.event.get():
102
107
  if event.type == pygame.QUIT:
103
108
  running = False
104
109
  screen.fill('#000000')
105
- screen.blit(surface, (0, 0))
110
+ screen.blit(surface, pos)
106
111
  pygame.display.flip()
107
112
  clock.tick(60)
108
113
  ```
109
114
 
110
115
  ## Print a wrap text to terminal🔡
111
116
  ```py
112
- from txtwrap import printwrap, LOREM_IPSUM_W
117
+ from txtwrap import printwrap, LOREM_IPSUM_WORDS
113
118
 
114
- printwrap(LOREM_IPSUM_W, width=20, alignment='left')
119
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='left')
115
120
  print('=' * 20)
116
- printwrap(LOREM_IPSUM_W, width=20, alignment='center')
121
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='center')
117
122
  print('=' * 20)
118
- printwrap(LOREM_IPSUM_W, width=20, alignment='right')
123
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='right')
119
124
  print('=' * 20)
120
- printwrap(LOREM_IPSUM_W, width=20, alignment='fill')
125
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='fill')
121
126
  ```
122
127
 
123
128
  ## Short a long text🔤
124
129
  ```py
125
- from txtwrap import shorten, LOREM_IPSUM_S
130
+ from txtwrap import shorten, LOREM_IPSUM_SENTENCES
126
131
 
127
- short_lorem = shorten(LOREM_IPSUM_S, width=20, placeholder='…')
132
+ short_lorem = shorten(LOREM_IPSUM_SENTENCES, width=20, placeholder='…')
128
133
  test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
129
134
 
130
135
  print(short_lorem)
131
136
  print(test)
132
137
  ```
138
+
139
+ ## Bonus🎁 - Print a colorfull text to terminal🔥
140
+ ```py
141
+ # Run this code in a terminal that supports ansi characters
142
+
143
+ from re import compile
144
+ from random import randint
145
+ from txtwrap import printwrap, LOREM_IPSUM_PARAGRAPHS
146
+
147
+ # Set the text to be printed here
148
+ text = LOREM_IPSUM_PARAGRAPHS
149
+
150
+ remove_ansi_regex = compile(r'\x1b\[(K|.*?m)').sub
151
+
152
+ def ralen(s: str) -> int:
153
+ return len(remove_ansi_regex('', s))
154
+
155
+ while True:
156
+ printwrap(
157
+ ''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
158
+ end='\x1b[H\x1b[J',
159
+ alignment='fill',
160
+ lenfunc=ralen
161
+ )
162
+ ```
@@ -0,0 +1,148 @@
1
+ # TxTWrap🔡
2
+ A tool for wrapping a text.🔨
3
+
4
+ > **⚠️All documents are in the each module.⚠️**
5
+
6
+ All constants and functions❕:
7
+ - `LOREM_IPSUM_WORDS` (same as `LOREM_IPSUM_W`)
8
+ - `LOREM_IPSUM_SENTENCES` (same as `LOREM_IPSUM_S`)
9
+ - `LOREM_IPSUM_PARAGRAPHS` (same as `LOREM_IPSUM_P`)
10
+ - `mono` (Fixed)
11
+ - `word` (Fixed)
12
+ - `wrap` (Fixed)
13
+ - `align` (Updated)
14
+ - `fillstr` (Updated)
15
+ - `printwrap` (Updated)
16
+ - `indent` (New)
17
+ - `dedent` (New)
18
+ - `shorten` (Fixed)
19
+
20
+ Mod `python -m txtwrap` Commands❗:
21
+ ```shell
22
+ python -m txtwrap --help
23
+ ```
24
+
25
+ Examples❓:
26
+ ## Render a wrap text in PyGame🎮
27
+ ```py
28
+ from typing import Literal, Optional
29
+ from txtwrap import align, LOREM_IPSUM_PARAGRAPHS
30
+ import pygame
31
+
32
+ def render_wrap(
33
+
34
+ font: pygame.Font,
35
+ text: str,
36
+ width: int,
37
+ antialias: bool,
38
+ color: pygame.Color,
39
+ background: Optional[pygame.Color] = None,
40
+ linegap: int = 0,
41
+ alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
42
+ method: Literal['word', 'mono'] = 'word',
43
+ preserve_empty: bool = True,
44
+ use_min_width: bool = True
45
+
46
+ ) -> pygame.Surface:
47
+
48
+ align_info = align(
49
+ text_or_wrapped=str(text),
50
+ width=width,
51
+ linegap=linegap,
52
+ sizefunc=font.size,
53
+ method=method,
54
+ alignment=alignment,
55
+ preserve_empty=preserve_empty,
56
+ use_min_width=use_min_width,
57
+ return_details=True
58
+ )
59
+
60
+ surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
61
+
62
+ if background is not None:
63
+ surface.fill(background)
64
+
65
+ for x, y, text in align_info['aligned']:
66
+ surface.blit(font.render(text, antialias, color), (x, y))
67
+
68
+ return surface
69
+
70
+ # Example usage:
71
+ pygame.init()
72
+ pygame.display.set_caption("Lorem Ipsum")
73
+
74
+ running = True
75
+ wscrn, hscrn = 600, 600
76
+ screen = pygame.display.set_mode((wscrn, hscrn))
77
+ clock = pygame.time.Clock()
78
+ surface = render_wrap(
79
+ font=pygame.font.Font(None, 20),
80
+ text=LOREM_IPSUM_PARAGRAPHS,
81
+ width=wscrn,
82
+ antialias=True,
83
+ color='#ffffff',
84
+ background='#303030',
85
+ alignment='fill'
86
+ )
87
+
88
+ wsurf, hsurf = surface.get_size()
89
+ pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
90
+
91
+ while running:
92
+ for event in pygame.event.get():
93
+ if event.type == pygame.QUIT:
94
+ running = False
95
+ screen.fill('#000000')
96
+ screen.blit(surface, pos)
97
+ pygame.display.flip()
98
+ clock.tick(60)
99
+ ```
100
+
101
+ ## Print a wrap text to terminal🔡
102
+ ```py
103
+ from txtwrap import printwrap, LOREM_IPSUM_WORDS
104
+
105
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='left')
106
+ print('=' * 20)
107
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='center')
108
+ print('=' * 20)
109
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='right')
110
+ print('=' * 20)
111
+ printwrap(LOREM_IPSUM_WORDS, width=20, alignment='fill')
112
+ ```
113
+
114
+ ## Short a long text🔤
115
+ ```py
116
+ from txtwrap import shorten, LOREM_IPSUM_SENTENCES
117
+
118
+ short_lorem = shorten(LOREM_IPSUM_SENTENCES, width=20, placeholder='…')
119
+ test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
120
+
121
+ print(short_lorem)
122
+ print(test)
123
+ ```
124
+
125
+ ## Bonus🎁 - Print a colorfull text to terminal🔥
126
+ ```py
127
+ # Run this code in a terminal that supports ansi characters
128
+
129
+ from re import compile
130
+ from random import randint
131
+ from txtwrap import printwrap, LOREM_IPSUM_PARAGRAPHS
132
+
133
+ # Set the text to be printed here
134
+ text = LOREM_IPSUM_PARAGRAPHS
135
+
136
+ remove_ansi_regex = compile(r'\x1b\[(K|.*?m)').sub
137
+
138
+ def ralen(s: str) -> int:
139
+ return len(remove_ansi_regex('', s))
140
+
141
+ while True:
142
+ printwrap(
143
+ ''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
144
+ end='\x1b[H\x1b[J',
145
+ alignment='fill',
146
+ lenfunc=ralen
147
+ )
148
+ ```
@@ -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.0',
8
+ version='1.2.0',
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', 'wrapping tool', 'text wrap', 'text wrapper',
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',
@@ -0,0 +1,31 @@
1
+ """
2
+ A simple text wrapping tool
3
+ """
4
+
5
+ # Supports only in Python 3.8+
6
+
7
+ from .txtwrap import (
8
+ version,
9
+ LOREM_IPSUM_WORDS, LOREM_IPSUM_SENTENCES, LOREM_IPSUM_PARAGRAPHS,
10
+ mono, word, wrap, align, fillstr, printwrap,
11
+ indent, dedent,
12
+ shorten
13
+ )
14
+
15
+ __version__ = version
16
+ __author__ = 'azzammuhyala'
17
+ __license__ = 'MIT'
18
+ __all__ = [
19
+ 'LOREM_IPSUM_WORDS',
20
+ 'LOREM_IPSUM_SENTENCES',
21
+ 'LOREM_IPSUM_PARAGRAPHS',
22
+ 'mono',
23
+ 'word',
24
+ 'wrap',
25
+ 'align',
26
+ 'fillstr',
27
+ 'printwrap',
28
+ 'indent',
29
+ 'dedent',
30
+ 'shorten'
31
+ ]
@@ -0,0 +1,133 @@
1
+ from argparse import ArgumentParser
2
+ from os import name, get_terminal_size
3
+ from txtwrap import version, printwrap, indent, dedent, shorten
4
+
5
+ if name == 'nt':
6
+ pyname = 'python|py'
7
+ elif name == 'posix':
8
+ pyname = 'python3'
9
+ else:
10
+ pyname = 'python'
11
+
12
+ parser = ArgumentParser(
13
+ prog='txtwrap',
14
+ description='Command-line tool for wrapping, aligning, or shortening text.',
15
+ epilog=f'for example: {pyname} -m txtwrap "Lorem ipsum odor amet, consectetuer adipiscing '
16
+ 'elit." -w 20 -m word -a center'
17
+ )
18
+
19
+ parser.add_argument(
20
+ 'text',
21
+ type=str,
22
+ help='Text to be wrapped, aligned, or shorted'
23
+ )
24
+
25
+ parser.add_argument(
26
+ '-v', '--version',
27
+ action='version',
28
+ version=version,
29
+ help='Show the version of the txtwrap'
30
+ )
31
+
32
+ parser.add_argument(
33
+ '-f', '--fill',
34
+ type=str,
35
+ default=' ',
36
+ metavar='<str (1 character)>',
37
+ help='Fill character (default: " ")'
38
+ )
39
+
40
+ parser.add_argument(
41
+ '-w', '--width',
42
+ type=int,
43
+ default=None,
44
+ metavar='<int>',
45
+ help='Width of the text wrapping (default: current width terminal or 70)'
46
+ )
47
+
48
+ parser.add_argument(
49
+ '-m', '--method',
50
+ type=str,
51
+ choices={'word', 'mono', 'indent', 'dedent', 'shorten'},
52
+ default='word',
53
+ metavar='{word|mono|indent|dedent|shorten}',
54
+ help='Method to be applied to the text (default: "word")'
55
+ )
56
+
57
+ parser.add_argument(
58
+ '-a', '--alignment',
59
+ type=str,
60
+ choices={'left', 'center', 'right', 'fill'},
61
+ default='left',
62
+ metavar='{left|center|right|fill}',
63
+ help='Alignment of the text (default: "left")'
64
+ )
65
+
66
+ parser.add_argument(
67
+ '-n', '--neglect-empty',
68
+ action='store_false',
69
+ help='Neglect empty lines in the text'
70
+ )
71
+
72
+ parser.add_argument(
73
+ '-x', '--prefix',
74
+ type=str,
75
+ default=None,
76
+ metavar='<str>',
77
+ help='Prefix to be added (indent) or remove (dedent) to the text'
78
+ )
79
+
80
+ parser.add_argument(
81
+ '-s', '--start',
82
+ type=int,
83
+ default=0,
84
+ metavar='<int>',
85
+ help='start index of the text to be shorten (default: 0)'
86
+ )
87
+
88
+ parser.add_argument(
89
+ '-p', '--placeholder',
90
+ type=str,
91
+ default='...',
92
+ metavar='<str>',
93
+ help='Placeholder to be used when shortening the text (default: "...")'
94
+ )
95
+
96
+ parser.add_argument(
97
+ '-r', '--no-strip',
98
+ action='store_false',
99
+ help='Do not strip the space in the text'
100
+ )
101
+
102
+ args = parser.parse_args()
103
+
104
+ if args.method == 'indent':
105
+ if args.prefix is None:
106
+ raise ValueError('The prefix (-x, --prefix) is required for the indent method')
107
+ print(indent(args.text, args.prefix))
108
+ elif args.method == 'dedent':
109
+ print(dedent(args.text, args.prefix))
110
+ elif args.method == 'shorten':
111
+ if args.width is None:
112
+ try:
113
+ args.width = get_terminal_size().columns
114
+ except:
115
+ args.width = 70
116
+ print(
117
+ shorten(
118
+ text=args.text,
119
+ width=args.width,
120
+ start=args.start,
121
+ placeholder=args.placeholder,
122
+ strip_space=args.no_strip
123
+ )
124
+ )
125
+ else:
126
+ printwrap(
127
+ args.text,
128
+ fill=args.fill,
129
+ width=args.width,
130
+ method=args.method,
131
+ alignment=args.alignment,
132
+ preserve_empty=args.neglect_empty
133
+ )