Skip to main content

Produce colored terminal text with an xml-like markup

Project description

Latest version released on PyPi Build status

Ansimarkup is an XML-like markup for producing colored terminal text.

from ansimarkup import ansiprint as print

print('<b>bold text</b>'))
print('<red>red text</red>', '<red,green>red text on a green background</red,green>')

Installation

The latest stable version of ansimarkup can be installed from pypi:

$ pip install ansimarkup

Usage

An annotated usage example:

from ansimarkup import parse, ansiprint

# parse() converts the tags to the corresponding ansi escape codes.
parse("<b>bold</b> <d>dim</d>")

# ansiprint() works exactly like print(), but first runs parse() on all arguments.
ansiprint("<b>bold</b>", "<d>dim</d>")
ansiprint("<b>bold</b>", "<d>dim</d>", sep=':', file=sys.stderr)

# Colors may be specified in one of several ways.
parse("<red>red foreground</red>")
parse("<RED>red background</RED>")
parse("<fg red>red foreground</fg red>")
parse("<bg red>red background</bg red>")

# Tags may be nested.
parse("<r><Y>red text on a yellow foreground</Y></r>")

# The above may be more concisely written as:
parse("<r,y>red text on a yellow background</r,y>")

# This shorthand also supports style tags.
parse("<b,r,y>bold red text on a yellow background</b,r,y>"
parse("<b,r,>bold red text</b,r,>
parse("<b,,y>bold regular text on a yellow background</b,,y>

# Unrecognized tags are left as-is.
parse("<b><element1></element1></b>")

For a list of markup tags, please refer to tags.py.

Ansimarkup may also be used as a command-line script:

$ python -m ansimarkup "<b>bold</b>" "<red>red</red>"

Ansimarkup uses the colorama library internally, which means that Windows support for ansi escape sequences is available by first running:

import colorama
colorama.init()

For more information on Windows support, consult the “Usage” section of the colorama documentation.

Limitations

Ansimarkup is a simple wrapper around colorama. It does very little in the way of validating that markup strings are valid. This is a conscious decision with the goal of keeping things simple and fast.

Unbalanced nesting, such as in the following example, will produce incorrect output:

<r><Y>1</r>2</Y>

License

Ansimarkup is released under the terms of the Revised BSD License.

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

ansimarkup-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansimarkup-0.1.1-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ansimarkup-0.1.1.tar.gz.

File metadata

  • Download URL: ansimarkup-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ansimarkup-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9d78cf74fe6172b6dde5360918bed200c6393f8f171bb9ab3d4c6431abd342fb
MD5 16d75a03aa8636b128331bbaa2427ab9
BLAKE2b-256 accbadfa228c606b009ce1a41282c1af220752454e81fc131fbf89465482d295

See more details on using hashes here.

File details

Details for the file ansimarkup-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ansimarkup-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 283325fcbd5023e6060b0e870980d1dd5fc6e29c679d8f36dd49b21049d7f431
MD5 d9ec9263a813e4f25c97d032802e2196
BLAKE2b-256 856cdd08c191044aa001380a294031e5d726c1aab8654285bde44c18f5c72fda

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