{"id":114,"date":"2009-08-03T19:34:09","date_gmt":"2009-08-04T03:34:09","guid":{"rendered":"http:\/\/sethjust.com\/?p=114"},"modified":"2011-07-24T10:03:16","modified_gmt":"2011-07-24T18:03:16","slug":"a-useful-calendar-in-conky","status":"publish","type":"post","link":"https:\/\/sethjust.com\/2009\/08\/03\/a-useful-calendar-in-conky\/","title":{"rendered":"Conky Calendar with Date Highlight"},"content":{"rendered":"

Since I got a new desktop a month or so ago, I’ve been running Ubuntu<\/a> as my main operating system, and am using Conky<\/a> for a nice heads-up-display. There are a lot of articles on the web about both Ubuntu and Conky, but one thing I couldn’t find a good, accurate how-to on was getting a calendar that highlighted the current date. While getting the calendar is easy using the cal command, actually getting the date highlighted is somewhat hard, and all of the articles I found suggested methods that broke in various situations. However, I managed to get it all worked out, and have a beautiful calendar that looks like this:<\/p>\n

\"\"<\/a><\/p>\n

After the jump, I’ll give you the code and explain how it all works.<\/p>\n

How it Works:<\/h2>\n

The code from ~\/.conkyrc that makes this calendar is simple:<\/p>\n

Edit: I’ve updated the code as Zacco suggested in the comments to fix a minor bug on certain dates.<\/p>\n

${color orange}CALENDAR ${hr 2}$color\r\n${execpi 60 DJS=`date +%_d`; cal | sed s\/\"\\(^\\|[^0-9]\\)$DJS\"'\\b'\/'\\1${color orange}'\"$DJS\"'$color'\/}}<\/pre>\n

This is a somewhat complicated expression, but what it does is fairly simple. The first line simply draws the title for the section, and it’s the second line that does the magic. Let me break it down and explain it.<\/p>\n

${execpi 60<\/pre>\n

This command tells Conky to execute the next argument every 60 seconds, and, importantly, parse the output of that argument. This way, when we put in commands that change the text color, Conky will obey them instead of printing them.<\/p>\n

DJS=`date +%_d`;<\/pre>\n

DJS (which stands for “date” followed by my initials) is a temporary variable that holds the current day of the month, padded with a leading space, if necessary (so that we’ll catch the first of the month, not every 1 in the calendar, for example).<\/p>\n

cal |<\/pre>\n

cal simply prints out a simple calendar, and the pipe feeds that output through to the next commmand:<\/p>\n

\r\n
sed s\/\"\\([^0-9]\\)$DJS\"'\\b'\/'${color orange}'\"$DJS\"'$color'\/<\/pre>\n

This is one big mess of a command, but the main body is a regex<\/a> that sed takes as an argument. The regex searches for the string “\\([^0-9]\\)$DJS”‘\\b’, which means the contents of the variable DJS (which we set earlier) preceded by anything other than a digit, and followed by a word boundary – a change from printable characters to white space. This means that it will find the current date (one or two digits) but won’t find the 3 of the 30th on the third of the month. It also means that it will work correctly if the current date is on the far left or right column of the calendar.<\/p>\n

The second half of the regex simply wraps what it found the first time around in a couple of Conky commands that change to color to orange, and then back to what it was before.<\/p>\n

Lastly, the closing bracket (}) at the end of the line closes out the opening one that is required for all Conky commands with arguments.<\/p>\n

Now go forth and cover your desktop in beautiful calendars.<\/p>\n

Update: <\/strong>Erik in the comments has a modification to center the entire calendar, if left-aligning isn’t for you.<\/p>\n","protected":false},"excerpt":{"rendered":"

Since I got a new desktop a month or so ago, I’ve been running Ubuntu as my main operating system, and am using Conky for a nice heads-up-display. There are a lot of articles on the web about both Ubuntu and Conky, but one thing I couldn’t find a good, accurate how-to on was getting […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[97,98,29,99,100,43,77,84,96,159,95],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":14,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/posts\/114\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sethjust.com\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}