Skip to main content

Text preprocessor for building strings for the rich library

Project description

rich-string-builder

Text preprocessor for building strings for the rich library

Background

This came about because I was annoyed at the way we needed to insert text into rich to make it reflect our intended stylistic choices. Take the following example:

console.print(
    f"Subnet: [{COLOR_PALETTE['GENERAL']['SUBHEADING']}]{netuid_}[/{COLOR_PALETTE['GENERAL']['SUBHEADING']}] "
    f"Stake:\n"
    f"  [blue]{current_stake}[/blue] "
    f":arrow_right: "[{COLOR_PALETTE['STAKE']['STAKE_AMOUNT']}]{new_stake}\n"
)

With this library, it's a bit more readable as:

console.print(
    rs.str(
        "Subnet: ", rs.GENERAL.SUBHEADING(netuid_),
        " Stake:\n  ",
        rs.c.blue(current_stake),
        " :arrow_right: ", rs.STAKE.STAKE_AMOUNT(new_stake, "\n")
    )
)

The ideas are largely derived from Tailwind and other utility-style frameworks/libraries.

Current State

Currently this is just proof of concept. I will likely add to this over time.

Examples

See examples directory for examples of using this.

One of the fundamental aspects of this library is the ability to preload your own custom styles from a dictionary, like so:

from rich.console import Console
from rich_string_builder.string_builder import RichStyles

console = Console()

my_styles = {
    "alert": "blink yellow",
    "headers": {
        "main": "bold",
        "sub": "#4196D6"
    }
}
rs = RichStyles(my_styles)
console.print(rs.custom.alert("WARNING!"))  # '[blink yellow]WARNING![/blink yellow]'

Though you are able to combine these styles ad-hoc:

bright_blue = rs.Styles.bright + rs.Colors.blue
console.print(bright_blue("Coldkey"))  # '[bright blue]Coldkey[/bright blue]'

Say you have a complicated custom style, and you wish to only remove an element or two and reuse it:

my_styles = {
    "foo": "blink bright yellow"
}
rs = RichStyles(my_styles)
console.print((rs.custom.foo - "blink")("Hello!"))  # '[bright yellow]Hello![/bright yellow]'
# or define a new type:
bar = rs.custom.foo - "blink"
console.print(bar("Hello!"))  # '[bright yellow]Hello![/bright yellow]'

Who is this for?

I am personally colourblind. It's nice to have someone who is not colourblind define a colour palette, and I simply build from this. While colourblind people are not the only intended users, I think it will help them (us) substantially in terms of readability of the styles. It's also very helpful for keeping easier track of your styles and reusability.

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

rich_string_builder-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

rich_string_builder-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file rich_string_builder-0.0.1.tar.gz.

File metadata

  • Download URL: rich_string_builder-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rich_string_builder-0.0.1.tar.gz
Algorithm Hash digest
SHA256 861da0e09ee1ded315660aa132ae671505ae89db7dd9f9941c12f11edb9aafc5
MD5 f4ad247442f69baedb15f7245ff1a70e
BLAKE2b-256 3d2ed2b9b288e068c855c05bbe24085e841af750838d00e31cbadb8513d9a049

See more details on using hashes here.

Provenance

The following attestation bundles were made for rich_string_builder-0.0.1.tar.gz:

Publisher: python-publish.yml on thewhaleking/rich-string-builder

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

File details

Details for the file rich_string_builder-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for rich_string_builder-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5292157a32b80cb3d0c6c9cb0954cbcb2966f0a4bc834354329b8492ce30622
MD5 f412ce7629c15df4d215d1a20f5a6cc6
BLAKE2b-256 af7393e25124e33e2216585343d384f42d4afc4a7a78f04a3c89b0b4320dd1f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for rich_string_builder-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on thewhaleking/rich-string-builder

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