PyTput
TL;DR: You can format your message with
{message:bold,underline,purple}
Simple executable and Python3 API to format messages using colors and styles from tput:
pytputexecutable to be used in any shell-like script- Python3 module to format messages directly in any python application
Install
Install via pip:
# Install pip for python3
$ sudo apt install python3 python3-pip python3-setuptools
# Install from PyPI
$ pip3 install --user pytput
# ... Or directly from the sources
$ pip3 install --user git+https://github.com/essembeh/pytput
To setup a development environment:
$ git clone https://github.com/essembeh/pytput
$ cd pytput
$ make venv
$ make install
$ source venv/bin/activate
(venv) $ pytput --help
Usage
Using tput_format and tput_print
tput_format allow to format messages using colors and styles.
This is nothing more than a shortcut to use TputFormatter.
tput_print simply prints the message after formatting it.
from pytput import tput_format, tput_print
# Create a format message
fmt = "Hello {who:red,bold}"
# You can print it
print(fmt)
# When formatting, styles are applied
a = tput_format(fmt, who="World")
print(a)
# Equivalent to
tput_print(fmt, who="World")
# And you can also use *constant* like {'today':bold} will be the string "today" with bold style applied
fmt += ", how are you {'today':green,underline}"
tput_print(fmt, who="User")
Styles can be combined with
','like{message:underline,bold,yellow}
You can also reuse
string.Formatterformat specification like{myint:05d,underline,bold,yellow}or{mystr:.10,underline,dim,red}
You can apply a style to constant strings with
{'today':bold}or{"today":bold}
Using a TputFormatter class
You can use the TputFormatter class to decorate variables with styles.
from pytput import TputFormatter
tf = TputFormatter()
# Build a colored string and print it
print(tf.format("{'Hello':bg_yellow,bold} {who:cyan,underline}!", who="World"))
# You can combine multiple styles and common str.format spec
print(tf.format("{'Hello':.2,bg_yellow,underline} {myvalue:04d,cyan}!", myvalue=42))
Here is the list of available styles
| Formatter keyword | tput command |
|---|---|
| bold | tput bold |
| dim | tput dim |
| underline | tput smul |
| blink | tput blink |
| standout | tput smso |
| reverse | tput rev |
| reset | tput sgr0 |
| black | tput setaf 0 |
| red | tput setaf 1 |
| green | tput setaf 2 |
| yellow | tput setaf 3 |
| blue | tput setaf 4 |
| purple | tput setaf 5 |
| cyan | tput setaf 6 |
| white | tput setaf 7 |
| bg_black | tput setab 0 |
| bg_red | tput setab 1 |
| bg_green | tput setab 2 |
| bg_yellow | tput setab 3 |
| bg_blue | tput setab 4 |
| bg_purple | tput setab 5 |
| bg_cyan | tput setab 6 |
| bg_white | tput setab 7 |
Directly use Style
from pytput import Style
print(Style.RED.apply("Hello"), Style.GREEN.apply("World"))
Using pytput executable
PyTput comes with an handy executable to use colors and styles directly from the command line. This is usefull to customize messages in shell scripts for example.
$ pytput '{"Hello":red} {0:green}!' 'World'
$ pytput "{'This is':bold,red} {0:underline,dim,yellow} {1:bg_purple,yellow,bold}" "a message" "with styles ;)"
$ pytput "{0:red,bold,underline}" "Simple error message"
$ pytput "{'Another message':bg_purple,white,bold}"
See
pytput --helpfor more details.
Disabling pytput
By default, pytput API is disabled if sys.stdout.isatty() is False, so you won't write any color nor style if you pipe the output or redirect it in a file.
Using pytput executable, you can force colors and styles using the --force argument.
# You get colors by default
$ pytput '{0:red} {"World":green}!' 'Hello'
# Colors will be disabled
$ pytput '{0:red} {"World":green}!' 'Hello' | cat
$ pytput '{0:red} {"World":green}!' 'Hello' > /tmp/pytput.txt
$ cat /tmp/pytput.txt
# Colors will be enabled
$ pytput --force '{0:red} {"World":green}!' 'Hello' | cat
$ pytput --force '{0:red} {"World":green}!' 'Hello' > /tmp/pytput.txt
$ cat /tmp/pytput.txt
Using pytput python3 API, you can force styles and colors even if sys.stdout is not a TTY using TputFormatter:
from pytput import TputFormatter, tput_print
# These lines won't have colors if you redirect stdout to a file
print(TputFormatter().format("{0:red} {'World':green}!", "Hello"))
tput_print("{0:red} {'World':green}!", "Hello"))
# These line will have colors even if stdout is redirected
print(TputFormatter(check_tty=False).format("{0:red} {'World':green}!", "Hello"))
tput_print("{0:red} {'World':green}!", "Hello", check_tty=False))
You can totally disable pytput by setting PYTPUT_DISABLE variable in environment.
# Colors will be enabled
$ pytput '{0:red} {"World":green}!' 'Hello'
# Colors will be disabled
$ PYTPUT_DISABLE=1 pytput '{0:red} {"World":green}!' 'Hello'
Note: When using pytput API (
tput_print,TputFormatter...), setting the environment variablePYTPUT_DISABLE=1will also disable all colors and styles.
Metadata
Release files for pytput 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytput-0.4.0.tar.gz | 200.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytput-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 213.5 kB
Release files / pytput-0.4.0.tar.gz
| Download URL | pytput-0.4.0.tar.gz |
|---|---|
| Size | 200.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7b02097f56d36527dc11af9565de605dd9dce030c530d65909a1cb8c5ebc6232
|
|
BLAKE2b-256 checksum How to use checksums |
f91504d5368aece7bc1a94469059b35ec24b45acae0b3f2b13528cf04d6c0274
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
|
Release files / pytput-0.4.0-py3-none-any.whl
| Download URL | pytput-0.4.0-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b4113d65f077fdd12d20c57a4eb3cd572b61c35dd8d590d994380eb24959de82
|
|
BLAKE2b-256 checksum How to use checksums |
167d92fb4041f2df009889e623a0419e827b71b78a87c243273f029c3c0d0246
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
|
Security for pytput 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.