Package: constyle
constyleA Python library to add style to your console.The name of the library comes from merging the wordsCONSoLEandSTYLE. Also "con" means "with" in Spanish.InstallationYou can install this package with pip or conda.$pipinstallconstyle$condainstall-cconda-forgeconstyle$condainstall-cabrahammurcianoconstyleLinksUsageThere are a couple of ways to use this library.ThestylefunctionThe simplest way is with thestylefunction.fromconstyleimportstyle,Attributesprint(style('Hello World',Attributes.GREEN,Attributes.BOLD,Attributes.ON_BLUE))StyleobjectsYou can also useStyleobjects to create a reusable style with any number of attributes.Calling aStyleobjectStyleobjects are callable and take a string as input and return a styled string.warning=Style(Attributes.YELLOW,Attributes.BOLD)print(warning('You shall not pass!'))AddingStyleobjectsAdding togetherStyleobjects will also createStyleobjects.whisper=Attributes.GREY+Attributes.DIM+Attributes.SUPERSCRIPTprint(whisper('Fly you fools'))ConvertingStyleobjects to stringsStyleobjects can be converted to strings to obtain the ANSI escape sequence for that style.warning=Style(Attributes.YELLOW,Attributes.BOLD)print(f"{warning}You shall not pass!{Attributes.RESET}")AttributesTheAttributesenum contains all the available ANSI attributes. You can read more about themhere.Attributesare alsoStyleobjects, and as such, as demonstrated above, they too can be called to style a string, added together and to otherStyleobjects, and converted to strings to obtain their ANSI sequence.You'll find there is limited support for all the ANSI attributes among some consoles.If you find more attributes that aren't provided in this enum, you can create your own by constructing aStylewith an integer.NestingIn order to nest styles, you can use theend=keyword argument of thestylefunction or theStyleclass. Usually when applying a style, theRESETattribute is appended to the end. This can be undesirable when nesting (see the example below).bold=Attributes.BOLDyellow=Attributes.YELLOWgreen=Attributes.GREENprint(yellow(bold('This is bold and yellow')))print(green(f"This is green.{yellow('This is yellow.')}This is no longer green"))In order to achieve the desired result in the above example, you would have to use theend=keyword argument of thestylefunction. You can pass anyStyletoend.print(green(f"This is green.{bold('This is green and bold.',end=Attributes.NO_BOLD)}This is still green but not bold anymore"))print(green(f"This is green.{yellow('This is yellow.',end=green)}This is now green again"))Custom coloursTheconstyle.custom_coloursmodule contains a few classes that can be used to create custom colours.RGB coloursYou can create aStylefor a custom RGB colour by using theRGBclass. This is not well supported by all consoles.fromconstyle.custom_coloursimportRGBprint(style('This is pink',RGB(255,192,203)))8-bit coloursSome consoles support 8-bit colours. You can create aStylefor an 8-bit colour by using theEightBitclass, passing a single integer to it, or you can use theEightBitRGBclass to create an 8-bit colour style as close to the RGB values as possible.The command line interfaceThis package also provides a very basic command line interface to print styled strings.You can pass it any number of strings and it will print them all together (likeecho). You can pass--attribute(or-a) with the name of an attribute to apply to the other strings being printed. You can pass--attributeas many times as you like.You can useconstyle --helpto see more specific details, as well as all available attributes.For example you can useconstylefrom your shell to print some styled text.$constyleHelloWorld!-agreen-abold-aon_whiteOr if you're writing a shell script you can make an alias or a function to reuse a certain style.#!/bin/bashaliaserror="constyle --attribute bold --attribute red"# With an aliaswarn(){constyle$@-abold-ayellow}# With a functionerrorYoushallnotpass!
warnFlyyoufools!