A simple text wrapping tool.
Project description
TxTWrap🔡
A tool for wrapping a text.🔨
⚠️All documents are in the each module.⚠️
All constants and functions❕:
LOREM_IPSUM_WLOREM_IPSUM_SLOREM_IPSUM_Pmonowordwrapalign(Updated!)fillstrprintwrapshorten
Mod python -m txtwrap Commands❗:
python -m txtwrap --help
Examples❓:
Render a wrap text in PyGame🎮
from typing import Literal, Optional
from txtwrap import align, LOREM_IPSUM_P
import pygame
def render_wrap(
font: pygame.Font,
text: str,
width: int,
antialias: bool,
color: pygame.Color,
background: Optional[pygame.Color] = None,
linegap: int = 0,
alignment: Literal['left', 'center', 'right', 'fill'] = 'left',
method: Literal['word', 'mono'] = 'word',
preserve_empty: bool = True,
use_min_width: bool = True
) -> pygame.Surface:
# Only supports in txtwrap 1.1.1+
align_info = align(
text=text,
width=width,
linegap=linegap,
sizefunc=font.size,
method=method,
alignment=alignment,
preserve_empty=preserve_empty,
use_min_width=use_min_width,
return_details=True
)
surface = pygame.Surface(align_info['size'], pygame.SRCALPHA)
if background is not None:
surface.fill(background)
for x, y, text in align_info['aligned']:
surface.blit(font.render(text, antialias, color), (x, y))
return surface
# Example usage:
pygame.init()
pygame.display.set_caption("Lorem Ipsum")
running = True
wscrn, hscrn = 600, 600
screen = pygame.display.set_mode((wscrn, hscrn))
clock = pygame.time.Clock()
surface = render_wrap(
font=pygame.font.Font(None, 20),
text=LOREM_IPSUM_P,
width=wscrn,
antialias=True,
color='#ffffff',
background='#303030',
alignment='fill'
)
wsurf, hsurf = surface.get_size()
pos = ((wscrn - wsurf) / 2, (hscrn - hsurf) / 2)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill('#000000')
screen.blit(surface, pos)
pygame.display.flip()
clock.tick(60)
Print a wrap text to terminal🔡
# Only supports in txtwrap 1.1.0+
from txtwrap import printwrap, LOREM_IPSUM_W
printwrap(LOREM_IPSUM_W, width=20, alignment='left')
print('=' * 20)
printwrap(LOREM_IPSUM_W, width=20, alignment='center')
print('=' * 20)
printwrap(LOREM_IPSUM_W, width=20, alignment='right')
print('=' * 20)
printwrap(LOREM_IPSUM_W, width=20, alignment='fill')
Short a long text🔤
from txtwrap import shorten, LOREM_IPSUM_S
short_lorem = shorten(LOREM_IPSUM_S, width=20, placeholder='…')
# Only supports in txtwrap 1.1.0+
test = shorten(' Helllo, \t\r\n World!! \f', width=20, placeholder='…', strip_space=True)
print(short_lorem)
print(test)
Bonus🎁 - Print a colorfull text to terminal🔥
# Run this code in a terminal that supports ansi characters
from re import compile
from random import randint
from txtwrap import printwrap, LOREM_IPSUM_P
# Set the text to be printed here
text = LOREM_IPSUM_P
remove_ansi_regex = compile(r'\x1b\[[0-9;]*[mK]').sub
def ralen(s: str) -> int:
return len(remove_ansi_regex('', s))
while True:
# Only supports in txtwrap 1.1.0+
printwrap(
''.join(f'\x1b[{randint(31, 36)}m{char}' for char in text) + '\x1b[0m',
end='\x1b[H\x1b[J',
alignment='fill',
lenfunc=ralen
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
txtwrap-1.1.1.tar.gz
(9.8 kB
view details)
File details
Details for the file txtwrap-1.1.1.tar.gz.
File metadata
- Download URL: txtwrap-1.1.1.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a300598ab32071a5a1a07bcd118e18079981d093b503fd13f420eea6238c2d09
|
|
| MD5 |
6b9f8b6f439d036dd8bd6ed75c45e7b3
|
|
| BLAKE2b-256 |
82b417a2424da892fe4831d5d30a6e4965be9fcd325867faa628372321911bb5
|