Skip to main content

Kids python ANSI string library

Project description

kids.ansi is a Python library providing helpers when writing command line utilities in python. It’s part of ‘Kids’ (for Keep It Dead Simple) library, but can be used with no extra dependencies.


This code is in alpha stage. Some part of it are ongoing reflexions. What is documented here shouldn’t change much, and is actually working.


using kids.ansi:

  • Access and insert ANSI escape sequences manualy.
  • Or use the aformat wrapper for full abstraction.

Note that it’s very close to what termcolor does.



kids.ansi features a aformat function to return a string ready for print with the ansi character inside:

>>> from kids.ansi import aformat

>>> aformat("You", fg="red")

>>> aformat("Hello You, how are you?", fg="black", bg="blue", attrs=["bold", ])
aformat('\x1b[30m\x1b[44mHello You, how are you?\x1b[39m\x1b[49m')

Notice that aformat is somewhat clever

>>> you = aformat("You", fg="red")
>>> aformat("Hello, Are " + you + " Well", fg="blue")
aformat('\x1b[34mHello, Are \x1b[31mYou\x1b[34m Well\x1b[39m')

Notice how the ending ansi sequence of the you sets back the blue color and not the default one. So the word “Well” still appears in blue.

aformat is clever, but still with some limitation, hitting mainly string interpolation:

>>> aformat("Hello, Are %s Well" % you, fg="blue")
aformat('\x1b[34mHello, Are \x1b[31mYou\x1b[39m Well\x1b[39m')

The word “Well” is NOT in blue. This is an issue.

Access to ansi sequences

Access to raw ANSI color sequence can be done via FG, BG, ATTR, and CTL attritbutes dicts:

>>> from kids.ansi import FG, BG, ATTR, CTL

>>> FG.white
>>> BG.default
>>> ATTR.bold
>>> ATTR.unbold
>>> CTL.reset

As these are dicts, and you don’t remember all the accessible keys, you can introspect them easily:

>>> sorted(ATTR)  ## doctest: +NORMALIZE_WHITESPACE
['blink', 'bold', 'conceal', 'faint', 'italic', 'reverse', 'strike',
 'unblink', 'unbold', 'unconceal', 'underline', 'unfaint', 'unitalic',
 'unreverse', 'unstrike', 'ununderline']

You could then:

>>> "Hmm " + + "Hello" + FG.default + " you."
'Hmm \x1b[31mHello\x1b[39m you.'

And print it.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for kids.ansi, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size kids.ansi-0.0.1.tar.gz (6.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page