Monthly Archives: December 2008

Calm before the Storm

I took this picture on 12/17/2008 right before the snow storm arrived.

 

The Space Needle before the Storm

The Space Needle before the Storm

Advertisements

Utility to Reformat Comment Lines

The Problem
These days, my work requires writing scripts in Tcl, bash, or Python. These scripts have one attribute in common: they use the hash sign (#) to start a comment. I often need to “reflow” my comments, i.e. reformat the comment lines to fit a number of columns (72 in my case) within my vi editor. For example, given the following comment lines:

# Extracts from a line the prefix and the rest of the line, with
# extra spaces trimmed.
# The prefix consists of any white space
# preceding the hash (#) and a space.
# Assume that the block of comment lines has the same indentation level.

I would like a tool to reformat them to something like:

# Extracts from a line the prefix and the rest of the line, with
# extra spaces trimmed. The prefix consists of any white space
# preceding the hash (#) and a space. Assume that the block of
# comment lines has the same indentation level.

The Solution
After my search for such a tool failed, partly because I did not look that hard, I decided to write that tool myself. After some research, I learned that Perl’s Text::Wrap module can satisfy my need. Thus, I wrote a short script called ‘cf’ — short for comment format or comment flow. Before using this script, you should place it in a directory in your path (~/bin for me). To use it within vi, it helps to turn on line numbering (via the ‘:set nu’ command). If the comments spans from line 4 to line 7, then to reflow it, you should issue the following command:
:4,7!cf

Limitations of cf
Cf assumes the comment lines share the same indentation level. If they don’t, the result will be unpredictable. Secondly, to simplify my script, I assume the users want to wrap the comments within 72 columns. If this is not the case, you can edit the script and change it to suit your need.

The Script

#!/usr/bin/env perl -n

# cf -- comment formatter
# by Hai Vu
# Last update: 2008-12-15
#
# A Perl script to reformat a comment block to wrap the text to fit
# within a number of columns. I design this script to work as a
# filter within the vi/vim editor. For example, to use this script to
# reformat from line 4 to line 8, issue the following command: line 4
# to line 8, issue the following command:
# :4,8!cf

use Text::Wrap;

# ----------------------------------------------------------------------
# Extracts from a line the prefix and the rest of the line, with
# extra spaces trimmed. The prefix consists of any white space
# preceding the hash (#) and a space. Assume that the block of
# comment lines has the same indentation level.

# Perform the regular expression to extract the comment character (#)
# including the preceding white spaces, and the rest of the line.
chomp;
/^(\s*# )\s*(\S.*\S)\s*$/;
$prefix = $1;
$line = $2;

# Add the line to the array for later processing
push(@lines, $line);

# ----------------------------------------------------------------------
# At the end of the input, wrap the lines, then print them out with the
# prefix.

END {
    # Set the width
    $columns = 72;
    local($Text::Wrap::columns) = $columns - length($prefix);

    # Wrap the text and print
    $text = wrap($prefix, $prefix, @lines);
    print $text;
}

iTunes: Move to Trash Shortcut

I often need to delete old podcasts to make room on my hard drive. My preferred way to delete an item is to highlight it, then hit the delete key. iTunes then displays a dialog similar to this one:
iTunes dialog asking to keep the file or move it to the trash
Since the “Keep File” button is highlighted, it is the default action if the user hits the return (also known as the enter) key. Instead of hitting the Cancel button to abort the operation, the user can hit the esc key. What about the “Move to Trash” button? After a few trials, I found it: just hit the m key and iTunes will move the item to trash. I have tested this shortcut on the Mac OS X environment, so I am not sure if it works in Windows.

If you know any useful shortcut keys that are off the beaten path, please share them via the comment section. Thank you.

Bash shell scripting: a colon difference

Have you ever wonder what the difference between ${name-“Hai Vu”} and ${name:-“Hai Vu”}? In other word, what is the difference does the colon make? To save you all the suspense: the colon works when a variable is either empty or unset. The construct without colon will only work when the variable is unset. To illustrate my point, I have created an example shell script (var_substitution.sh). Please save it to a file, examine, and run it to see my demo.

As always, please use the comment facility to post your questions, suggestions, or comments. I love to hear from you.
Hai

#!/bin/bash

# Demonstrate the subtle difference between ${var-word} and ${var:-word}

clear

printf “\n=================================\n”
printf “\${varName+word} and \${varName:+word} comparison\n\n”

varName=”Aloha”
echo “varName is not empty: the two constructs behave the same”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

unset varName
echo “varName is unset: they still behave the same way”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

varName=””
echo “varName is empty: this is where the two differs”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

printf “\n=================================\n”
printf “\${varName-word} and \${varName:-word} comparison\n\n”

varName=”Aloha”
echo “varName is not empty: the two constructs behave the same”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

unset varName
echo “varName is unset: they still behave the same way”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

varName=””
echo “varName is empty: this is where the two differs”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

echo ”
=================================
Conclusion
– The colon (:) construct works when a variable is empty AND unset
– The non-colon construct only works when the variable is unset