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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
861da0e09ee1ded315660aa132ae671505ae89db7dd9f9941c12f11edb9aafc5
|
|
| MD5 |
f4ad247442f69baedb15f7245ff1a70e
|
|
| BLAKE2b-256 |
3d2ed2b9b288e068c855c05bbe24085e841af750838d00e31cbadb8513d9a049
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rich_string_builder-0.0.1.tar.gz -
Subject digest:
861da0e09ee1ded315660aa132ae671505ae89db7dd9f9941c12f11edb9aafc5 - Sigstore transparency entry: 182839424
- Sigstore integration time:
-
Permalink:
thewhaleking/rich-string-builder@07349a8029d4bc8e2af3d32c5317d900273a4dc0 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/thewhaleking
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@07349a8029d4bc8e2af3d32c5317d900273a4dc0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file rich_string_builder-0.0.1-py3-none-any.whl.
File metadata
- Download URL: rich_string_builder-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5292157a32b80cb3d0c6c9cb0954cbcb2966f0a4bc834354329b8492ce30622
|
|
| MD5 |
f412ce7629c15df4d215d1a20f5a6cc6
|
|
| BLAKE2b-256 |
af7393e25124e33e2216585343d384f42d4afc4a7a78f04a3c89b0b4320dd1f3
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rich_string_builder-0.0.1-py3-none-any.whl -
Subject digest:
d5292157a32b80cb3d0c6c9cb0954cbcb2966f0a4bc834354329b8492ce30622 - Sigstore transparency entry: 182839428
- Sigstore integration time:
-
Permalink:
thewhaleking/rich-string-builder@07349a8029d4bc8e2af3d32c5317d900273a4dc0 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/thewhaleking
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@07349a8029d4bc8e2af3d32c5317d900273a4dc0 -
Trigger Event:
release
-
Statement type: