Skip to main content

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. Also "con" means "with" in Spanish.

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.

Join the support Discord server 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))

Style objects

You can also use Style objects to create a reusable style with any number of attributes.

Calling a Style object

Style objects are callable and take a string as input and return a styled string.

warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(warning('You shall not pass!'))

Adding Style objects

Adding together Style objects will also create Style objects.

whisper = Attributes.GREY + Attributes.DIM + Attributes.SUPERSCRIPT
print(whisper('Fly you fools'))

Converting Style objects to strings

Style objects can be converted to strings to obtain the ANSI escape sequence for that style.

warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(f"{warning}You shall not pass!{Attributes.RESET}")

Attributes

The Attributes enum contains all the available ANSI attributes. You can read more about them here.

Attributes are also Style objects, and as such, as demonstrated above, they too can be called to style a string, added together and to other Style objects, and converted to strings to obtain their ANSI sequence.

You'll find there is limited support for all the ANSI attributes among some consoles.

If you find more attributes that aren't provided in this enum, you can create your own by constructing a Style with an integer.

Nesting

In order to nest styles, you can use the end= keyword argument of the style function or the Style class. Usually when applying a style, the RESET attribute is appended to the end. This can be undesirable when nesting (see the example below).

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"))

In order to achieve the desired result in the above example, you would have to use the end= keyword argument of the style function. You can pass any Style to end.

print(green(f"This is green. {bold('This is green and bold.', end=Attributes.NO_BOLD)} This is still green but not bold anymore"))
print(green(f"This is green. {yellow('This is yellow.', end=green)} This is now green again"))

Custom colours

The constyle.custom_colours module contains a few classes that can be used to create custom colours.

RGB colours

You can create a Style for a custom RGB colour by using the RGB class. This is not well supported by all consoles.

from constyle.custom_colours import RGB

print(style('This is pink', RGB(255, 192, 203)))

8-bit colours

Some consoles support 8-bit colours. You can create a Style for an 8-bit colour by using the EightBit class, passing a single integer to it, or you can use the EightBitRGB class to create an 8-bit colour style as close to the RGB values as possible.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

constyle-1.1.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

constyle-1.1.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file constyle-1.1.0.tar.gz.

File metadata

  • Download URL: constyle-1.1.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.15.0-1014-azure

File hashes

Hashes for constyle-1.1.0.tar.gz
Algorithm Hash digest
SHA256 75f9dc798aca932fa0aa4f7721beafa55df25b47c0be051e99ad9b1dc21d4163
MD5 ba4fda8b4e9f41581d6c5b4a60f4c083
BLAKE2b-256 d4eeb5e29fd62af08b1ae5406220b7d64129d1a02b4214a42e224214ee2f9bfe

See more details on using hashes here.

File details

Details for the file constyle-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: constyle-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.15.0-1014-azure

File hashes

Hashes for constyle-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79a5877d7ed1b5a96da2b90e0fc68f2b76cbdbddc1b71964935b2c2d0543ee66
MD5 dc9a88d7307849bfade3e15f23111ff0
BLAKE2b-256 b6e7becbb949341a50192839cbb0d7fe98b50992dd9e92af71290a565f695bae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page