Skip to main content

Taking care of the dirty parts of terminal colors.

Project description

Stain

Stain helps you color your terminal output without having to worry about formatting classes, replacement print functions, or using anything more than familiar string manipulations.

Getting Started

Using the basics of stain is fast and easy.

Stain provides a context manager that handles pre and post-printing of formatting characters.

Ex:

from stain import Stain

stain = Stain()

with stain.red(): # the context manager __enter__ prints \033[31m
    print("This line is red.")
    print("So is this one.")
# the context manager __exit__ prints \033[0m

print("But this one is not.")

For more complex coloring, stain also provides formatting constants.

from stain import Stain

stain = Stain()

print("Hello " + stain.BLACK + " darkness " + stain.RESET + "my old friend.")

Both forms of stain can take stacked attributes in any order:

stain.BLACK_ON_RED

or

with stain.bold_green():

or even

stain.BOLD_RED_ON_BLACK_UNDERLINE

Terminal Detection

Stain is TTY-aware, meaning it will not print coloring characters when your output has been redirected to files, pagers, etc.

Supported Formatting

Stain supports 16-Color ANSI foreground and background colors, and the common formatters.

Whether or not a given terminal supports them is another matter.

COLORS:

  • Black
  • Red
  • Green
  • Yellow
  • Blue
  • Magenta
  • Cyan
  • Light gray
  • Dark gray
  • Light red
  • Light green
  • Light yellow
  • Light magenta
  • Light cyan
  • White

FORMATS:

  • Bold
  • Reset Bold
  • Dim
  • Reset Dim
  • Underline
  • Reset Underline
  • Blink
  • Reset Blink
  • Reverse
  • Reset Reverse
  • Hidden
  • Reset Hidden
  • Reset All

Caveat Regarding Background Coloring

By default, background coloring is disabled in the context manager form of stain. This is due to how background color determination works when scrolling a terminal.

When printing a newline at the bottom of the screen such that everything needs to shift upward, the background color will bleed onto the next line. The context manager cannot print the reset before print statements in the block print their newlines, so ugly background color bleeding will occur.

This is also true of the Reverse formatter.

To enable background colors with the context manager, do the following:

from stain import Stain

stain = Stain(unsafe=True)

with stain.black_on_light_gray():
    print("This will have a background color" + stain.RESET)

You will need to hand-reset at the end of each line to prevent scrolling background bleed.

Prerequisites

None

Installing

pip install stain

Running the tests

make tests

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

stain-0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

stain-0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file stain-0.1.tar.gz.

File metadata

  • Download URL: stain-0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for stain-0.1.tar.gz
Algorithm Hash digest
SHA256 1e4b293d8051580d85a7e5ffb1978e3f9eb40e7a170657faa6804a04877f10f1
MD5 95f8d679c95be2a5654315649d362ea6
BLAKE2b-256 ccfa5d7e143a6d2948cb01a806c27c32cdf56c952cccb418eeaefba8579bc815

See more details on using hashes here.

File details

Details for the file stain-0.1-py3-none-any.whl.

File metadata

  • Download URL: stain-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for stain-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5ae59b3d74a45558ed5450f58978bd20ae87920856471cf60a84b0c86e5053b
MD5 2cd65fb99578fe29b00b34ce473817f5
BLAKE2b-256 e02a547962859426ad4e615eb9ab9f07cb077a6d593e877c22a12f640bcfda31

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