Simple Clock

From awesome
Jump to: navigation, search

These are simple instructions to add a clock to your Awesome status bar. Or, more basically, this example is used to create a simple widget using simple techniques. While the script used to populate the data isn't the best, it's functional. At the bottom of the page is a better script to be used to achieve the same results.

With these simple techniques, you can create widgets that update from virtually any source of data.

Contents

Adding the text field to your .awesomerc

Edit your ~/.awesomerc and add to the "screen/statusbar" section - this is just a skeleton config file to make sure the code is put into the right spot:

screen 0
{ 
 ... config file snipped ...
statusbar mystatusbar {
   ...config file snipped ...
  taglist mytag {
    ...config file snipped ...
  }
  layoutinfo mylayout {
    ...config file snipped ...
  }
  #
  # Add the lines below to create the clock
  #
  textbox clock {
    text_align = "right"
    align = "right" 
    width = "100" # Note: Once you get this working, you may need to tweak the 'width' a bit depending on the font you're using.
  }
  #
  # Stop here
  #
}


Populate the data in the widget (awesome 2.2)

Create ~/bin/awesome-clock (You can add .sh if you would like)

#!/bin/sh
# this script is for awesome 2.2

updateClock() {
  # See 'man date' to see the possible replacements for the % fields.
  echo "0 widget_tell clock `date +\"%a, %b %d %I:%M%p\"`" | awesome-client
}

while true; do
  updateClock
  sleep 10;
done

Make it executable:

 $ chmod a+x ~/bin/awesome-clock

For awesome 2.3.x just change the piped echo command in the script above to:

   echo "0 widget_tell mystatusbar clock text `date +\"%a, %b %d %I:%M%p\"`" | awesome-client

Populate the data in the widget (awesome-2.3)

Create ~/bin/awesome-clock (You can add .sh if you would like)

#!/bin/sh
# this script is for awesome 2.3

# Point this to a directory you can write to. 
#PID=~/var/run/awesome_clock.pid
PID=~/bin/awesome_clock.pid

updateClock() {
   # See 'man date' to see the possible replacements for the % fields.
   echo "0 widget_tell mystatusbar clock text "   " `date +\"%a, %b %d %I:%M %p\"`" | awesome-client
}

# This will prevent this script from running multiple times
if [ -f $PID ] ; then
   kill -0 `cat $PID` 2> /dev/null
   if [ $? -ne 0 ] ; then
       echo "["`date`"] $0 killed bogus lock file ["`cat $PID`"]"
   else
       echo "["`date`"] $0 already running ["`cat $PID`"]"
       exit
   fi
fi
echo $$ > $PID

while true; do
  updateClock
  sleep 10;
done

Make it executable:

 $ chmod a+x ~/bin/awesome-clock

If you get a message like:

W: awesome: send_msg:60: can't write to /home/username/.awesome_ctl.0

then just creat a blank file called ".awesome_ctl.0" in your home directory.

Troubleshooting

Before adding to .xinitrc, doing a manual test to make sure everything is set up correctly is a good idea. To test or troubleshoot this setup, you can run (for awesome-2.3)

 echo "0 widget_tell mystatusbar clock text "   " `date +\"%a, %b %d %I:%M %p\"`" | awesome-client

From a terminal command line to verify the clock update function works properly

Make the clock auto-start with X

Add the following lines to ~/.xinitrc if you want it to start up every time you start awesome

   ~/bin/awesome-clock &
   exec awesome


A Better Method for sending the data

The above methods are to show the flexibility of using the awesome-client command. A better script to accomplish the same is the below:

Create ~/bin/better-awesome-clock

 #!/bin/sh
 #
 while true 
 do
       if [ -S ~/.awesome_ctl.0 ]; then
               while true
               do
                       # See 'man date' to see the possible replacements for the % fields.
                       # uncomment the following line for use with awesome 2.3
                       # echo "0 widget_tell mystatusbar clock text "   " `date +\"%a, %b %d %I:%M %p\"`"
                       echo "0 widget_tell clock "   " `date +\"%a, %b %d %I:%M %p\"`"
                       echo "" # an empty line flushes data inside awesome
                       sleep 1
               done | awesome-client
       else
           sleep 1
       fi
 done
Personal tools