Mind Like Water

Cool things about code.

My Font for Gmail

Fri 26 August 2011 by Lance Jian in Tool.

I have lots of plain text emails in Gmail. The format of those emails is always a problem: code and other nicely formated text are not aligned well because Gmail uses "arial,sans-serif" as the default font. I couldn't find a good solution to change it to a monospaced font, so I decided to write my own Google Chrome extension: My Font for Gmail.

This extension changes the default font of Gmail to any font you like. To use it:

  1. Click the "wrench" icon on upper-right corner of Chrome,
  2. Select tools->Extensions->My Font for Gmail™->options.
  3. Enter any font in your system.
  4. Click "Save".
  5. Refresh the Gmail page or re-open it.

Please note that this extension works best for plain-text emails. Rich-text(HTML) emails will possibly remain its original font because those emails can set their own font for each element. Usually this doesn't bother me since they are already well-formatted.

Source code is published at my git repository under MIT license.


Install Faenza in Ubuntu

Tue 19 April 2011 by Lance Jian in Linux.

Faenza Icons is a very beautiful Gnome icon theme provided by Tiheum.

Faenza Icons

It's very easy to install this theme in Ubuntu. Just run the following commands:

sudo add-apt-repository ppa:tiheum/equinox
sudo aptitude update
sudo aptitude install faenza-icon-theme

Next, go to System->Appearance->Customize...->Icons, choose "Faenza".


Get the Absolute Path of the Running Bash Script

Wed 13 April 2011 by Lance Jian in Linux.

Sometimes you want to run a bash script from any directory, especially when it is placed or linked in a directory in the $PATH environment and depends on other files in the same folder. The problem is: If you use a relative path for the other file, it will try to find the file in the current working directory. If you use an absolute path, you cannot move it later. So it should know where itself is running in order to get the correct file path.

For the impatient, here is a script example that can run from anywhere to print the content of file "help.txt" under the script's directory:

relative_path=`dirname $0`
absolute_path=`cd $relative_path;pwd`
cat "$absolute_path/help.txt"

First, it gets the relative directory. There are two situations:

  1. If the script is supposed to be executed directly like ./script.sh, use: relative_path=`dirname $0`

  2. If the script is supposed to be "sourced" like source script.sh, use: relative_path=`dirname $BASH_SOURCE`

Once it has the relative path, it's easy to get the absolute path:

absolute_path=`cd $relative_path;pwd`

This method is compatible with both Linux and Mac OS. However, it's not perfect. It won't work if you have changed the cd command to something else using alias or function. Although it does not usually happen, in this case, you can use the other way:

absolute_path=`readlink -m $relative_path`

This command only works in Linux, so you have to pick the way suits you best. Now $absolute_path is the absolute path of the file being executed, so you can use it to get other files which will be correct no matter the current working directory is.


Clone a Ubuntu Virtual Machine Image

Sat 09 April 2011 by Lance Jian in Linux.

I recently created a Ubuntu server virtual machine image in kvm and wanted to clone it so that I don't need to install everything from scratch again. It turned out this is fairly easy.

This works for any virtual machine program including Vmware, VirtualBox, etc.

First, copy the image file and create a new virtual machine from it. Next, start and log in the "new" machine.

Now, remove 70-persistent-net.rules, otherwise the network will not work:

sudo rm /etc/udev/rules.d/70-persistent-net.rules

This file will be re-generated after a reboot.

Next, change the hostname in /etc/hostname and /etc/hosts according to your preference.

Finally, reboot and enjoy using the new virtual machine.

See also: Cloning Ubuntu 10.04 Server KVM guests efficiently


PIL IOError: decoder jpeg not available

Mon 04 April 2011 by Lance Jian in Python.

Today I got this error when I try to resize a JPEG image in python using the Python Imaging Library (PIL):

File "/usr/local/lib/python2.6/dist-packages/PIL/Image.py", line 1290, in resize
   self.load()
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line 189, in load
   d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/usr/local/lib/python2.6/dist-packages/PIL/Image.py", line 385, in _getdecoder
   raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

This is because at the time PIL was being compiled, it did not find the JPEG library. To fix this problem, run:

sudo aptitude install libjpeg-dev

Then you need to uninstall and reinstall the PIL package:

sudo rm -rf /usr/local/lib/python2.6/dist-packages/PIL
sudo easy_install pil

Or if you are using pip:

sudo pip install -I pil

In the output messages, you can find the PIL setup summary like following:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
...
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
...
--------------------------------------------------------------------

This shows that now PIL has JPEG support. All the commands here works under Ubuntu 10.04.