Play the System Bell as shell commands end in Ubuntu/Debian

This is a nice little trick I started using after switching to Gnome3, as I don’t have the application switcher panel any longer: before, I could put my mouse cursor over that to see a preview of shell windows where a command is running.

Now I should keep moving the cursor to the top-left corner and wait for the animation to show all open windows, which is tedious when you don’t have a precise idea of how long will commands take.

So, the long hated system bell sound comes to rescue!

Just append ; echo -e "\a" after the command you want to run and… voilà, you’re in the event-driven world! 😛

Example:

wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.13.tar.bz2; echo -e "\a"

This command of course won’t do anything if your volume is set to 0 or you explicitly disabled system audio notifications.

Advertisements

Syntax highlighted code in Gmail with Vim

I used to paste code in emails by simply choosing the monospaced font in gmail’s web client (I don’t like native clients… well, except Android’s), in hope that the (usually) small width of the paragraph didn’t mess up with the code.

Whenever I had the need to paste snippets larger than, say, 4-5 LOC I always ended up using some external website like pastebin; this however resembled attaching files to the email too much, in that you can’t really talk about snippets in the email without having your addressees jump back and forth between tabs, like they would between windows in the attached-file case. In those cases I relied on commenting the code on pastebin, so my emails were actually cut in two (or three, or four,… depending on the number of snippets in the email).

My new process goes like this: I write the code in the editor (Eclipse or Geany), so I have autocompletion, autoformatting and tabs converted into spaces. Then, I copy and paste the code into a new file in Vim and save it as an HTML file using the

:TOhtml

command as I learned reading this article. I open the file with Chromium (any browser will do, of course) and I copy and paste the code right into Gmail’s editor.
This way Gmail nicely inserts the HTML code inside the email, using the styles I chose in Vim, background color included!

You don’t like Vim’s color scheme? You’ll find a lot of color schemes in this wonderful website 🙂

Some simple utility methods I use a lot in Java

Whenever I start a new project in Java I always find myself in need of some basic utility methods that are lacking in the standard library. I suppose that every programmer has his own little “bag of tricks” for every language (well, maybe not for python.. ❤ ).. and this is mine!

These are only some of the methods I use the most, of course, those that may be useful for many people 🙂

/*
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details.
 */

import java.util.List;
import java.util.Set;

/**
 * Contains only a bunch of <code>static</code> utility methods.
 *
 * @author mb - somethingididnotknow.wordpress.com
 */
public final class Utilities {

    /**
     * Checks whether <strong>all</strong> the provided objects are
     * <code>null</code>.
     *
     * @param objects
     *            a number of objects of any kind that are to be checked
     *            against <code>null</code>
     * @return <code>true</code> in case <strong>all</strong> the argument
     *         objects are <code>null</code>, <code>false</code> otherwise.
     */
    public static boolean areAllNull(Object... objects) {
        for (Object o : objects) {
            if (o != null)
                return false;
        }
        return true;
    }

    /**
     * Checks whether <strong>any</strong> of the argument objects is
     * <code>null</code>.
     *
     * @param objects
     *            a number of objects of any kind that are to be checked
     *            against <code>null</code>.
     * @return <code>true</code> if at least one of the arguments is
     *         <code>null</code>.
     */
    public static boolean isAnyNull(Object... objects) {
        for (Object o : objects) {
            if (o == null)
                return true;
        }
        return false;
    }

    /**
     * Checks whether the two arguments are equal using a <em>null-safe</em>
     * comparison.
     *
     * In case only one of the two objects is <code>null</code>,
     * <code>false</code> is returned. In case both are not <code>null</code>
     * {@link Object#equals(Object)} is called on the first object using the
     * second as argument.
     *
     * @param first
     *            the first object to be checked.
     * @param second
     *            the second object to be checked.
     * @return <code>true</code> in case {@link Object#equals(Object)} returns
     *         <code>true</code> or the objects are both <code>null</code>,
     *         <code>false</code> otherwise.
     */
    public static boolean nsEquals(Object first, Object second) {
        if (first == null)
            return second == null;
        if (second == null)
            return false;
        return first.equals(second);
    }

    /**
     * Returns a String that is empty in case the argument <tt>string</tt> is
     * <code>null</code>, the unmodified <tt>string</tt> otherwise.
     *
     * @param string
     *            the string to be checked against <code>null</code>
     * @return the empty String if <tt>string</tt> is <code>null</code>, the
     *         argument <tt>string</tt> unmodified otherwise
     */
    public static String nonNull(final String string) {
        return string == null ? "" : string;
    }

