February 11, 2018

Centering a Block of Text in Conky

My goal was to display a “Message of the Day”, randomly-selected at boot, in the Conky text area. A message would be a block of text having varied length lines and an undefined width, as shown below. Importantly, I wanted the selected message’s block of text to be centered rather than left-justified.

Text block with varied length lines in a text file

The ability to dynamically center a block of text in Conky is something I had assumed would be straightforward. I carefully read through the Conky man page and did a lot of online searching for info on the subject. It's likely I overlooked something, used the wrong search terms, or searched the wrong places, but in the end I couldn’t really find anything about how to do it. So, if I wanted it, I'd have to do it myself.

The result was a Lua script called from in the .conkyrc file that reads lines of text from a text file, then adds a Conky center alignment object (i.e. ${alignc}) to the beginning of each line while also equalizing the length of each line. That way, while each line of the script-processed text is independently centered, the result is an effective centering of the block of text rather than centering of each of the varied length lines of the unprocessed text.

Here’s an example…

Text block displayed in Conky at various widths

The script is available free of charge at https://github.com/dfyockey/conky-text-block-centering. Enjoy!