Category Archive "Code"

conky for my desktop

Published April 22nd, 2010 by Chad

At work I have a todo list that I maintain in a simple text file on my system. This todo list looks create and is readable on the command line (using cat) and in vim. However, I was really wanting it in a place where I could see it at a glance.

Also sometimes I miss the Ubuntu notifications in the top right corner of the screen (my background is black and the notifications don’t stand out on a black background since they are also black (I can’t wait until we can easily customize the notification system theme, anyone know how to do that yet?)).>

Another item of information that I love to have visible at all times is the list of my co-workers who are currently online (logged into IM). This helps if I need to ask one of them a quick question.

The solution: Conky

At first conky may scare you, I know it did me. The configuration files have a little bit of a learning curve that may be too steep for some. Conky is mainly used as a “light weight system monitor”. I however don’t need a system monitor (I’ve never really used the information they provide while I am on my desktop computer). System monitors are great for a laptop where I need to worry about what processes might be cramping my speed or draining my battery. I might also use the system monitor to check the temperature of my laptop or the battery level. For my desktop however I don’t need (or want) that stuff. Thankfully conky is so much more than a “light weight system monitor” I can set up scripts to run at any interval I would like and display the output in a nice way in a place where I can (almost) always see it.

Here is my conky configuration file:

background yes
own_window yes
own_window_type normal
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes

text_buffer_size 1024

use_xft yes
xftfont Inconsolata:size=12
#xftfont Angleterre Book:size=10

alignment top_right
gap_x 20
gap_y 20
maximum_width 400

TEXT

${font Cash Font:bold:size=12}${color #444}TODO:  ${hr 7}

${font}${color gray}${execpi 60 /home/chansen4/conkyTodo.py work}

${font Cash Font:bold:size=12}${color #444}NOTIFICATIONS:  ${hr 7}

${font}${color gray}${execpi 10 /home/chansen4/bin/getLastNotify.py}

${font Cash Font:bold:size=12}${color #444}PEEPS:  ${hr 7}

${execpi 10 conkyPidgin -I co-workers -C C -W U -A A -o -t ~/conkyPidgin.template}

Everything after the “TEXT” is what is displayed (before that are just settings). If you want to change some setting or another you start with the $ (dollar sign) and in {} (curly braces) afterwards specify the property to change and the value to be set.

For example: ${font Cash Font:bold:size=12} sets the font to be the “Cash Font” font, makes it bold and sets the size to 12 point. ${color #444} sets the color to be hex 444. ${hr 7} creates a horizontal rule of 7 pixels thick.

The real interesting part though are the ${execpi commands (the format for these commands is ${execpi [RUN TIME IN SEC] [SCRIPT], not too difficult). This will run an executable script and parse the output at more conky configuration code. There are three such calls in my configuration file. conkyTodo.py (my own script to parse my todo list and display it nicely in conky), getLastNotify.py (once again, my own script to parse the notify-osd.log file to get the last 4 notification messages and display them nicely in conky), and conkyPidgin (which is part of the conky hardcore package and gives one access to the Pidgin contacts).

If any of this is too confusing or you would like some more information/clarification just leave a comment and I will do my best to answer your questions. Thanks!

My current conky theme at work

My current conky theme at work

Update: Here is my getLastNotify.py script.

removing the top N lines from a file

Published October 9th, 2009 by Chad

Just a quick post here. I was messing around with some pretty large text files yesterday. I had a file with 2.9 million insert statements (one per line) that I was running on a database. It was taking forever so I stopped it. I new how many of the insert statements had run so I needed to get rid of the top million and a half so that they were not inserted again. I came across this nifty one-liner that made it really easy and didn’t take too long: sed '1,1500000d' inserts.sql > inserts2.sql Now I will explain what I understand it to be doing. sed is a command that is very powerful (I don’t know the first thing about using it really) for working with text and files. The 1,1500000 indicates that I want to do something to lines 1-1500000 of the file. The d tells sed that I want to delete those lines. Then specify the file you want to do that do and output the remaining contents (>) to a new file insert2.sql.

Pretty simple, but sed can be pretty involved.

one-liner to print out a random line from a file

Published May 6th, 2009 by Chad

Here is a bash script one-liner for printing out a random line from a file.

j=$(wc -l words.txt | awk '{print $1}'); i=$RANDOM; i=$(echo "$i%$j+1" | bc); sed -n ${i}p words.txt

The variable j has stored in it the total number of lines in the file with the help of the wc and awk commands. The variable i has stored in it a random number that is then changed to be in between 1 and the number of lines in the file, j. The random number is obtained by assigning i to $RANDOM and then piping a mod operation to the bc calculator. The resulting random line number, i, is displayed using the sed command.

PS: I know the script is on two lines, that that is just because there wasn’t room on the blog to go all the way across.

calendar generation

Published January 21st, 2009 by Chad

At work I was faced with the task of trying to generate a calendar and adding events to the specific dates that they appeared on. I wanted to do this in such a way that I could generate the current month, for example, without having to go in and change the code from the previous month.

I also didn’t do a lot of research on the web for this kind of thing, instead I just came up with a quick way to find out what a month looks like by just knowing the number of days in the month and the day of the week on which the month starts.

Pseudo Code totalDays = number of days in the month; startDay = day of the week the month starts 1-7; numberOfDaysInFirstWeek = -1 * startDay + 8; remainingDaysAfterFirstWeek = totalDays - numberOfDaysInFirstWeek; numberOfFullWeeks = floor(remainingDaysAfterFirstWeek / 7); numberOfDaysInLastWeek = remainingDaysAfterFirstWeek % 7;

After determining these values the task is a little self explainatory. Take the numberOfDaysInFirstWeek and loop that many times to create those days, starting withstartDay. Loop for numberOfFullWeeks to create the middle weeks, than loop for numberOfDaysInLastWeek to finish off the month.