Skip to main content

A Python library providing a styleText function similar to Node.js util.styleText

Project description

styletext

A Python library providing a style_text() function similar to Node.js util.styleText(). Style your terminal output with ANSI escape codes using a simple, intuitive API.

Installation

uv add styletext

Or with pip:

pip install styletext

Or simply copy styletext.py to your project - zero dependencies required!

Usage

Basic Usage

from styletext import style_text

# Single style
print(style_text('red', 'Error message'))
print(style_text('green', 'Success message'))

# Multiple styles
print(style_text(['bold', 'blue'], 'Important text'))
print(style_text(['bgYellow', 'black', 'bold'], 'Warning'))

Chainable API (Bonus)

from styletext import styled

print(styled('Hello World').red.bold)
print(styled('Success!').green.underline)
print(styled('Warning').bgYellow.black)

Available Styles

Colors

  • black, red, green, yellow, blue, magenta, cyan, white, gray/grey
  • brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, brightWhite

Background Colors

  • bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite, bgGray/bgGrey
  • bgBrightRed, bgBrightGreen, bgBrightYellow, bgBrightBlue, bgBrightMagenta, bgBrightCyan, bgBrightWhite

Text Styles

  • bold, dim, italic, underline, blink, inverse, hidden, strikethrough

Examples

Log Messages

from styletext import style_text

# Success
print(style_text(['bold', 'green'], '✓') + ' Test passed')

# Error
print(style_text(['bold', 'red'], '✗') + ' Test failed')

# Info
print(style_text(['bgBlue', 'white', 'bold'], ' INFO ') + ' Server started')

# Warning
print(style_text(['bgYellow', 'black', 'bold'], ' WARN ') + ' Deprecated API')

# Error with details
error_msg = (
    style_text(['bold', 'red'], 'Error: ') +
    'Could not find file ' +
    style_text(['italic', 'cyan'], 'config.json')
)
print(error_msg)

Progress Indicators

from styletext import styled

print(styled('⠋').cyan + ' Loading...')
print(styled('⠙').cyan + ' Processing...')
print(styled('✓').green.bold + ' Complete!')

Comparison with Node.js

Node.js:

import { styleText } from 'node:util';

console.log(styleText('red', 'Error!'));
console.log(styleText(['bold', 'blue'], 'Important'));

Python (this library):

from styletext import style_text

print(style_text('red', 'Error!'))
print(style_text(['bold', 'blue'], 'Important'))

Requirements

  • Python 3.11+
  • Zero external dependencies - just pure Python and ANSI escape codes!

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

styletext-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

styletext-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file styletext-1.0.0.tar.gz.

File metadata

  • Download URL: styletext-1.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for styletext-1.0.0.tar.gz
Algorithm Hash digest
SHA256 79f4075a8ef37979496aea4164d08a258ca7b13ad1fc9cb4abc123f3237f5172
MD5 57fc672dcf86d571f08fb58b99ae24cd
BLAKE2b-256 a0e6ceb5f9186b31bc2e8022bb1b0621017e324d6783629513f7837054cb3431

See more details on using hashes here.

Provenance

The following attestation bundles were made for styletext-1.0.0.tar.gz:

Publisher: publish.yaml on goggle555/styletext-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file styletext-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: styletext-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for styletext-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9ba1965ac6af3b360d720ccf5454a22c2b9354301d4f33a33ccc3f8ffeb6308
MD5 834c78f5b691c65b62111b8c05a6118a
BLAKE2b-256 e7c3f7943d9e9677bcceee9e6d74f873733b9da3fad1dd8bda4f803668c33e8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for styletext-1.0.0-py3-none-any.whl:

Publisher: publish.yaml on goggle555/styletext-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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