Skip to main content
https://pypip.in/v/kids.ansi/badge.png https://secure.travis-ci.org/0k/kids.ansi.png?branch=master

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.

Maturity

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.

Features

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.

Usage

aformat

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('\x1b[31mYou\x1b[39m')

>>> 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
'\x1b[37m'
>>> BG.red
'\x1b[41m'
>>> BG.default
'\x1b[49m'
>>> ATTR.bold
'\x1b[1m'
>>> ATTR.unbold
'\x1b[21m'
>>> CTL.reset
'\x1b[0m'

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 " + FG.red + "Hello" + FG.default + " you."
'Hmm \x1b[31mHello\x1b[39m you.'

And print it.

Metadata

Release files for kids.ansi 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kids.ansi 0.0.1
File Size Uploaded
kids.ansi-0.0.1.tar.gz 6.2 kB Details

Release files / kids.ansi-0.0.1.tar.gz

Download URL kids.ansi-0.0.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
47386c534acc25f236c379e04379931884ff4f1b589dbc1175b7ca89ee3e50f4
BLAKE2b-256 checksum
How to use checksums
8623b58b70987bcdad412f46df7118176ac133400482e95841c7b424c4e7cac5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page