Haskell ? : The ternary operator

•April 11, 2008 • 4 Comments


? :: Bool -> (t, t) -> t
a ? (b, c) = if a then b else c

And there you have the Haskell ternary operator. Nice syntax, and lazy evaluation means that only one of b or c is ever evaluated!

Simple Javascript Progress Bar

•April 8, 2008 • 11 Comments

Here’s a quick and obvious-in-hindsight progress bar that I created while working on a project:

<div id="progress" style="border: 1px solid black;">
  <div style="background-color:green; width:50%">&nbsp;</div>
</div>

Now just use Javascript to change the width of the inner div!

Of course there can be any number of variations on this theme. For example, a repeating background-image can give a nice effect.

Or how about a discrete bar instead with say, five stars? Just use five image tags, then if you have these image elements in the array ‘stars’:

progress = Math.round (progress/20); //progress was a percentage
for (i =0; i < progress; i++) stars[i].src = "filled_star.gif"; for (i = progress; i < 5; i++) stars[i].src = "empty_star.gif"; [/sourcecode]

A look at Gobo Linux

•March 26, 2008 • Leave a Comment

I came across an interesting Linux distro today, called GoboLinux. Gobo brings about a radical change in the way your filesystem is organized. Instead of having your programs scattered all over, Gobo organizes the filesystem in a much more sane manner (because lets face it, there may be a method to the madness that is the Linux filesystem, but for most purposes, it’s still madness). In Gobo, doing an  ls /  will give you:

Programs
Users
System
Files
Mount
Depot

You can easily tell whats going to be where. For example the /Programs directory has… well all your Programs! But the best part is, if you had two versions of Foo, they’d be in /Programs/Foo/1.0/ and /Programs/Foo/2.0/. In effect, the filesystem is now your package manager. Thats a really cool way of looking at package management. You install a program into /Programs (Gobo comes with a nice path-agnostic compilation system called “Compile”), and then if you want to uninstall, just delete its directory. Want to know which program a file “belongs” to? That’s natural as well, with this hierarchy.

Of course everything cannot possibly be so simple, and Gobo has an uglier side. First of course, the filesystem-as-package-manager cannot by itself deal with dependencies. Compile handles dependencies, but if you’re going to slap a package manager on top anyway, then you’ve lost some of the elegance you gained with this new filesystem hierarchy.

What’s worse is that Gobo needs to litter all the directories with symlinks to handle shared files, etc. What’s even worse is that Gobo still maintains the the traditional hierarchy alongside the new one! Without it, they would go straight to compatibility hell. So they still have /usr and friends, but these are now symlinks to their Gobo counterparts. They’ve even made a kernel extension (called GoboHide) to hide this ugly underbelly from your eyes (GoboHide is not strictly required, it’s only for hiding the traditional hierarchy).

I can’t help get the feeling that I’ve been sent back to square one :). Of course, that’s not really true, and my gripes are aesthetic more than anything else. Gobo really has done something pretty innovative. But wouldn’t it have been easier to just use fuse to achieve the same goal? Don’t touch the filesystem hierarchy, don’t make a new distro, just make a filesystem with fuse, that acts as a different “view” of all your files.

I admit I haven’t thought too much about it, but it seems possible to extend this to a package manager that would masquerade as a filesystem. You would certainly be able to get the /Programs coolness, and an rm -rf /Programs/Foo/1.0, would uninstall version 1.0 of Foo. You might even be able to manage dependencies within the confines of the filesystem-as-package-manager paradigm. On the other hand, it could just be a hook into an existing package manager, and still provide many of the advantages of Gobo. Food for thought, and perhaps for an interesting project at some point in the future!

As far as Gobo itself goes, I think its great that they’ve done something so innovative, and I wish them luck. Only problem is, a nicer filesystem hierarchy is not by itself a strong enough motivation to switch distros (especially since I’m perfectly happy with my package manager). Maybe as Gobo matures it’ll be worth looking at again.

Mumble your tests!

•February 22, 2008 • 17 Comments

mumbles screenshot
I’ve been working on a rails project, and in some screen cast, I saw autotest+growl and fell in love. I googled a bit for a Linux version of growl, and I found Mumbles. There was no built in autotest support, so I hacked around, extended the Mumbles dbus interface just a little, and then made a simple autotest hook that uses Mumbles.

Until the author commits my patch to his repository, you can get my patched version as a tarball here: Mumbles patched (its a bziped tarball, not a jpeg file – wordpress limitations, so right-click and save as)

And here’s the autotest plugin:

require 'dbus'

