Skip to main content

Advanced text formatting and alignment for terminal output

Project description

Termalign

Termalign is a lightweight Python module for advanced text formatting and alignment in terminals, logs, CLI output, and simple plain-text reports.

It provides intuitive functions and classes to align, format, wrap, colorize, and decorate text with optional borders and multi-column layouts.


Features

  • Align text: left, right, center, justify
  • Wrap and indent multiline text
  • Add decorative or structural ASCII/Unicode borders
  • Apply color and bold styling (ANSI-compatible)
  • Define reusable formatting styles
  • Easily create multi-column text layouts
  • Simple API (format_table) for quick use
  • Advanced API (TextFormatter, ColumnBuilder) for full control

Installation

# Clone or copy this module into your project
# or install manually via pip (in development)
pip install -e .

Usage Examples

Align a single line

from termalign import align_line

print(align_line("Hello", width=20, align="center"))
# --> "       Hello        "

Format a block of text

from termalign import format_block

text = "Python is a powerful, readable, and versatile programming language."
print(format_block(text, width=40, align="justify"))
#Python  is  a  powerful,  readable,  and
#versatile      programming     language.

Use a reusable formatter

from termalign import TextFormatter

fmt = TextFormatter(width=50, align="center", indent=4, border=True)
print(fmt.format("Centered and bordered text"))
#    +------------------------------------------------+
#    |           Centered and bordered text           |
#    +------------------------------------------------+

Add a border (ASCII, Unicode, Double, Dashed, DoubleDashed)

from termalign.utils import add_border

print(add_border("Hello World", style="unicode"))
#┌─────────────┐
#│ Hello World │
#└─────────────┘

Apply color

from termalign.utils import colorize

print(colorize("Success!", "green", bold=True))
print(colorize("Warning!", "yellow"))
print(colorize("Error!", "red"))

Format a Table (the easiest way!)

from termalign import format_table

print(format_table(
    texts=[
        "Full name of the participant",
        "Subject age",
        "Current city of residence"
    ],
    widths=[30, 20, 40],
    aligns=["left", "center", "right"],
    borders=True
))
#+--------------------------------+  +----------------------+  +------------------------------------------+
#| Full name of the participant   |  |     Subject age      |  |                Current city of residence |
#+--------------------------------+  +----------------------+  +------------------------------------------+

Advanced Usage with ColumnBuilder

from termalign import ColumnBuilder, ColumnLayout

layout = ColumnLayout.from_builders(
    ColumnBuilder("Left Column").width(30).align("left").border(),
    ColumnBuilder("Center Column").width(20).align("center").border(),
    ColumnBuilder("Right Column").width(40).align("right").indent(2).border()
)

print(layout.format())
#+--------------------------------+  +----------------------+    +----------------------------------------+
#| Left Column                    |  |    Center Column     |    |                           Right Column |
#+--------------------------------+  +----------------------+    +----------------------------------------+

Tools Reference

Core Functions

  • align_line(text, width, align): Align a single line.
  • format_block(text, width, align): Wrap and align multiple lines.
  • wrap_and_indent(text, width, indent): Wrap text with paragraph-style indentation.
  • format_columns(blocks, widths, align): Combine pre-formatted blocks into columns.

Formatting Utilities

  • pad_line(text, width): Manually pad a string.
  • indent_lines(text, indent): Add indentation to each line.
  • add_border(text, style): Wrap text with a border (ascii, unicode, double, dashed, DoubleDashed).
  • colorize(text, color, bold=False): Apply terminal ANSI color and bold style.

High-Level Interfaces

  • TextFormatter(width, align, indent, border): Object for reusable formatting config.
  • format_table(...): Quick function to build table with one-liners.
  • ColumnBuilder(text): Fluent builder for defining a column.
  • ColumnLayout.from_builders(...): Combine columns with full control.

License

MIT License

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

termalign-0.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

termalign-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file termalign-0.1.1.tar.gz.

File metadata

  • Download URL: termalign-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for termalign-0.1.1.tar.gz
Algorithm Hash digest
SHA256 282507629d520b8021a9a7445e548ec40759afbf005d665791e2cf0f8934fc98
MD5 921a43d332e12ba8550e9cb8aa420fb7
BLAKE2b-256 db21e41d26e2912aa48d81a1fa0b9402a6f5acc1b12755f33145a072a11d1b26

See more details on using hashes here.

File details

Details for the file termalign-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: termalign-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for termalign-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d132b8f19074363605398cda25d9b77f9dc6d222d1fee6f52e18b79741d8d759
MD5 644c858b0043da622467dc6063750f11
BLAKE2b-256 17153fbde7baf3d2e36845ff9076acbd95001f93036036fb414209714c53145b

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