Return to Blog Home

Highlighting Terminal Queries

Source 

Highlighting Terminal Queries

It's easy to take query-highlighting for granted, until you you use something without it...

Both my Twitch VOD Chat Downloader and #100Devs Asset Manager CLI support searching through text for queries, but whenever I used it and the results were long paragraphs of text, I had to use my terminals search feature to find the query I searched for.

Unfortunately these two tools were written in differing technologies: Python and Node.js, but I wouldn't let that stop me, so I started with the Twitch VOD Chat Downloader. I decided to go with colorama for no other reason then it appeared to be the most cross-platform library, but I did want to make this optional, so the searching script still would work without it.

Now I am making the assumption that terminals have black backgrounds with white text, therefore my "highlight"s are white backgrounds with black text, and while I am aware this will not have the desired effect on themed terminals, I could not find a cross-platform solution that allowed either the detection of the current background & foreground colors, or supported inverting the current terminal colors.

For my #100Devs Asset Manager, I went with chalk, as one might expect, the implementation was nearly identical for both: Perform a Regex-based replacement on the query, replacing it with the query but with the terminal escape codes to change the color accordingly.

Just like that, I had query highlighting in my search results.