Skip to main content

Kids python ANSI string library

Project description

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.

Project details


Download files

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

Source Distribution

kids.ansi-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file kids.ansi-0.0.1.tar.gz.

File metadata

  • Download URL: kids.ansi-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for kids.ansi-0.0.1.tar.gz
Algorithm Hash digest
SHA256 47386c534acc25f236c379e04379931884ff4f1b589dbc1175b7ca89ee3e50f4
MD5 0a472c4658349a4472aa7a0aa37d1293
BLAKE2b-256 8623b58b70987bcdad412f46df7118176ac133400482e95841c7b424c4e7cac5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page