CodingDojo and gentle introduction into Fantom

Last Saturday I was participating in  CodingDojo organized by Szczecin JUG. Our task was to write simple parser that will take a text representation of minefield like “.*..*” (asterisk are mines, dots are empty fields) and transform it into string like this “1*11*”. In other words we was creating heart of the Saper game). It was unusual task because there was only one laptop and one projector, each of participants has 5 minutes to write some code (tests or implementation, of course we use TDD). After few hours of coding and discussions about approaches, data structures and algorithm that would be used we manage to write fully functional parser.

This was really strange experience for me, and to be honest I was little bit scared of … from one point of view this is strange, because I shared lots of code in EGit and other open source project and for sure I’m not scared by showing my code to other developers … but writing it in front of other people, and this filling that they are watching while I’m typing was really strange! But honestly I really like this and can recommend such experiment to everyone!

On my way back I’ve started thinking how same quasi-aplication could be implemented in Fantom. I’m almost sure that you are wondering “what the heck is Fantom”. Fantom is (yet another) JVM language … but it is quite different from others (at least from that I’ve herd about). What is different ? First of all it uses each own byte code; yes, this has some performance penalty, but with such approach its binaries can run on JVM, CLR, browser (yes its compile to JavaScript) and LLVM. Secondly it has build in test framework, build framework, modularity support, actors, truly immutable objects, object-to-string serialization etc. Last but not least, it distinguish nullable and not-nullable objects therefore you have null-contract build in in language syntax, compiler and documentation.  And of course it has support for functional programming and is statically typed … more information you  can find on fantom.org

Now when you know what is Fantom, we can dive into some code samples. My example application would be mentioned above minefield-parser. I would start from tests, and then move to implementation.

As I mentioned before Fantom has build in test framework, just create a file with fan extension, you can call it how you want but by convention file name is same as class name that would be inside (but this isn’t a hard rule). Yours test class need to extend Test (to be more specific sys::Test, sys is name of pod (pods are more or less equivalent with jars)).

class SaperTest : Test {

  Void testEmptyInput() {
    // given
    input := ""

    // when
    result := Parser().parse(input)

    // then
    verifyEq(result, "")
  }

Test methods must return Void type (notice uppercased notation) and name must start from ‘test’ (same as in JUnit3). To assert use verifyXY methods, simple as that. Below you will see full test suite for my Parser.parse method, you don’t need to scan/read all of them just look on first two.

