Skip to main content

Colorist for Python

Project description

Latest version Python >=3.9 MIT license

🌈 Colorist for Python 🌈

Lightweight Python package that makes it easy and fast to print terminal messages in colors.

Prerequisites

  • Python 3.9 or higher
  • macOS
    • It may work on Windows or Linux, but hasn't been tested

Installation

PyPI

pip3 install colorist

Homebrew

If you already have installed the Homebrew package manager for Mac and Linux, use this terminal command to tap Colorist for Python:

brew tap jakob-bagterp/colorist

And then install:

brew install colorist

Text Colors

Getting Started

Full Terminal Output

How to print a full line of colored text in the terminal:

from colorist import green, yellow, red

green("This is GREEN!")
yellow("This is YELLOW!")
red("This is RED!")

How it appears in the terminal:

Example of terminal message with green, yellow, red text color

Custom Terminal Output

How to customize terminal messages and change color inside a paragraph:

from colorist import Color

print(f"I want {Color.RED}red{Color.OFF} color inside this paragraph")

print(f"Both {Color.GREEN}green{Color.OFF} and {Color.YELLOW}yellow{Color.OFF} are nice colors")

How it appears in the terminal:

Example of terminal message with green, yellow, red text color

from colorist import BrightColor

print(f"I want {BrightColor.CYAN}cyan{BrightColor.OFF} color inside this paragraph")

How it appears in the terminal:

Example of terminal message with cyan text color

Remember to use Color.OFF or BrightColor.OFF every time you want to revert back to the default terminal text style. Otherwise the color may spill over and into other terminal messages.

Other String Formats

It's easier and more readable to use f-strings as in the examples above, but you can also use string format:

from colorist import Color

print("I want {0}red{1} color inside this paragraph".format(Color.RED, Color.OFF))

How it appears in the terminal:

Example of terminal message with red text color

Supported Text Colors

Color Full Text Function Custom Example
Green green("text") Color.GREEN Green text color in terminal
Yellow yellow("text") Color.YELLOW Yellow text color in terminal
Red red("text") Color.RED Red text color in terminal
Magenta magenta("text") Color.MAGENTA Magenta text color in terminal
Blue blue("text") Color.BLUE Blue text color in terminal
Cyan cyan("text") Color.CYAN Cyan text color in terminal
White white("text") Color.WHITE White text color in terminal
Black black("text") Color.BLACK Black text color in terminal
- - Color.OFF -
Bright green bright_green("text") BrightColor.GREEN Bright green text color in terminal
Bright yellow bright_yellow("text") BrightColor.YELLOW Bright yellow text color in terminal
Bright red bright_red("text") BrightColor.RED Bright red text color in terminal
Bright magenta bright_magenta("text") BrightColor.MAGENTA Bright magenta text color in terminal
Bright blue bright_blue("text") BrightColor.BLUE Bright blue text color in terminal
Bright cyan bright_cyan("text") BrightColor.CYAN Bright cyan text color in terminal
Bright white bright_white("text") BrightColor.WHITE Bright white text color in terminal
Bright black bright_black("text") BrightColor.BLACK Bright black text color in terminal
- - BrightColor.OFF -

Background Colors

Getting Started

Full Terminal Output

How to print a full line of text with colored background in the terminal:

from colorist import bg_green, bg_yellow, bg_red

bg_green("This is GREEN background!")
bg_yellow("This is YELLOW background!")
bg_red("This is RED background!")

How it appears in the terminal:

Example of terminal message with green, yellow, red background color

Custom Terminal Output

How to customize terminal messages and change background color inside a paragraph:

from colorist import BgColor

print(f"I want {BgColor.RED}red{BgColor.OFF} background color inside this paragraph")

print(f"Both {BgColor.GREEN}green{BgColor.OFF} and {BgColor.YELLOW}yellow{BgColor.OFF} are nice background colors")

How it appears in the terminal:

Example of terminal message with green, yellow, red background color

from colorist import BgBrightColor

print(f"I want {BgBrightColor.CYAN}cyan{BgBrightColor.OFF} background color inside this paragraph")

How it appears in the terminal:

Example of terminal message with cyan background color

As with text colors, remember to use BgColor.OFF or BgBrightColor.OFF every time you want to revert back to the default terminal text style. Otherwise the color may spill over and into other terminal messages.

Supported Background Colors

