Package: tinta
TintaTinta is a magical console output tool with support for printing in beautiful colors and with rich formatting, like bold and underline, using static, chain-able methods. It's so pretty, it's almost like a unicorn!Features and Tinta BasicsTinta takes a statically typed approach to handling rich-color console output.In the past you might have fiddled with ANSI colors codes, or passed strings to a generic class, only to discover you typo'd one of them! (Yes, we've all been there).But with Tinta, you can create your owncolors.inifile, which dynamically generates builder pattern methods forTinta. If you add a color forwineto your colors file, you can then use:fromtintaimportTintaTinta.load_colors('colors.ini')Tinta().wine('sip')You can make a really simple drop-inprint()replacement:Tinta('Our neural pathways have become accustomed '\'to your sensory input patterns.').print()Or you can create a variable to make things easier to read (or use control flows):fromtintaimportTintachildren=Truet=Tinta().mint('Fate.')t.dark_gray('It protects')t.underline().red('fools')ifchildren:t.normal().pink('little children')else:t.normal().yellow('spotted cats')t.dark_gray(', and ships named')t.mint("Enterprise.").print()OK, neat, so how is that like unicorns?Glad you asked! Here are some pretty pictures:Installation and Getting StartedInstall Tinta:pipinstalltinta(Or visithttps://pypi.org/project/tinta/)Add Tinta to your project, and optionally configure a path to yourcolors.inifile. This path can be relative, or absolute; the best way to make a path is usingpathlib.Path().fromtintaimportTinta# or to specify your custom colors,# relative to your project's cwd:frompathlibimportPathfromtintaimportTintaTinta.load_colors(Path().cwd()/'config/colors.ini')You can useTinta.discover()to discover what colors are available on your console.Tinta.discover()# orTinta.discover(background=True)An examplecolors.inifile might look like:# A list of ansi colors for your console.green:35red:1blue:32yellow:214(Or modify the Tinta colors.ini fromhere)API ReferenceTinta (+ color methods)Dynamic methodsThese methods are loaded dynamically from yourcolors.inifile:Tinta().green()Tinta().red()Tinta().blue()Tinta().wine()Tinta().my_color()# etc.ArgsEach color method (andTinta) supports the following args. A copy if itself is then returned for method chaining:*s (str)– A sequence of one or more text strings, to be joined together.sep (str)– Used to join segment strings. Defaults to' '.For example:Tinta('A set','of strings','joined','with','semicolons',sep=';').print()~»Aset;ofstrings;joined;with;semicolonsAttributesAllTintaand dynamic color methods will make available the following attributes:color (str)— A color string or ansi color code (int), e.g. 'white' or 42.style (str)— A style string, e.g. 'bold', 'dim', 'underline'. Multiple styles are joined with a +.parts (list)— A list of richly styled text segments.parts_plaintext (list)— A list of unstyled text segments.Built-in Methodsprint()– Prints to the console. See below for supported args.text(sep=' ') -> str– Returns a compiled rich text stringplaintext(sep=' ') -> str– Returns a compiled plaintext stringadd() -> self– Adds segments using any previously defined styles.line() -> self– Appends the contents preceded by a newline char (\n).bold() -> self– Sets segments to bold.underline() -> self– Sets segments to underline.dim() -> self– Sets segments to a darker, dimmed color.code() -> self– Adds segments using the specified ansi code.normal() -> self– Resets the style to default.reset() -> self– Resets both style and color to default.discover()– Prints a list of available colors to the console.All style methods support the same arguments asTintaand dynamic color methods:Tinta().underline('Underlined','text').print()Tinta().bold('Bold','text',sep='+').print()Tinta().dim('Dimmed','text',sep='_').print()print()Prints to the console. Probably the most important method, because if you don't print, you don't see anything at all!
A good first step in troubleshooting is checking that you remembered toprint()(ask me how I know...)This supports all the built-in Python 3print()methods, too (sep,end,file,flush), as well as:plaintext (bool)– Prints in plaintext if set to Trueforce (bool)– Forcibily prints to the console, even if'env::TINTA_STEALTH'is set.# Prints in plaintextTinta().purple('A bird').print(plaintext=True)# Always prints, even if 'env::TINTA_STEALTH' is set)Tinta().green('A plane').print(force=True)It's also important to note thatprint()doesn't make a variable unusable, it just resets and clears itself when called. This means you can do:tint=Tinta()tint.blue('A cloud').print()tint.green('A tree').print()add()Sometimes you want the convenience of readability without changing styles, or you might want to use control flow to set a variable. For these, you can useadd():tint=Tinta().gray('I am a bear')ifyou_love_bears:tint.pink('and I love bears!')else:tint.add('but I am sad.')tint.print()You can also usesepto control how multiple segments are joined together:Tinta().brown('I am a brown bear',sep='').black(', and I am a black bear.').print()line()Adds your same text, but preceded by a newline.Tinta('A cat').line('scratches').print()# A cat# scratchescode()Sometimes you might want to use a color that wasn't defined in yourcolors.ini. For that, you can use.code().
Just set thecodearg to specify an ANSI color code:Tinta().code('A bear',code=42).print()This is useful for adding colors on the fly if they aren't defined incolors.ini.Environment VariablesSometimes it's useful to globally configureTintaon a system where you might want it to behave differently, without changing your source code. If these Environment variables are present on the system, they will be considered True.TINTA_STEALTH– Disables console output globallyTINTA_PLAINTEXT– Disables rich console output, only printing plain text.TINTA_SEPARATOR– Changes the default separator (' ') to this value.Running TestsTo run tests, run the following command:pipinstallpytest
python-mpytest-xvContributingContributions are welcome! Please send in a PR with a clear explanation of what you're adding and why, and where applicable, add tests to validate. Please read ourcode of conductbefore contributing.AcknowledgementsSpecial thanks to@katherinecodesforreadme.so,@jessicaspacekatforrikeripsum.com, andansicolors.LicenseTinta is licensed under both theMIT Licenseand theHippocratic License. Were a conflict or dispute to arise between these two licenses, theHippocratic Licenselicense shall take precedence. Under its principles of Do No Harm, no portion of this software may be used to (or be a part of software that can be used to) cause, infer, encourage, incite, or otherwise lead to physical or verbal harm for any person or people,especiallymarginalized and underrepresented people.