  Void testTrowErrWhenIllegalCharactedIsPresent() {
    // given
    input := ".a"

    // when
    verifyErr(ArgErr#) {
      Parser().parse(input)
    }
  }

As you can notice,  first test has a strange notation in ‘then’ section, there is verifyErr(ArgErr#) { Parser().parse(input) }, what it means ? This is an assertion method that check does given line throws and ArgErr (with is an argument exception). ArgErr# is more or less equivalent to ArgErr.class (or IllegalArgumentException.class) in java. Second parameter for verifyErr method (yes, statement in curly brackets is an parameter) is actually code that should throw given exception. This actually example of passing function as a parameter and when it last (or single parameter) it can be passed as a block statement after method non-functional parameters.

Another thing that you can notice is that there is no new keyword or equivalent. Fantom constructors are some how similar to factory methods, you just mark them with ‘new‘ keyword in class definition fallowed with unique name (make is default one) and you have yours constructor.

Same as in other modern JVM languages default access level to method/constructor/class is ‘public’ and you don’t need to type it explicitly, also semicolons are not required at end of the line. Another commonly used feature in new JVM language is type inference, but comparing to Scala’s type inference, one used in Fantom is really simple, it can only infer variable types, you must explicitly declare return type and parameters types.

Rest of test test code is pretty much the same, so lets move to more interesting part … the implementation! Method parse should be understandable for every one since it is only calling private methods.

class Parser {

  Str parse(Str input) {
    lines := input.splitLines
    validateInput(lines)
    result := createMatrix(lines.size, lines[0].size)

    lines.each |line, i| {
      parseLine(line, result, i);
    }

    return convertToStr(result)
  }

Only code in line 8 and 9 could be little bit strange. So lines variable is an List of Str‘s (yes it is shorten notation of String), and we are calling each method on this array (when method takes no arguments or one function argument brackets can be skip); each method takes function with two parameters first is value of element, second is it position. Notation |value, i| { parseLine(…) } is declaration of function that takes two parameters with name value and i , types of those parameters are infer from each method parameter signature. So, this simple two line notation calls parseLine method on each input line, it is that simple! Lets move with rest of the code!

  private Void validateInput(Str[] lines) {
    firstLineSize := lines[0].size
    if (!lines.all |line| {
      line.size == firstLineSize && line.all |c| { c == '.' || c == '*' }
    })
      throw ArgErr("Line size mismatch or illegal character was used")
  }

Another strange notation can be found on line 17 and 18. In both lines we are calling all method that is defined on Str and List classes. In both cases it checks does all elements matches given boolean condition. This “simple” if statement checks does each line have same number of elements and don’t contain other chars then dots and asterisk. Yet another hint, if yours function/closure/method has only one statement you don’t need to use return keyword.

  private Int[][] createMatrix(Int row, Int cols) {
    Int[][] result := [,]
    row.times {
      result.add([,].fill(0, cols))
    }

    return result
  }

Next strange notation you can find in line 24. What the heck is that [,] ? This is sort cut for creating empty List. As you can notice type inference in Fantom works both ways, now it figures out that this empty list should be type of List[][].

In Fantom you don’t need to use for loops to repeat some code X times. There is times method on Int type, it takes function as a argument and it will call this function X times. Example of using this method can be found in line 25.

In line 26 you  can find example usage of fill method defined on List class. An empty list is created and then filled with zeros. So method createMattrix will return matrix with given size filled in with zeros. Simple as that .. and it only has 4 lines of code!

  private Void parseLine(Str input, Int[][] result, Int i) {
    input.each |c, j| {
      if (c == '*') {
        prevCol := j - 1
        nextCol := j + 1
        result[i][j] = -10
        if (j > 0)
          result[i][prevCol]++
        if (j < input.size - 1)
          result[i][nextCol]++
        range := prevCol.max(0)..nextCol.min(result[i].size - 1)
        if (i > 0)
          incrementInRange(result[i - 1], range)
        if (i < result.size -1)
          incrementInRange(result[i + 1], range)
      }
    }
  }

In line 33 we meet once again each method, I hope that you already know what it does 😉

Another interesting thing can be found in line 42; double dot operator. This is a code sugar for creating Range objects (it will be used in line 52), they are simplifying iterating over only range of elements. You just pass bottom boundary double-dots upper boundary and Fantom generates everything in between. There is also ‘..<‘ operator  for exclusive range.

  private Void incrementInRange(Int[] row, Range range) {
    row.eachRange(range) |value, p| {
      row[p]++
    }
  }

eachRange, yet another useful and interesting method in List class! I’m using it in line 52 to increment values in neighbourhood of filed with mine. Here I’m only interested in position of element (the p variable), but because position is second parameter of function I also need declare variable that will hold element value.

This is pretty much it, now rest of code in this gist should be understandable for you, if not (or you have further questions) leave me comment under this post.

Automatic Screen Detection And Configuration Under Awesome WM

Recently I’m mainly interested in automating my day-to-day tasks on Linux laptop. I’ve started with automating back-up process, now come time to automate external screen detection (and configuration).

For couple of months I was struggling with ZSH history to find XRandR commands that enables and disables external screen. During that time I was almost sure that there is another way of handling this task … since eg. Gnome was doing it automatically. First idea was to find part of Gnome that is responsible for this feature and simply just integrate it with Aersome WM. But my from-time-to-time raw searches didn’t snow any interesting resources. Therefore I’ve decided that I must done it by my self … this is how screenful project was born.

What is screenful ?

It is simple extension to Awesome WM that allows you (with little knowledge of Lua and XRandR params) automatically configure connected screen. It integrates udev drm/change event with Awesome and XRandR. First of all there is udev rule that when drm/change event appears inform screenful about output change in particular card. Then screenful will “calculate” which card output was connected/disconnected, computes screen id based on EDID and finally run configured action. When configuration file doesn’t have specified configuration for this id then screenles will append commented configuration for it and run default action.

What is unique in screenful ?

Same as Awesome WM config file, the screenful config file is a Lua script! Therefore you can do anything you can imagine when external monitor is connected/disconnected. For example you can switch mplayer default audio output when you HD TV set is connected using HDMI cable then change it back when you disconnecting HDMI cable. Also you can reorganize yours tags and windows using Awesome Lua API … and more … Honestly I doubt that you can achieve similar behaviour in any different Window Manager 😉

Project is hosted on github if you encounter any issues or have ideas about new functionalities in screenful feel free to fork me, fill a bug or mail me 😉

Automatic backup in Linux

Some time ago I’ve started backing up my laptop hard drive in any case of failure. Until today I was simply connecting external back-up-drive and manually launching mount && rsync command from zsh history. But this requires that always after connecting this special device I need to go to terminal, log in as root then find this back up command and run it.

Recently I was wondering how this process could be improved or even done automatically. I had few requirements:

  • it should be fully automated
  • system should inform me that back up process was started and that it ends
  • this notification should be done as some system pop up in X windows

So I’ve come with a solution that connects udev, DBus notification and awesome wm naughty library … and it is really simple 😉

How it works ? When you connects back-up-device udev will pick up this event, create a symbolic link to /dev/backup, then launch backup script. This script will send a notification through DBus, this event will be received by naughty and displayed as a simple notification in Awesome WM. After sending notification signal the backup script will mount given device and launch rsync command. After successful synchronization backup device will be unmonted and another notification signal will be send to inform you that you can now disconnect it.

How to achieve this ?

First of all we need to figure out UUID of our back up device (I assume that /dev/sdb2 is yours back-up-partition) :

# blkid /dev/sdb2
/dev/sdb2: UUID="97377fed-edaf-407b-9e02-5c6cecd6dceb" SEC_TYPE="ext2" TYPE="ext3"

Then we need to create new udev rule in /etc/udev/rules.d we can call it 99-backup.rules:

ENV{ID_FS_UUID}=="97377fed-edaf-407b-9e02-5c6cecd6dceb", SYMLINK+="backup"
ACTION=="add", ENV{ID_FS_UUID}=="97377fed-edaf-407b-9e02-5c6cecd6dceb", RUN+="backup"

Of course you need to replace my UUID with one you get from blkid command. Those two lines are responsible for creating symbolic link called backup in /dev directory to device with given UUID; second line will launch backup script when device with given UUID is added.

Finally we need to create our backup script and put it into /lib/udev directory. Here is content of this script:

#!/bin/sh
 
### CONFIGURATION ###
USER="lock"
MSG_TIMEOUT=-1
 
### DO NOT MODIFY ###
function show_notification() {
	su $USER -c "export DISPLAY=':0.0'; \
		export XAUTHORITY='/home/$USER/.Xauthority'; \
		/usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications \
			/org/freedesktop/Notifications  org.freedesktop.Notifications.Notify string:'' \
			uint32:0 string:'' string:'$1' string:'' array:string:'' array:string:'' int32:$MSG_TIMEOUT"
}
 
function backup() {
	show_notification "Backup process started! Do NOT disconnect backup device."
	mount /dev/backup /mnt/backup
	ionice -c 3 rsync -az --delete --exclude=/dev --exclude=/mnt --exclude=/media --exclude=/run --exclude=/proc --exclude=/tmp --exclude=/sys --exclude=/var/log --exclude=/var/run / /mnt/backup
	umount /mnt/backup
	show_notification "Backup process successed! You can disconnect backup device."
}
 
backup &

In this file you only need to change USER variable to yours UNIX user name, also you can adjust MSG_TIMEOUT to your needs (-1 means that default value will be taken). Most difficult part in this script was to figure out how to send DBus notification from root to regular user, since all window managers create and listen on theirs own session scoped buses. As you can notice we can force dbus-send command to send signal to proper bus instance by executing this command on behalf of  given user and with exported DISPLAY and XAUTHORITY variables.

Full source code is available on github. Have a nice and pleasant back up’s 😉

EGit Iteractive Adding

So what is “interactive adding” in git? Imagine situation when you change two or three lines in file (this could be also two or three blocks/methods), then you realize that you will want to commit those changes in two (three or more) separate commits.

Here is Al Blue’s description how interactive adding works in native git: Git Tip of the Week: Interactive Adding

What about EGit? Honestly this is currently supported … but it isn’t really straight forward to access. You need to select a file then choose from context menu Team > Compare With > Git Index and then you can easily move changes around, finally when you are happy with both versions (left is working tree and right is one that will be staged/added to index) just save compare editor contents. Version from right hand side will be added to index as it is in this editor.

OK, so now you can easily stage and unstage partial changes … maybe you would also like to partial replace staged changes by version in HEAD? Currently in version 1.1 you can compare version from git index with HEAD version using context menu Team > Compare With > Git Index with HEAD, but you cannot overwrite anything … yet, because (hopefully) in 1.2 such option will be available. Here is patch that adds this functionality (to be honest this is a side effect…).

And what about (my favorite) Synchronize View? Unfortunately EGit 1.1 don’t allow to move any changes in compare editors launched from Synchronize View … but with change mentioned above and with this one both features would be available from Git Commits model (before it was named as Git Change Set model)

Here is a short screen cast that shows how this work:

 

End of Google Summer of Code 2011

Officially coding period in Google Summer of Code was closed last Monday (August 22nd) , so this is good time to sum up last three months of my work on EGit project.

During that time I’ve manage to contribute 32 commits (29 of them are already merged into master, rest is pending for review). I have also two not finished changes in my local git repository. First is about supporting non-workspace files in Workspace presentation model in sync-view, second is refactoring of current Git Change Set implementation.

Here is detailed list of things that was done during GSoC and are available in current nightly build:

  • improved synchronize wizard
  • Workspace presentation model will refresh after repository change
  • pushing from sync view to multiple repositories is now possible
  • Git Change Set presentation model will be refreshed after workspace change
  • context menu in synchronize view was cleaned up and git actions are shown for multiple selections
  • synchronization on folder level is now possible and it will narrow results just for selected folder (when synchronization is launched for project then results will show changes in whole repository)
  • local changes can be drag and dropped between <working tree> and <staged changes> nodes in Git Change Set presentation model

Performance improvements for Workspace presentation model are still awaiting for review in Gerrit. Some part of performance and memory usage improvements for Git Change Set are also pending in Gerri, I’m currently working on rest of required refactoring in Git Change Set presentation model.

I want to thank my mentor Matthias Sohn for his commitment in GSoC, for reviewing my patches and his feedback. It was a great pleasure working  with him! 😉