Skip to main content

styled-str

styled-str is a package for creating strings styled with ANSI escape sequences. The motivation for writing this package was that many existing string-styling modules do not implement logical string behavior, like properly formatting the string, indexing, containment, all of which are useless if they don't ignore the styling.

The Styled object ignores styling and acts on just its content for the following operations:

For an example Styled s = Styled('Hello!', fg='red', style='italic')

  • length, e.g. len(s) => 6
  • formatting, e.g. f'{s:.3}' => 'Hel'
  • indexing, e.g. s[1] => 'e'
  • iteration, e.g. for c in s: => H, e, l, l, o, !
  • reversed, e.g. for c in reversed(s): => !, o, l, l, e, H
  • containment, e.g. only characters of "Hello!" are True for c in s

We would like to have a strings with the same content viewed as distinct though, and so Styled includes styling for the following operations:

For example Styled a = Styled('Hello!', fg='red'); b = Styled('Hello!', bg='blue')

  • equality, e.g. a == b => False
  • comparison, e.g. a < b => True

Installation

Install using pip (Python3):

pip3 install styled-str

Usage

Import the module and create a string with your desired styles:

from styledstr import Styled

s = Styled('Hello World!', fg='cyan', bg='white', style=['bold', 'underlined'])

# Print it out
print(s)

# Use it in a larger string
print("He said, '" + str(s) + "'")

# Format it worry-free!
print(f'Title: {s:>20}')

Behavior

In the following examples, variables prefixed with styled refer to Styled strings, while variables prefixed with regular refer to standard Python strings.

The following builtin functions all work as they normally would on only the underlying string contents:

  • repr(styled)

  • int(styled)

  • float(styled)

  • complex(styled)

  • hash(styled)

  • str(styled) returns a Python string that includes escape sequences for the specified formatting.

Metadata

Release files for styled-str 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for styled-str 0.1.0
File Size Uploaded
styled-str-0.1.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for styled-str 0.1.0
File Interpreter ABI Platform
styled_str-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / styled-str-0.1.0.tar.gz

Download URL styled-str-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
498e7836228d85217528b4c42fc1a8985a856f9ee97993152dd558bc110cfb63
BLAKE2b-256 checksum
How to use checksums
76e0882d990b82807850460e17fd594245778615bb050d78363690a1c0dfc3c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.7

Release files / styled_str-0.1.0-py3-none-any.whl

Download URL styled_str-0.1.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4c93315b597d0ee1ddeb2897b79258c039b2fae767456badd5b1548cd4eba6b
BLAKE2b-256 checksum
How to use checksums
5ee090e895cebd784def04a6fb5d1323b7d22b815c511f893bf376e19b7b198f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page