styled: Style your terminal with ease!

Posted 4 years, 1 month ago

Introduction

Welcome to styled, a simple Python package that makes a breeze of writing beautiful text to the terminal. The main innovation behind styled is the dead-simple user interface which does away with the user's need to know anything other than the style names. Behind the scenes styled handles everything to keep your styles consistent and redundant and informing you when you have made formatting errors.

styled was borne out of the frustration encountered in using other packages which muddle the boundary between user-space and design-space. The user should be free to be a user and it is the designer's job to hide the implementation behind a simple user interface that facilitates the user's task. This is what I've tried to do. If I have failed to live up to this please let me know. I'm sure together we can come up with something better.

Getting Started

To get it from PyPI use

pip install styled

It's best to do this in a virtual environment.

# anaconda/miniconda
conda create -n styled python
source activate styled
pip install styled

# virtualenv
virtualenv /path/to/env/styled -p /path/to/python
source /path/to/envs/styled/bin/activate
pip install styled

Using styled is easy. Try this out in your Python console.

>>> from styled import Styled
>>> s = Styled("We are [[ 'bold'|bold ]] men!")
>>> print(s)
We are bold men!

You can perform string formatting directly in the constructor.

>>> s = Styled("There were up to [[ '{}'|bold:fg-red ]] people who handed over themselves to the \
[[ '{police}'|fg-black:bg-red:bold ]].", 24, police='policia')
>>> print(s)
There were up to 24 people who handed over themselves to the policia.

You have to delimit the text you want to style with [[ ... ]] then make sure that the following three (3) conditions hold:

  • separate the text from the styles with a pipe (|),
  • quote the text part with either a pair of single ('...') or double ("...") quotes, then
  • separate each style with a colon (:)

There are three (3) types of styles:

  • text styling such as bold, blink, underline etc.
  • foreground colours, such as fg-red,
  • background colours, such as bg-blue.

If you want to style an extended piece of text you can use a special additional style marker (no-end) indicating to continue the styling for as long as possible. This can be useful if you want to style a long stretch of text.

>>> s = Styled("There were up to [[ '{}'|bold:fg-red:no-end ]] people who handed over themselves to the \
[[ '{police}'|fg-black:bg-red:bold ]].", 24, police='policia')
>>> print(s)
There were up to 24 people who handed over themselves to the policia.

The above example will have all the text until the next marker affected by the red foreground styling. You can also manually enforce an end marker by using yes-end as a style. By default all style markers will terminate on the text to be styled. So, for example

>>> # an example of a terminating end marker
>>> s = Styled("There were up to [[ '{}'|bold:fg-red:no-end ]] people who handed over themselves [[ ''|yes-end ]] to the \
[[ '{police}'|fg-black:bg-red:bold ]].", 24, police='policia')
>>> print(s)
There were up to 24 people who handed over themselves to the policia.

will suspend red foreground at the end of the word 'themselves'.

More

You can find more information at the Github repository.

Edit