A Python library to add style to your console.
Project description
constyle
A Python library to add style to your console.
The name of the library comes from merging the words CONSoLE and STYLE.
Installation
You can install this package with pip or conda.
$ pip install constyle
$ conda install -c abrahammurciano constyle
Links
The full documentation is available here.
The source code is available here.
Usage
There are a couple of ways to use this library.
The style
function
The simplest way is with the style
function.
from constyle import style, Attributes
print(style('Hello World', Attributes.GREEN, Attributes.BOLD, Attributes.ON_BLUE))
Attribute
objects
Attribute
objects are all callable, and calling them will apply their style to the given input string.
from constyle import Attributes
underline = Attributes.UNDERLINE
print(underline("You wanna experience true level? Do you?"))
Style
objects
You can also use Style
objects to create a reusable style with several attributes. Style
objects are callable and take a string as input and return a styled string.
Adding together Attribute
objects will also create Style
objects, as will adding Attribute
s to existing Style
objects.
from constyle import Style, Attributes
warning = Style(Attributes.YELLOW, Attributes.BOLD)
whisper = Attributes.GREY + Attributes.DIM + Attributes.SUPERSCRIPT
print(warning('You shall not pass!'))
print(whisper('Fly you fools'))
Attributes
The Attributes
enum contains all the available ANSI attributes. You can read more about them here.
You'll find there is limited support for all the ANSI attributes among consoles.
If you need more attributes than the ones provided in this enum, you can create your own by using the Attribute
class.
Nesting
Nesting strings is not supported. The inner string will cause the rest of the outer string to lose its formatting.
NOTE: I would like to implement a fix for this in future, but I am uncertain if it is even possible, let alone feasible. If you have any suggestions, feel free to open an issue.
from constyle import Attributes
bold = Attributes.BOLD
yellow = Attributes.YELLOW
green = Attributes.GREEN
print(yellow(bold('This is bold and yellow')))
print(green(f"This is green. {yellow('This is yellow.')} This is no longer green"))
RGB and 8-bit colours
You can create an attribute for whichever colour you want with the classes ForegroundRGB
, BackgroundRGB
and Foreground8Bit
and Background8Bit
. For example:
from constyle import ForegroundRGB, style
print(style("This is a pink string", ForegroundRGB(255, 128, 255)))
The command line interface
This package also provides a very basic command line interface to print styled strings.
Use constyle --help
to see how to use it.
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
File details
Details for the file constyle-0.2.0.tar.gz
.
File metadata
- Download URL: constyle-0.2.0.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1031-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceb31c979b2f0ca279aa8b915085405926cf4f0325def271a0e0eb7a2c581096 |
|
MD5 | c6e9a3d2d35e4e57108a3bbcb5274bb7 |
|
BLAKE2b-256 | f52b4e725c63b982b8186410646ac5cdda71de0ffc2a62b6b9c808a7694d3212 |
File details
Details for the file constyle-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: constyle-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1031-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d452288069b991efc1ffbacb5083d59d46f53fc7721fc65a52047ad66badb4b6 |
|
MD5 | 7d1f774a486f7524c2f922ac0e3a050c |
|
BLAKE2b-256 | 504ea61d2f20757f153f771072ba4239a853405e5d94c1f9c81bda8a04501bf3 |