It seems as though more and more of the simple command-line tools and small scripts that used to be bash or small c programs are slowly turning into python programs. Of course, we will just have to wait and see if this ultimately turns out to be a good idea. But in the meantime, next time you’re revamping or writing a new tool, why not spice it up with Rich?
Rich is a python library written by [Will McGugan] that offers text formatting, colors, graphs, progress bars, markdown, syntax highlighting, charts, and more through the power of ANSI codes. The best part is that it works with macOS, Windows, and Linux. In addition, it offers logging solutions that work out of the box. One of the best features of Rich is the inspect functionality. You can pass in an object, and it will use reflection to print a beautiful chart detailing what exactly the object is, helpful in debugging. The other feature is the traceback, which shows a formatted and annotated snapshot of relevant code on the stack during exceptions.
The source itself is well-written python with comments and typing information. There’s a good chance you’ll pick up a trick or two reading through it. Rich is used to build Textual (also by [Will]), which aims to be a GUI API that runs in the terminal. It served as an excellent example of what Rich is capable of. It is incredible how long these protocols have been around. [Will] even ran Rich on a Teletype Model 33. If you’re working with a bit more of a constrained environment, why not bring some color to your Arduino serial terminal?
6 thoughts on “Make Your Python CLI Tools Pop With Rich”
Interesting – I did some console manipulation with Python a while back and thought it should’ve been a travelled path.
I don’t think adding more blingbling solves more problems than it adds.
Maybe if you are a new programmer or choose a low quality library. When you get more experience, it will be easier to implement things like this with minimal complications.
What problems does it add? Why is clear, organized, formatted data presentation a problem not worth solving?
It results in shit like log4j (and billions in damage). Python’s logging module is very simple code for a reason.
Ooooh cool. I’ve been working up a little script that runs a few different utilities in parallel. The columns and live display could make a few things pretty easy for me!
Please be kind and respectful to help make the comments section excellent. (Comment Policy)