December 30, 2014

Two fawr the price awf one!


At the moment, I'm quite busy (new job, many interesting projects, responsibilities) and unfortunately I lack time. That's why I can't write a "full featured" article/script (which will be probably sent to /dev/null anyway), despite the fact that my brain is full of ideas. But I still want to "feed" my personal website with new content(s), this way I tell myself it's not dead (yet).

One month ago, I switched from French AZERTY to UK QWERTY (ISO) and it was kinda painful. Not only because it is a totally new layout but also because I tried to forget my old habits. You could ask me why I chose to torture myself, that's a valid question. Well, the answer is pretty simple: AZERTY just sucks, especially when you regularly write code.

I still need/miss some chars when I talk to frenchies, like à or ç. Maybe I should ignore those dudes, but this solution is clearly unreasonable. So, I've decided to create a custom layout for my (lovely) mechanical keyboards, which is a mix of QWERTY + the nameless boring chars.

Enough talked boy, the topic is going to be irrelevant!

In this context, I "reduced" the verbosity of xev(1). Every time you push/release a key, a block of lines is printed. For my needs, it's useless. The ArchLinux wiki proposes me the following command:

xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

grep and sed? Sexy isn't it? Too much processes for me, sorry. I can't handle this. I invoke awk to rescue us. The required line is:

state 0x10, keycode 38 (keysym 0x61, a), same_screen YES,

I extracted 38, 0x61 and a. The remaining output could be dropped. Here the "ultimate" tiny tool:

xev | awk '
	/keysym/ {
		out = $4 $6 $7
		if (out != oldout)
			printf("%-9s%-6s%s\n", $6, $4, $7)
		oldout = out

First we match the keysym pattern. Then (, ) and , are removed. The out variable is defined to avoid printing the same line over and over again. So if you spam the same key, out is equal to oldout and nothing is returned, except the first result.

0xfe03   108   ISO_Level3_Shift
0xffe3   37    Control_L
0x71     24    q
0x38     17    8
0xe9     26    eacute
0x5b     34    bracketleft
0x7a     52    z

There are three columns: keysym, keycode and the symbol name (it can be also found in /usr/include/X11/keysymdef.h). Another thing: my script seems to be faster than the previous one-liner, possibly due to --line-buffered (cf. grep(1)). When I use it, I can notice a tiny delay, after I pressed the key. You could even do the "test" yourself, if you don't trust me.


As the title says it, I will "share" two mini-scripts at once. The second snippet is about LOC. I tend to count lines inside my projects. The persons who talked to me already know that. If an optional functionality adds too many lines, I simply exclude it. But how to count them? A lazy man will install one of the existing tools (sloc or the other Perly things). Luckily, I'm not that lazy.

awk(1) is the perfect candidate to process files. For now, almost all the languages I try to tame have the same comments, starting by # (I don't care about HTML, CSS or PHP). Another regexes can be easily added. Therefore, the result is basic:

BEGIN { x = 0; w = 0; l = 0 }

/^[\t ]*#/ {

/^[\t ]*$/ {

We initialize all the variables in BEGIN. We make sure it defaults to 0 and we will be able to display a value, when nothing is found. If not, the script would return foobar has 28 LOC, comments instead of 0 comments. Going forward, we "match" the pattern(s): x for comments and w for empty lines. With next, we jump to the following line and we treat the conditions.

	if (NR == 0)
		printf("%s is empty!\n", ARGV[1])
	else {
		l = NR-x-w
		printf("%s has %s LOC, %s comment(s), %s blank(s) & %s SLOC\n",
			ARGV[1], NR, x, w, l)

When the entire file is parsed, a report is shown. l is equal to NR minus comments and whitespaces. As you can see LOCSLOC. I really don't know how the other tools count SLOC (and I don't give a fuck). The final conclusion looks like: has 138 LOC, 17 comment(s), 14 blank(s) & 107 SLOC

2nd script is sleeping on git. Those scripts aren't explosive, but thanks to them my life is cooler and less stressful.

Ohh before I forget: I wish you an early happy New Year, whoever you are.

P.S. Glad to see you found the website, after the URL "migration".