Now you can go beyond pimping your terminal with iTerm2 and oh-my-zsh. Rich CLI will allow you to add a whole new dimension to how you can view files on the command line. By providing a toolkit for adding colour and formatting, Rich CLI will not only transform CSV files into beautiful tables, enable your coding scripts to look just like they do in your IDE of choice, and provide special formatting for Markdown and JSON files, but it will also allow you to format the text you enter on the command line yourself.
Rich CLI has been created by Will McGugan and is available on Mac OS, Linux and Windows. Will is also the creator of a Python library called Rich and a Python framework called Textual. The Rich Python library enables Python programers to render rich text output to the terminal and has already been adopted by a wide variety of mainstream Python projects. The release of Rich CLI brings the Rich Python library capability to the command line.
"You will never use the built in CAT, HEAD or TAIL commands ever again !"
Rich CLI – Installation
The Rich Cli Tool can be installed into a specific directory or virtual environment simply with the pip install
command…
python -m pip install rich-cli
However, if you would like to be able to use Rich CLI globally across your machine (and I am sure once you start to use it you will want to be able to use it everywhere!) then I would recommend installing it with pipx.
Tools and packages installed with pipx are stored in a global virtual environment that will be accessible on your command line, to all your global directories and to all your virtual environments.
Python : How to create a clean learning environment with pyenv, pyenv-virtualenv & pipX
To install pipx on a Mac, run the following command in your terminal.
brew install pipx
pipx ensurepath
Then to install, upgrade or uninstall packages and tools with pipX, you simply use pipx install
, pipx upgrade
and pipx uninstall
as you would when using pip to manage your libraries. To install Rich CLI simply use the following command.
pipx install rich-cli
Rich CLI – Capabilities
Rich CLI is incredibly simple to use and results in beautiful formatting of a wide range of file types straight out of the box (with no options applied). Simply use the Rich
command followed by the path to your chosen file and you will amazed by the output you see in your terminal.
rich Ex11_Prime_No_Function.py`
The toolkit offers a lot of options that you can use to tailor the output to your exact preference. I will simply highlight a few of my favourites below so that you can get the feel of what it is capable of. To get a list of all available options and arguments simply invoke the built in help function by typing rich --help
into your terminal.
Line Numbers & Guides
A great feature for viewing code files in the terminal is the ability to add line numbers and indent guides. This is easily achieved by adding the options -n
for line numbers and -g
for guide lines.
rich Ex11_Prime_No_Function.py -n -g
Themes
Rich CLI even allows you to set some common Themes that will be applied to the displayed file. Themes can be set for an individual file
rich Ex11_Prime_No_Function.py --theme monokai
or set as a default setting in the environment variable
RICH_THEME=monokai
rich Ex11_Prime_No_Function.py
There are a wide range of themes to choose from in the Styles section on the Pygments.org website.
Pager
If the number of lines in the file is to big to comfortably view in the standard terminal window then you can use the --pager
option to open the file in a built in pager application where either cursor keys or the scroll bar can be used to navigate around the file.
CSV Files as Tables
Rich CLI will display the contents of CSV or TSV files in a very clearly presented and easy to read table. When coupled with the pager option --pager
illustrated above you can easily scroll up and down to review the contained data set.
The above examples hardly scratch the surface of Rich CLI’s capabilities. Some other key features worthy of a mention are:
-— head xx
or—-tail xx
to allow you to specify the number of lines from either the beginning or the end of the file to be displayed with the chosen formatting applied.- Specific JSON and MARKDOWN formatting
- Specific text block width, alignment and justification
- Coloured rulers constructed from user defined symbols
- Rich printing, panels and padding
- Read files directly from the internet from URL’s starting with HTTP/HTTPS
- Write the formatted output (all commands/options) to a HTML file
For full details of all commands/options on offer, along with code samples and example output images please refer to the very well constructed README.md file in the Rich CLI GitHub repo.
I really encourage you to try out Rich CLI and if you are a Python developer then you should take a look at both Rich and Textual as well. All three projects are still actively being maintained, with new features added regularly.
I certainly can’t wait to see what amazing new capabilities will be added to these great toolkits in the near future.
References
If you wish to explore the tools I have referenced in this article further then you can explore their full feature sets by following the links below…..