top | item 29945848

Rich: A Python library for rich text and formatting in the terminal

389 points| willm | 4 years ago |github.com

82 comments

order
[+] pedrovhb|4 years ago|reply
An extremely useful hidden gem in Rich is its `inspect` function [1].

When called on an object, it pretty prints a list of its public attributes (though you can also request private and dunder attributes) [2]. It can also be called on a class or function; it'll then pretty print the docstrings, parameters with types, methods, etc. It's great for debugging and for starting to code with libraries you're not familiar with, don't have comprehensive documentation, or have some dynamic aspect.

In a similar vein, `objexplore` [3] is another library that lets you dive into an object's nested attributes to understand how it's laid out.

In interactive debugging sessions I often find these to be more useful than the IDE's features which provide similar functionality.

[1] https://rich.readthedocs.io/en/stable/reference/init.html#ri...

[2] https://i2.paste.pics/798cff2903f6b3351289a24c839d4f44.png

[3] https://github.com/kylepollina/objexplore

[+] orf|4 years ago|reply
The ipython ? Syntax also does this: “some_object?”
[+] samwillis|4 years ago|reply
See also Textual by the same developer. Really impressive work.

https://github.com/Textualize/textual

[+] agumonkey|4 years ago|reply
oh man

    python -m textual.app 
is so satisfying. You get web like layout dynamicism without the whole browser stack.
[+] elteto|4 years ago|reply
Woah. That calculator and file tree _look_ like native widgets. Impressive indeed.
[+] bitigchi|4 years ago|reply
I am not sure if it's only me, but as time passes I find myself using more and more simple Terminal stuff. No fancy air/earth lines, no fancy formatting, no fancy ligatures or typefaces.
[+] planetafro|4 years ago|reply
Oddly, I'm the opposite. Working in devops and vscode, I find the visual aspect of color and logical formatting improves my work dramatically. Small tools like "bat" vs. "cat" on the terminal are great at quick data parsing for things like yaml, json, and the like. Staying old school for the sake of old school is a bit of a fallacy IMHO.
[+] TheCondor|4 years ago|reply
We have 5k monitors, Gpus with 12GB of RAM, fast networks, etc. color in consoles, underline, bold and italics seem overdue.

What rubs me wrong is the collective halfassedness of it all. When less craps out ANSI codes, when the terminal tears and your cursor is offset because it got confused about control characters, dozens of different color configs rather than a system theme.

There was this time in the Linux dark ages, red hat Linux 2 era. The enlightenment window manager was considered part of protocol GNOME, there were a lot of really exotic or organic themes, skinning was the hotness. Windows couldn’t do it. Ux be damned, if you wanted your desktop to look like some sort of demonic rune set, you could do it. Rationality, ux, consistency, etc took over. Cli skinning never seems to grow that way. I keep hoping some hacker that doesn’t know better will write a new terminal emulator, replaces ANSI codes with some minimal html and invents console css and the community embraces it.

[+] jonpalmisc|4 years ago|reply
I tend to agree with this. I'm not anti-formatting, but it seems that in the majority of cases, the "formatting" isn't done artfully, and actively makes it harder to read command output, etc.
[+] rajandatta|4 years ago|reply
Absolutely. I still have a Terminal window open all the time to do various tasks. The ability to say 'find me x or y or z' and not having the overhead of looking through other visual clutter. Plus a few well chosen directory jump shortcuts make things easier.
[+] SavantIdiot|4 years ago|reply
Nope, not just you. Been coding since the 80's, and command-line tooling has become more noisy and less parser/redirect-friendly over the past decade (but option parsers have def. gotten better). To be fair, once terminals started supporting escape color codes, things started to fall apart (circa late 90's). IMHO.

EDIT: Just noticed a peer comment below captures the issues with ANSI way better than I did.

[+] sdfgsdf|4 years ago|reply
Perhaps because you're gradually learning to distinguish what's style and what's substance?
[+] gnubison|4 years ago|reply
Exact same thing happened to me. At one point I tried out not even including $(pwd) in the prompt, and … turns out it actually works fine.
[+] gloriana|4 years ago|reply
same, it's too much to keep up with. would rather miss out at this point.
[+] yeetaccount4|4 years ago|reply
Same. I don’t even use syntax highlighting anymore. It changes how you focus.