def send_message(title, message, icon)
  begin
    bus = DBus::SessionBus.instance
    mumbles_service = bus.service("org.mumblesproject.Mumbles")
    mumbles = mumbles_service.object("/org/mumblesproject/Mumbles")
    mumbles.introspect
    mumbles_iface = mumbles["org.mumblesproject.Mumbles"]
    sig = mumbles_iface.signals["Notify"]
    bus.emit(mumbles_service, mumbles, mumbles_iface, sig, title, message, icon)
  rescue Exception => e
  end
end

Autotest.add_hook :ran_command do |at|
   begin
     output = at.results.last.slice(/(\d+) examples?, (\d+) failures?(, \d+ pending)?/)
     if output =~ /.*[1-9] failure.*/ then
       send_message("FAIL", "#{output}", "fail.png")
     else
       send_message("PASS", "#{output}", "pass.png")
     end
  rescue Exception => e
  end
end

You’ll need the ruby-dbus library which you can get here

*Update:* surrounded the hook with begin rescue. Without this, autotest would crash if I had a syntax error in my spec, because at.result.last would be nil

chwnd!

•February 19, 2008 • Leave a Comment

I did something incredibly stupid.
I wanted to do this:

sudo chown <me> somedir/*

Instead, I did this:

sudo chown <me> /*

Oops! I realised what was happening, and Ctrl+C’d. When I tried to put things back to normal, I realised i couldn’t sudo. sudo gave me some error about /etc/sudoers having the wrong uid. It turns out this was because /etc/sudoers was one of the things that got chowned before I stopped chowning things.

So if you’re ever in such a situation, here’s what you do:
In ubuntu, simply boot into recovery mode, with other distros, you may have to manually log in as root. Then chown your sudoers, and anything else that you messed up and reboot.

What I did was simply this:

chown -R root:root /bin
chown -R root:root /boot
chown -R root:root /etc

Those being the only three dirs affected. However, there may have been files that should belong to me and shouldn’t be chowned to root (there weren’t in this case, but if I hadn’t Ctrl+C’d fast enough, there would have been such files). So really, I should be checking for files with me as owner, but root as the group, and then only chowning those. In which case the following might be a better way (might because its untested)

ls -Rl / | grep "<yourusername>[[:space:]]root"  | awk '{print $8}' | xargs chown -R root:root

Of course this wont work if you have spaces in your filenames. Just change the awk part appropriately (it’ll end up uglier!)

bash fun: alarm clock

•February 8, 2008 • 1 Comment

Over time I’ve become immune to most kinds of alarm clocks. The only thing loud enough to wake me is music coming from my 5.1 speakers. But I’m really particular about what I want to wake up listening to, and it can’t be the same song every day. So, I came up with this thing:

#!/bin/bash
function createAlarm
{
  command="gst-launch-0.10 playbin uri=\"file://"$1"\""
  echo ${2}" "${3}" * * * "${command}|crontab -
}
if [ -z "$1" ]; then
  echo Usage: $0 " "
  exit
fi

if [ -z "$2" ]; then
  min=00
else
  min=$2
fi
if [ -z "$3" ]; then
  hr=07
else
  hr=$3
fi

pattern=$(echo "$1" |sed -r -e 's/\s/*/g' -e 's/.*/*&*/')

IFS=$'\n'
declare -a results
results=($(find -H ~/Music/ -iname "$pattern" -printf "%p\n%f\n"))

if [ ${#results} -eq 0 ]; then
  echo "No results"
  exit;
elif [${#results} = 2]; then
  #dont show menu
  createAlarm ${results[0]} $min $hr
else
  for i in `seq 1 2 ${#results[*]}`
  do
    echo $((($i+1)/2))") "${results[i]}$'\n'
  done
  echo -n "select# "
  read i
  createAlarm ${results[$(($i*2-2))]} $min $hr
fi

It searches through my music based on the keywords I give, then it creates a crontab which uses gstreamer to play the song. Incredibly useful, and much faster than navigating a GUI to find a song, set a time, etc like I used to do before. Try it!

Installing Ubuntu

•January 21, 2008 • Leave a Comment

As I clap my hands,
with the echoes, it begins to dawn–
the summer moon.
-Basho

I finally made the decision to install Ubuntu. I had been running it off VMWare for really long, but I always knew I had to switch eventually. After several long hourse of defragmentation (with 3 different defragmenters!) I finally managed to get the Vista disk manager to shrink my partition enough to get myself a measly 8 Gb of space.

Anyway installation was no problem, what I really wanted to do was restore my system as it used to be on VMWare. Remastersys seemed perfect, but sadly didn’t work for me. The author was really helpful, but given the amount of time I had spent defragging, I was way too impatient to wait for a fix. There are other ways to duplicate your system, but they involve re-downloading all your packages anyway, so I decided to do it manually.

By the time everything was ready it was really late and I was exhausted. Still, I feel an irresistible urge to tweak things. Also I need to launch a hunt for alternatives to some of the Windows only programs I had gotten used to. There goes the rest of the week!