Package: tcolors
tcolorsSimple and easily-disableable library to make your life easier when working with ANSI colors in the terminal.Without dozens of options, methods and combinations. Coloring the terminal should be a simple task.Supports basic colors, bright colors, 256 ANSI colors, true colors and most of the ANSI text modifiers.InstallationInstalltcolorsby using pip:$ pip install tcolorsMotivationThere are dozens, if not hundreds, of libraries that color the terminal with ANSI escape sequences, why develop another one?Mainly because I always miss a feature in almost all existing libraries: the option to enable or disable the colored automacally.This is especially useful when developing command line applications whose output may bepipedinto another. In such cases it is quite practical to have a flag to globally disable coloring (--no-colorsfor example) and make it easier for downstream applications that don't have to deal with data polluted by escape sequences.UsageColorstcolorscan color text with different color palettes. Take into account that not all terminal emulators support all of them.Basic 3-bit colorsColorForegroundBackgroundBlackColor.BLACKBgColor.BLACKRedColor.REDBgColor.REDGreenColor.GREENBgColor.GREENYellowColor.YELLOWBgColor.YELLOWBlueColor.BLUEBgColor.BLUEMagentaColor.MAGENTABgColor.MAGENTACyanColor.CYANBgColor.CYANWhiteColor.WHITEBgColor.WHITEBright colorsColorForegroundBackgroundBlackColor.BRIGHT_BLACKBgColor.BRIGHT_BLACKRedColor.BRIGHT_REDBgColor.BRIGHT_REDGreenColor.BRIGHT_GREENBgColor.BRIGHT_GREENYellowColor.BRIGHT_YELLOWBgColor.BRIGHT_YELLOWBlueColor.BRIGHT_BLUEBgColor.BRIGHT_BLUEMagentaColor.BRIGHT_MAGENTABgColor.BRIGHT_MAGENTACyanColor.BRIGHT_CYANBgColor.BRIGHT_CYANWhiteColor.BRIGHT_WHITEBgColor.BRIGHT_WHITEModifiersModifierDescriptionResetReset all attributesMod.RESETBoldBoldMod.BOLDDimmedFaint, decreased intensity, or dimMod.DIMItalicItalicMod.ITALICUnderlineUnderlineMod.UNDERLINESlow blinkBlink less than 150 times per minuteMod.SLOW_BLINKRapid blinkBlink more than 150 times per minute. Not widely supportedMod.RAPID_BLINKInvertedSwap foreground and background colorsMod.INVERTConcealConceal or hide. Not widely supportedMod.CONCEALStrikethroughStrikethroughMod.STRIKE256 colorsColorForegroundBackgroundNColor256.C_{N}BgColor256.C_{N}Where{N}can be any number from 0 to 255.True colorsThere are no built-in colors for the more than 16M of colors of the true color palette, but you can create your own colors just by creating a new instance ofTrueColororBgTrueColorfromtcolorsimportTrueColor,BgTrueColor# Define colors either as hex strings (normal or short) or as rgb valuesBLUE=TrueColor('#1F2041')PURPLE=TrueColor('#437')GREEN=TrueColor(65,123,90)BG_COLOR=BgTrueColor(r=63,g=123,b=90)Colorize textYou can add color in two ways:colorize: given a string it return a new string with the color escape sequences.cprint: print applying colors to the printed text.cprinthas the same signature as the built-in print function with an extra keyword argumentstyleto define the style to apply.fromtcolorsimportColor,Mod,colorize,cprintcolorize('Text with colors',Color.RED)cprint('Print with color',style=Mod.BOLD)Both methods adds the reset modifier at the end automatically.In fact you can concatenate styles (all colors and modifiers are instances of the Style class) to any string directly. Just remember that in these cases it is necessary to put the reset modifier manually.print(Color.RED+'Colored text'+Mod.RESET)print(Color.RED,'Colored text',Mod.RESET,sep='')str1='This is a{}blue{}text'.format(Color.BLUE,Mod.RESET)str2='This is a%syellow%stext'%(Color.YELLOW+Mod.BOLD,Mod.RESET)str3=f'{Color.RED}{Mod.BOLD}This is a text in bold red{Mod.RESET}'Mix styles togetherIt is possible to combine styles using the addition operator.cprint('This is a warning',style=Color.RED+BgColor.YELLOW+Mod.BOLD+Mod.Italic)If you plan to use the same combination frequently you can assign it to a variable:title_style=Color.BLUE+Mod.UNDERLINEcprint('Title 1',style=title_style)colorize('Another title',style=title_style)ConfigurationYou can configuretcolorsto enable or disable the colored globally.If it is disabled all calls tocolorizeorcprintwon't color the text.fromtcolorsimportconfigure_colorsconfigure_colors(enable_colors=False)You can also define a default style that will be added automatically in all calls tocolorizeorcprintfromtcolorsimportconfigure_colors,Mod# Put everything in boldconfigure_colors(default_style,Mod.BOLD)cprint('Just an example...',style=Color.GREEN)# It will be printed in green and boldcprint('Only bold')Colorizer classIf you need to colorize several things in a different way use custom instances of theColorizerclass.fromtcolorsimportColorizer,Modcolorizer1=Colorizer(default_style=Mod.BOLD)colorizer2=Colorizer(default_style=Mod.ITALIC)colorizer1.colorize('The colorizer1 instance always prints text in bold',style=Color.BLUE)colorizer2.colorize('While colorizer2 always prints in italic',style=Color.YELLOW)As you can see the behaviour is the same as in thecolorizeandcprintfunctions. That is becausetcolorsuses an instance of theColorizerclass internally and exports those methods. Combine custom instances with the internal instance as you will.If you want to use some default style only in a certain piece of code you can useColorizeras a context manager:fromtcolorsimportColorizerwithColorizer(default_style=Color.GREEN)asc:c.cprint('Inside the context manager')ExamplesYou can find more practical examples in theexamplesdirectoty.