Skip to content

Moosi312/color_console_py

Repository files navigation

color_console_py

Small tool that enables you to write to the console in color using context mangers or f-strings.

It uses ANSI codes to color the console.

How to use

Getting a color

Creating a color requires style, color and background, all of which can be called as positional or keyword arguments. If not specified they will default to the console's default.

Colors can also be called as attributes of Color or strings:

  • Color.black = 0
  • Color.red = 1
  • Color.green = 2
  • Color.yellow = 3
  • Color.blue = 4
  • Color.pink = 5
  • Color.brown = 6
  • Color.white = 7

Possible ways to call:

from color_console import Color

Color(1, 2, 3) # resulting color will be in style 1, color green, background yellow
Color() # resulting color will in default style, default color, default background
Color(1, "black", "white") #resulting color will be in style 1, color black, background white
Color(color=Color.red) # resulting color will be in default style, color red, default background

There are also some color presets like error/err, success/succ, warning/warn, info, that can be called with the positional keyword name or as an attribute of Color.

In this you can also define your own colors:

from color_console import Color

Color(name="error") # resulting color will be style 1, color red, background black
Color.info # resulting color will be style 1, color blue, default background

Color.test = Color(1, Color.pink, Color.brown) # color saved
Color.test # resulting color will be style 1, color pink, background brown

Using a color

Using a color can be done by printing it directly or using context managers:

Simple example:

from color_console import Color

col1 = Color(1, 2, 3) # get your color
print(col1.start()) # changes your console to the desired color/style
print("This is in color/style specified by col1")

col2 = Color(4, 5, 6) # get your color, no need to reset before
print(col2.start())
print("This is in color/style specified by col2")

print(col2.stop()) # resets the color

Better example using implicit conversion to str in f-strings:

from color_console import Color

print(f"{Color(1, 2, 3)}This is in the first color, {Color(4, 5, 6)} This is in the second color, {Color.end} This is in the default color")

Using context managers:

from color_console import Color

with Color.info:
    print("This will be in the specified color")
    print("You can print as much as you want in the with-statement")
print("This will be in the default color again")

Color combinations

Here it shows the used ANSI codes in its respective style and color. The following are the numbers that are used under the hood (the Color class uses the numbers 0-8 for everything)

First number (0-7): Style

Second number(30-37): Text color

Third number(40-47): Background color

Style 0

style 0

Style 1

style 1

Style 2

style 2

Style 3

style 3

Style 4

style 4

Style 5

style 5

Style 6

style 6

Style 7

style 7

Misc

misc

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages