Skip to main content

Colorist for Python

Project description

Latest version Python >=3.10 MIT license Test

🌈 Colorist for Python 🌈

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

Prerequisites

  • Python 3.10 or higher

Installation

PyPI

Assuming that Python is installed already, execute this command in the terminal:

pip3 install colorist

If you already have installed Colorist for Python, use this command to upgrade to latest version:

pip3 install --upgrade colorist

Homebrew

If you already have installed the Homebrew package manager for Mac and Linux, execute 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.2.3.tar.gz (11.5 kB view hashes)

Uploaded Source

Built Distribution

colorist-1.2.3-py3-none-any.whl (10.3 kB view hashes)

Uploaded Python 3

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