Mercurial Templating

It’s possible to customize how the log output looks like using hg.
We can use inline templates as in:


hg log --template "{rev}"

{rev} is called a “keyword” and it gets replaced by the actual value when we do hg log. The list of keywords can be seen here.

Alternatively we can define our style in a separate file, e.g. ~/.hg/mytemplate.style:


changeset = 'changeset: {rev}:{node|short}'

We specify the style using --style=/home/unixname/.hg/mytemplate.style or setting the default style in .hgrc:


[ui]
style=/home/unixname/.hg/mytemplate.style

Other settings

Colors

We can change the font-colors of the text by using ANSI escape codes

Basically we specify \x1B[3<i>m where <i> is a number from 0 to 7 (see the wikipedia page for the color mapping). And \x1B[0m resets the color.

Files in multiple lines

We can add the following line that configures the formatting of a file entry (when using the {files} keyword)


file = ' {file}\n'

Date in my timezone

We can use

{date|localdate|rfc822date}

References

[1] http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html
[2] http://stackoverflow.com/questions/8643612/how-to-make-hg-log-verbose-show-files-on-multiple-lines
[3] http://www.selenic.com/hg/help/templates

2048 CLI in python

I’ve decided to code a simple CLI version of the 2048 game in python:

https://github.com/kunigami/blog-examples/blob/master/2014-05-13-2048-python/2048.py

Some details of the implementation:

* I’ve handled only the “up” move. For the others I do a rotation of either 90, 180 or 270 degrees the matrix representing the board.
* The core of the algorithm is apply “gravity”, perform the merge with adjacent element with same number and then apply the “gravity” again because my merge function can leave empty spaces.

[bash] Move and change directory (mv & cd)

In many cases when I move one or more files to a directory I want to change to that directory afterwards. I did a little search and found this discussion, which resulted in the following bash script function:

function mvcd () 
{ 
     num_of_files=$(($# - 1))
     for i in $(eval echo {1..$num_of_files})
     do
         eval mv \${$i} \${$#}
     done
     eval cd \${$#}
}

Example:

mkdir c
touch a.txt b.txt
mvcd a.txt b.txt c

[Linux] xclip command

Copying

The following command

echo "foo" | xclip -selection clipboard

will copy “foo” to you clipboard. Try pasting it elsewhere. The clipboard selection is the one used by Ctrl-C Ctrl-V commands. The default selection of xclip is the one used when you select text in linux (XA_PRIMARY).

A shorter version would be:

echo "foo" | xclip -sel c

Pasting

You can also send the contents of the current clipboard to the terminal (useful for piping).

xclip -sel c -out

Or

xclip -sel c -o

[Eclipse] Find all occurrences where a variable is written

Problem

Search all occurrences where a given variable is written (but not read) in a workspace, using Eclipse.

Solution

Search for current references (Shortcut: Ctrl + Shift + G) and filter out occurrences by type using the menu illustrated in the image below. In this case, it’d probably suffice to check the ‘Read’ entry.

Reference

[1] http://stackoverflow.com/questions/9333634/find-all-assignments-to-variable

[Eclipse] Finding the opening bracket at a closing one

Problem

The method/loop you’re reading is very long and you after a while you see a lot of closing brakets and it’s difficult to tell to which opening bracket it matches to.

Solution

Place the cursor at the right of the closing bracket and use the following shortcut:

Ctrl + Shift + P

Or (for Mac)

Cmd + Shift + P

PS. In Eclipse 3.8 a new feature is expected to help in this task.

References

[1] http://stackoverflow.com/questions/1910836/show-function-statement-declaration-after-selecting-closing-brace-in-eclipse