    /**
     * An equivalent of Python's <code>str.join()</code> function on lists: it
     * returns a String which is the concatenation of the strings in the
     * argument array. The separator between elements is the argument
     * <tt>toJoin</tt> string. The separator is only inserted between
     * elements: there's no separator before the first element or after the
     * last.
     *
     * @param toJoin
     *            the separator, if <code>null</code> the empty String is used
     * @param list
     *            a list of <code>Object</code>s on which
     *            {@link Object#toString()} will be called
     * @return the concatenation of String representations of the objects in
     *         the list
     */
    public static String join(String toJoin, Object[] list) {
        if (list == null || list.length == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        String delimiter = nonNull(toJoin);
        int i = 0;
        for (; i < (list.length - 1); i++) {
            if (list[i] != null)
                builder.append(list[i]);
            builder.append(delimiter);
        }
        builder.append(list[i]);
        return builder.toString();
    }

    /**
     * An equivalent of Python's <code>str.join()</code> function on lists: it
     * returns a String which is the concatenation of the strings in the
     * argument list. The separator between elements is the string providing
     * this method. The separator is only inserted between elements: there's
     * no separator before the first element or after the last.
     *
     * @param toJoin
     *            the separator, if <code>null</code> the empty String is used
     * @param list
     *            a list of <code>Object</code>s on which
     *            {@link Object#toString()} will be called
     * @return the concatenation of String representations of the objects in
     *         the list
     */
    public static String join(String toJoin, List list) {
        if (list == null || list.isEmpty())
            return "";
        StringBuilder builder = new StringBuilder();
        String delimiter = nonNull(toJoin);
        int i = 0;
        for (; i < list.size() - 1; i++) {
            if (list.get(i) != null)
                builder.append(list.get(i));
            builder.append(delimiter);
        }
        builder.append(list.get(i));
        return builder.toString();
    }

    /**
     * An equivalent of Python's <code>str.join()</code> function on lists: it
     * returns a String which is the concatenation of the strings in the
     * argument list. The separator between elements is the string providing
     * this method. The separator is only inserted between elements: there's
     * no separator before the first element or after the last.
     *
     * @param toJoin
     *            the separator, if <code>null</code> the empty String is used
     * @param set
     *            a set of <code>Object</code>s on which
     *            {@link Object#toString()} will be called
     * @return the concatenation of String representations of the objects in
     *         the set
     */
    public static String join(String toJoin, Set set) {
        return join(toJoin, set.toArray());
    }
    /**
     * Checks whether the argument <tt>array</tt> contains at least a
     * <code>null</code> value.
     *
     * @param array
     *            the array to be checked.
     * @return <code>true</code> in case <em>at least</em> one of the values
     *         stored in the argument <tt>array</tt> is <code>null</code>, or
     *         in case the <tt>array</tt> itself is <code>null</code>.
     */
    public static boolean containsNull(Object[] array) {
        if (array == null)
            return true;
        for (Object o : array) {
            if (o == null)
                return true;
        }
        return false;
    }

    /**
     * Checks whether the argument <tt>list</tt> contains at least a
     * <code>null</code> value.
     *
     * @param list
     *            the list to be checked
     * @return <code>true</code> in case <em>at least</em> one of the values
     *         stored in the argument <tt>array</tt> is <code>null</code>, or
     *         in case the <tt>list</tt> itself is <code>null</code>
     */
    public static boolean containsNull(List list) {
        if (list == null)
            return true;
        for (Object o : list) {
            if (o == null)
                return true;
        }
        return false;
    }

    /**
     * Checks whether the argument <tt>string</tt> is <code>null</code> or
     * empty. Please note that the <tt>string</tt> is
     * <strong>trimmed</strong>, so that a check on a string containing
     * white spaces only will always return <code>true</code>.
     *
     * @param string
     *            the string to be checked
     * @return <code>true</code> in case the argument <tt>string</tt> is
     *         <code>null</code>, empty ({@link String#length()} returns 0) or
     *         contains only white spaces (
     *         <tt>{@link String#trim()}.length()</tt> returns 0)
     */
    public static boolean isNullOrEmpty(String string) {
        return string == null || string.trim().length() == 0;
    }
}

I don’t know what’s wrong with the syntax highlighter… here’s the same class in pastebin!

Change color scheme in Geany

After trying Sublime for a while, and quite liking it, I found myself in the middle of a deep customization of the editor… to make it work like Geany!

For quick editing of local bash/python scripts or configuration files there’s no editor that meets my taste better (I said local cause when dealing with remote files my all-time favorite is vim).

The one thing that I like more in Sublime than in Geany is its look: it’s very elegant, but in the end what I really missed in Geany was a dark editor theme.

I discovered this project on GitHub with several available themes, very easy to install (as in execute-install-script-with-no-options) but.. not that easy to choose from Geany’s interface!

[Update – Mar13] – unjordi posted a nice command line one-liner to get and install the geany-dark color schemes, here it is:

wget -qO- http://geany-dark-scheme.googlecode.com/files/geany_dark_filedefs_20100304_190847.tar.bz2 | tar jxv -C ~/.config/geany/filedefs/

After you’ve downloaded the color schemes and before editing the configuration file as described here below try to restart Geany and check if there’s an entry under View/Editor>Color Schemes>; if it’s there you can choose among all installed color schemes from a nice list! 🙂

If you’re out of luck (no list for you) you must edit Geany’s configuration file ~/.config/geany/geany.conf and find the color_scheme line. You must specify the whole file name of the color scheme you wish to use, without its path (it must be in the ~/.config/geany/colorschemes folder anyway).

So, to set your theme to tango-dark you shall have this line in your geany.conf file:

color_scheme=tango-dark.conf

Restart Geany and there you have your nice dark theme 🙂

[Update – Nov13] – a reader had troubles with the configuration file (always reverting to its original state, or not being read correctly by Geany), scroll down to November 5 2013 in the comments if you have the same issues!