Simple Terminal Colors

October 9th, 2023
tech
I work in the terminal a lot and I often want to write programs that color their output a bit. For example, perhaps I'm looking at a sequencing read and I want to know what part of it was the reason it was flagged. I could use colorama, termcolor, or another library, but including a dependency for something this simple is not really worth it. Instead, printing colored text is as simple as:

COLOR_RED_BOLD = '\x1b[1;31m'
COLOR_BLUE_BOLD = '\x1b[1;34m'
COLOR_END = '\x1b[0m'
print(
  "One fish, two fish, " +
  COLOR_RED_BOLD + "red" +
  COLOR_END + " fish, " +
  COLOR_BLUE_BOLD + "blue" +
  COLOR_END + " fish.")

I usually just want one or two highlight colors, typically in bold, and rarely find the six standard colors limiting. It's especially helpful if you want to draw your eye to something specific, while maintaining the context around it when you need to look further.

How compatible is this? Will someone with a non-ANSI terminal someday run my code and complain? One data point is that I wrote icdiff with this approach in 2009, and it's been reasonably popular. While I've gotten hundreds of bug reports this has never been one people have complained about.

Comment via: facebook, lesswrong, mastodon, substack

Recent posts on blogs I like:

The One That I Want Another Sister

I want a baby sister or brother. Because I want to play with another kid, and I don't want to play with Josie because she is in preschool and I won't play with her very often. So I want to play with someone very often who is not just two people. …

via Nora Wise's Blog Posts September 5, 2026

Linkpost for September

On September 19 and 20, Sparrow Infrastructure is putting on The Overhang, a conference in Washington D.C.

via Thing of Things September 4, 2026

Ed Zitron's AI prediction track record

I was curious how well the predictions of the most widely cited AI skeptic I've seen (Ed Zitron) have done, so I looked at how his predictions panned out. To disclose my own biases, I've never had a particularly strong pro or anti AI progress posi…

via Posts on September 1, 2026

more     (via openring)