Thursday, March 28, 2013

A geeky calendar

To continue a little bit with some of the geektool things, i was browsing around and came across a page with a small list of common user commands at applevie.ws and found a calendar script.
The script they reference there is


cal | sed -e '1d' -e '2p;2p;2p;2p' | sed -e '$!N;s/\n/ /' -e '$!N;s/\n/ /' -e '$!N;s/\n/ /' -e '$!N;s/\n/ /' | sed "s/^/ /;s/$/ /;s/ $(date +%e) /\|$(date +%e)\|/" 


and that will give you a nice little plugin that looks like this

With some slight tweaking to the sed script you can make the dates line up nice and pretty, but i felt that the calendar in that format was a little too long for me, and I always perfered the square date.
So using sed I was able to take the above script and warp it slightly to this

cal  | sed "s/^/ /;s/$/ /;s/ $(date +%e) /\[$(date +%e)\]/"

and that gives me the following calendar with [ ] around todays date.
 Now this isnt anything advanced, there are some ruby scripts around that add color to the bars around the date, but I like the simplicity of this.


1 comment:

  1. I did not like how the original script manually dealt with repeating the days of the week and removing line breaks, so I took it as an excuse to familiarize myself a bit with sed (which previously I had only used for simple pattern substitutions). So after some tinkering I put this together. I think that it made for a nice little learning exercise. This was done in a Redhat environmnent, so your results may vary


    cal | sed -n '1d; /Su/ { h;d }; :a; /[0-9]/ { N; s/\n/ /; x; s/\(Su[^\(Sa\)]\+Sa\)/\1 \1/; x }; $!ba; x; p; x; p' | sed "s/ \($(date +%e)\) /[\1]/"

    and here is the command breakdown
    # Pipe (aka send) call command output to sed
    cal | sed
    # Do not print each line unless instructed to
    -n
    # delete first line
    '1d;
    # If line matches "Su" do commands in code block
    /Su/ {
    # Copy line into Hold buffer
    h;
    # Delete the line and close our code block
    d; };
    # Set a section label
    :a;
    # If line contains a number do commands in code block
    /[0-9]/ {
    # Append the next line to our current pattern buffer (so we are now working with 2 lines at once)
    N;
    # Replace line breaks with spaces
    s/\n/ /;
    # swap hold buffer with our pattern buffer (so we can manipulate the line we previously saved in the hold buffer)
    x;
    # Find the first instance of Su through Sa and duplicate it (this grows our week day labels by one week each time it runs)
    s/\(Su[^\(Sa\)]\+Sa\)/\1 \1/;
    # Swap the hold and pattern buffers again (putting the newly modified hold pattern back into the hold buffer) and close the code block
    x };
    # When we have run out of lines to append, we will finally hit an end of line (the "$") until that happens, do branch (aka goto) label "a"
    $!ba;
    # Swap the hold and pattern buffers to print out our week day labels
    x; p;
    # Swap the hold and pattern buffers again to print out the days of the month
    x; p'
    # Pipe everything we just did to another sed command where we will take care of the current day indicator
    | sed
    # Replace the leading and trailing spaces (as reported by command 'date +%e') with brackets
    "s/ \($(date +%e)\) /[\1]/"


    cal | sed -n '1d; /Su/ { h }; :a; /[0-9]/ { N; s/\n/ /; x; s/\(Su[^\(Sa\)]\+Sa\)/\1 \1/; x }; $!ba; x; p; x; p' | sed "s/ \($(date +%e)\) /[\1]/"

    and here is the command breakdown
    cal | sed # Pipe (aka redirect) output from the 'cal' command to sed
    -n # Do not print each line unless instructed to
    '1d; # delete first line
    /Su/ { # If line matches "Su" do commands in code block ( everything between { } )
    h; # Copy line into Hold buffer
    d }; # Delete the line and close our code block
    :a; # Set a section label
    /[0-9]/ { # If line contains a number do commands in code block
    N; # Append the next line to our current pattern buffer (so we are now working with 2 lines at once)
    s/\n/ /; # Replace line breaks with spaces
    x; # swap hold buffer with our pattern buffer (so we can manipulate the line we previously saved in the hold buffer)
    s/\(Su[^\(Sa\)]\+Sa\)/\1 \1/; # Find the first instance of Su through Sa and duplicate it (this grows our week day labels by one week each time it runs)
    x }; # Swap the hold and pattern buffers again (putting the newly modified hold pattern back into the hold buffer) and close the code block
    $ # When we have run out of lines to append, we will finally hit an end of line (the "$")
    !ba; # At the end of line, do NOT (not being the "!") brance / go to (the "b") label "a"
    x; p; # Swap the hold and pattern buffers to print out our week day labels
    x; p' # Swap the hold and pattern buffers again to print out the days of the month
    | sed # Pipe everything we just did to another sed command
    "s/ \($(date +%e)\) /[\1]/" # Replace the leading and trailing spaces (as reported by command 'date +%e') with brackets

    ReplyDelete