Skip to main content

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_WORDS (same as LOREM_IPSUM_W)
  • LOREM_IPSUM_SENTENCES (same as LOREM_IPSUM_S)
  • LOREM_IPSUM_PARAGRAPHS (same as LOREM_IPSUM_P)
  • mono (Fixed)
  • word (Fixed)
  • wrap (Fixed)
  • align (Updated)
  • fillstr (Updated)
  • printwrap (Updated)
  • indent (New)
  • dedent (New)
  • shorten (Fixed)

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_PARAGRAPHS
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:

    align_info = align(
        text_or_wrapped=str(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_PARAGRAPHS,
    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🔡

from txtwrap import printwrap, LOREM_IPSUM_WORDS

printwrap(LOREM_IPSUM_WORDS, width=20, alignment='left')
print('=' * 20)
printwrap(LOREM_IPSUM_WORDS, width=20, alignment='center')
print('=' * 20)
printwrap(LOREM_IPSUM_WORDS, width=20, alignment='right')
print('=' * 20)
printwrap(LOREM_IPSUM_WORDS, width=20, alignment='fill')

Short a long text🔤

from txtwrap import shorten, LOREM_IPSUM_SENTENCES

short_lorem = shorten(LOREM_IPSUM_SENTENCES, width=20, placeholder='…')
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_PARAGRAPHS

# Set the text to be printed here
text = LOREM_IPSUM_PARAGRAPHS

remove_ansi_regex = compile(r'\x1b\[(K|.*?m)').sub

def ralen(s: str) -> int:
    return len(remove_ansi_regex('', s))

while True:
    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


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.2.0.tar.gz (10.8 kB view details)

Uploaded Source

File details

Details for the file txtwrap-1.2.0.tar.gz.

File metadata

  • Download URL: txtwrap-1.2.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for txtwrap-1.2.0.tar.gz
Algorithm Hash digest
SHA256 16910979caf80d4b497e3f872258bc30142797bab96db4da42eee3272d063949
MD5 129c0c0dce417a5028a19ed8d1c62bfb
BLAKE2b-256 a8ee3b817c0383b8744a7a88dd5e9b1023e18128ca64144d408763f11fffc262

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page