Not sure how people put up with all that powerline stuff either, or multi-line PS1s that have shit on either side. Reminds me of geocities.

[+] asicsp|4 years ago|reply
Past discussion:

Show HN: Python lib for rich text, markdown, tables, etc. in the terminal (https://news.ycombinator.com/item?id=23070821 2020, 47 comments)

Related discussion:

Textual: a Python text user interface with Rich as the renderer (https://news.ycombinator.com/item?id=27526031 7 months ago, 21 comments)

[+] 0x008|4 years ago|reply
I feel this library gets posted here every month. But maybe it is just my personal impression between HN and GitHub trending.
[+] dayjah|4 years ago|reply
The way structlog and rich work together makes for a really great and simple development experience;

Stack traces are presented in a gorgeous fashion, log.debug lines have lots of great meta data with them. I almost always start new projects with those as my first imports.

[+] kubami|4 years ago|reply
does it just work out of the box?
[+] noufalibrahim|4 years ago|reply
I've been following Will McGugan (the author) on twitter for a while. I learnt PyGame from his book. He more or less documented his thought processes on twitter and it's been very nice to see it develop over time.

It got me thinking that, atleast for technology developers, this is the ideal use of twitter. An uncensored "thoughtstream". Some tools to mine that and extract lessons might be an interesting project.

[+] nyellin|4 years ago|reply
Very nice. Two other great python libraries for cli display are:

1 tabulate: helps render ASCII tables in various formats (https://github.com/astanin/python-tabulate)

2. typer: write cli commands by decorating python functions. It uses type annotations to automatically parse input (https://typer.tiangolo.com/)

We're using both of those in our Kubernetes troubleshooting tool (https://robusta.dev). Going to look at adding Rich too

[+] ZeroGravitas|4 years ago|reply
Is there a companion project for the input side of these apps, like a modernized readline type thing yet?
[+] pridkett|4 years ago|reply
This would be so helpful. About a year ago I got the combination of FZF and bat working for my command line history. It’s wild how much better and easier that makes it to find complex commands from the past.
[+] aseipp|4 years ago|reply
Isocline, a readline alternative by Daan Leijen (an absolute legend) is about the closest I've found to this: https://github.com/daanx/isocline

That said it doesn't push "input controls" quite to the extreme Rich/Textual do for UI controls, but it might be a starting point to look into.

[+] rajandatta|4 years ago|reply
The library looks brilliant! The author's clearly put a lot of thought and effort into it. Looking forward to using it.
[+] armchairhacker|4 years ago|reply
Great library and an amazing README. I kind of wish this functionality was built into the language tooling and other languages.
[+] asimjalis|4 years ago|reply
I especially like pretty print in rich. Makes it so much fun to introspect on deeply nested objects and JSON.
[+] rjzzleep|4 years ago|reply
This looks interesting. Can anyone explain to me why cli rich text editors seemingly died after DOS?
[+] mrits|4 years ago|reply
I think the quick answer is that they most certainly didn't. Vim or Emacs are popular and fill most use cases while obviously working in dozens of other languages. For actual text rendering use cases their are plenty of popular libraries such as curses
[+] willm|4 years ago|reply
Some Jerk called Tim Berners-Lee invented the “web”
[+] jack_pp|4 years ago|reply
Anyone know what font he's using for the screenshots?
[+] atarian|4 years ago|reply
It looks like Fira Code.
[+] comonoid|4 years ago|reply
It seems Rich is not compatible with communism.
[+] iqanq|4 years ago|reply
I hate CLI utilities that think they are so cool for turning my terminal into a disco.
[+] willm|4 years ago|reply
Colour hating curmudgeons can set NO_COLOR env var, which Rich will respect.
[+] kortex|4 years ago|reply
So don't use them or configure your terminal for monochrome? I for one welcome the disco.

Well, you can tell from the way I use nGrok, I'm a jq man, no time for awk