Color Full Text Function Custom Example
Green bg_green("text") BgColor.GREEN Green background color in terminal
Yellow bg_yellow("text") BgColor.YELLOW Yellow background color in terminal
Red bg_red("text") BgColor.RED Red background color in terminal
Magenta bg_magenta("text") BgColor.MAGENTA Magenta background color in terminal
Blue bg_blue("text") BgColor.BLUE Blue background color in terminal
Cyan bg_cyan("text") BgColor.CYAN Cyan background color in terminal
White bg_white("text") BgColor.WHITE White background color in terminal
Black bg_black("text") BgColor.BLACK Black background color in terminal
- - BgColor.OFF -
Bright green bg_bright_green("text") BgBrightColor.GREEN Bright green background color in terminal
Bright yellow bg_bright_yellow("text") BgBrightColor.YELLOW Bright yellow background color in terminal
Bright red bg_bright_red("text") BgBrightColor.RED Bright red background color in terminal
Bright magenta bg_bright_magenta("text") BgBrightColor.MAGENTA Bright magenta background color in terminal
Bright blue bg_bright_blue("text") BgBrightColor.BLUE Bright blue background color in terminal
Bright cyan bg_bright_cyan("text") BgBrightColor.CYAN Bright cyan background color in terminal
Bright white bg_bright_white("text") BgBrightColor.WHITE Bright white background color in terminal
Bright black bg_bright_black("text") BgBrightColor.BLACK Bright black background color in terminal
- - BgBrightColor.OFF -

Effects

Getting Started

In addition to colors, Colorist for Python can also add effects to text messages in the terminal.

Full Terminal Output

How to print a full line of text effect in the terminal:

from colorist import effect_blink

effect_blink("This is BLINKING!")

How it appears in the terminal:

Example of terminal message with blinking text

And this can also be combined with an optional color:

from colorist import Color, effect_blink

effect_blink("This is BLINKING!", Color.CYAN)

How it appears in the terminal:

Example of terminal message with blinking, cyan-colored text

Custom Terminal Output

How to customize terminal messages and change effect inside a paragraph:

from colorist import Effect

print(f"I want {Effect.UNDERLINE}underlined text{Effect.UNDERLINE_OFF} inside this paragraph")

print(f"I want {Effect.BOLD}emphasized text{Effect.BOLD_OFF} inside this paragraph")

How it appears in the terminal:

Example of terminal message with underline and bold text

Effects can also be mixed with colors:

from colorist import Color, Effect

print(f"I want both {Color.RED}colored and {Effect.BLINK}blinking{Effect.BLINK_OFF} text{Color.OFF} inside this paragraph")

How it appears in the terminal:

Example of terminal message with red and blinking text

Similar to Color.OFF, remember to turn off an effect with the relevant reset option (e.g Effect.BOLD_OFF, Effect.DIM_OFF, etc. or even just Effect.OFF) every time you want to revert back to the default terminal text style. Otherwise the effect may spill over and into other terminal messages.

Supported Effects

Effect Full Text Function Custom Reset Example
Bold effect_bold("text") Effect.BOLD Effect.BOLD_OFF Example of terminal message with bold text
Dim effect_dim("text") Effect.DIM Effect.DIM_OFF Example of terminal message with dimmed text
Underline effect_underline("text") Effect.UNDERLINE Effect.UNDERLINE_OFF Example of terminal message with underlined text
Blink effect_blink("text") Effect.BLINK Effect.BLINK_OFF Example of terminal message with blinking text
Reverse effect_reverse("text") Effect.REVERSE Effect.REVERSE_OFF Example of terminal message with reversed text color and background
Hide effect_hide("text") Effect.HIDE Effect.HIDE_OFF Example of terminal message with hidden text
- - - Effect.OFF -

Donate

This module is free to use. And if you like it, feel free to buy me a coffee.

Contribute

If you have suggestions or changes to the module, feel free to add to the code and create a pull request.

Report Bugs

Report bugs and issues here.

MIT License

Copyright (c) 2022 – present Jakob Bagterp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

colorist-1.1.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

colorist-1.1.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file colorist-1.1.2.tar.gz.

File metadata

  • Download URL: colorist-1.1.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for colorist-1.1.2.tar.gz
Algorithm Hash digest
SHA256 e9ffa6d8ee5f2e90fa0761dc440296b206563760272aa736331ab4309dcd3334
MD5 ad85bfea618df3bbee5392c14530f385
BLAKE2b-256 73cb404e9f06b7190529061f75af604e9600f1f79147468ba91229c0f5e167e4

See more details on using hashes here.

File details

Details for the file colorist-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: colorist-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for colorist-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2ba238e01fd92a803e2a6712379a9fcab0daa33b8f4e247e4fe74b04b3bca93c
MD5 a9e3d3d9fc33ab7900f894afbfc60de6
BLAKE2b-256 17e2ed514d2e5a4ee706d13d73c9d1d71a46f7bf7103223b6b844855cbd09b0b

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