A bit of Terminal-fu

Yesterday, my little brother was typing something on a linux bash and suddenly went back to the beginning of the line with a simple Ctrl-a. It blew my mind.

I don’t know why, but it never occurred to me to look for shortcuts in Terminal (and bash in general), other than Ctrl-c for badly behaving processes. I just cursed myself and frantically typed on the left arrow key each time I typed cd instead of cp.

Turns out bash has plenty of them, and here are a few useful ones I’m really glad to know now:
Beginning of the line: Ctrl+a
End of the line: Ctrl+e
Delete the word under the cursor or before: Ctrl+w
Delete all chars before the cursor: Ctrl+u
Delete all chars after the cursor: Ctrl+k

Apparently these shortcuts work in a lot of text fields, everywhere.

You can also move the cursor word-by-word by typing Esc, then f or b. It’s not really practical though, so you’d be better of remapping the following keys in Terminal Settings > Settings > Keyboard:
control cursor left: \033b (\033 is actually Esc)
control cursor right: \033f

Now you can swiftly move word-by-word with Alt+left arrow and Alt+right arrow.

I guess the lesson here is there’s always to learn from your siblings — especially the geeky ones.

Synchronize and backup Address Book contacts with Dropbox

A nice way to synchronize contacts between your Macs without having to buy a MobileMe account is to have them on your Dropbox.

Basically, it boils down to moving the folder containing your Address Book data on your Dropbox, then adding a symbolic link to it so Address Book will know where to find its data.

Let’s go on bash!

Move the folder:

$ mv -v ~/Library/Application\ Support/AddressBook ~/Dropbox/

Add a symbolic link:

$ ln -s ~/Dropbox/AddressBook/ ~/Library/Application\ Support/AddressBook

Done!

Now you’ll only have to add the symbolic link to every Mac you want to synchronize (you may also need to remove the existing AddressBook folder). Another awesome consequence is that you don’t have to worry about losing your contacts if your hard drive crashes: they’re safe in your Dropbox.

A word of advice though: Address Book was not originally meant to be used this way. It would be wise not to edit your Address Book from the two computers are the same time, for example.

So what could I get at McDonalds? (.com)

I’ve been meaning to try and learn Ruby for a few months now, since I’ve discovered _why’s (poignant) guide. As often when it comes to programming (and about everything, really), nothing replaces practice. Combine that with an idea that floated in my head, and here’s what gives:

http://sowhatcouldigetatmcdonalds.com/

The concept

The idea for this website popped up as I was chatting with fellow students; something along the lines of “hey, wouldn’t it be cool if you had a website that’d told you, like, you know, say, for $24 you can have a bunch of big macs, 12 large fries and a sundae? hu? pretty neat, heh?”.

Their answers, mostly “hmm yeah I guess” and related lukewarm comments were not that encouraging of a feedback; so if you don’t think it’s that neat, don’t worry.

But really, as you probably noticed, the concept of the website is pretty simple. I have some money, you have a McDonalds menu: let’s make that work!

NP-completeness

Well it turned out that this problem is actually NP-complete, as in an O(n^errr okay that shit is hard) kind of complexity – XKCD sums it pretty well. To be more formal, it’s a problem that cannot be solved both fast (in polynomial time) and accurately. A long line of people attempted to maximize their bang for the buck at any restaurant (I only remembered the XKCD strip after starting to code, did I unvolontarily plagiarized it?), and it’s called the ‘knapsack problem‘ – or ‘unbounded knapsack problem’, if you really want to shine at parties.

Algorithm

The best (known) ways of solving the knapsack problem involves some heavy and interesting stuff, like dynamic programming, or even what wikipedia calls a “fully polynomial-time approximation scheme” (I didn’t even check that out, search at your own risks). Considering developing that would already take a great deal of time, I opted for a quite simpler approach, to begin with at least: my algorithm tries to stuff random McDonalds items in until there’s not enough money left. Complex algorithms? To-mah-to, to-mae-to.

Prices

Prices at McDonalds aren’t the same everywhere in the US (let alone the world), considering it’s a franchisee system. Being a truly conscientious and precise man, I decided to use the prices from someone’s picture of a McDonalds menu found randomly on Flickr (I’ll give props if I ever find the original source again).

Behind the scenes

Programming langage: Ruby

Not a lot to say here — I wanted to learn Ruby (because all the cool kids use Ruby!) so I did it in Ruby.

Framework: Sinatra

At first I wanted to use Ruby on Rails, but a bit of digging showed that Rails doesn’t really make sense for a project that small. I still plan on learning it, but Sinatra is incredible to quickly design and launch websites.

Templates: Haml & Sass

Haml and Sass are really great templating engines. Following Ruby’s approach of simplicity, minimalism and DRY, they allow you to write darn clean code that will be interpreted in HTML and CSS at runtime by Ruby (using the haml gem).

Hosting: Heroku / OVH

Heroku is hands down the simplest way to have your Sinatra app online. I would gladly repeat their sales pitch, but their website looks cooler than mine (damn pro infographists): http://heroku.com/

I also list OVH here because I registered http://sowhatcouldigetatmcdonalds.com/ through OVH’s registrar. 7€ for a year is perfect for me, and apparently even cheaper than GoDaddy.

Deployment: Git

If it’s on Heroku, it’s on Git.

I wish I used Git more during the development itself (it’s designed for source control after all), but I ended up not using it much before actually pushing the application on Heroku and GitHub.

Show me the code!

Sure! Here it is: http://github.com/Pluies/sowhatcouldigetatmcdonalds

Misc

The XHTML Transitional code generated by Haml is all neat (yay!) and passes the W3C validator (yay!) – same for the CSS (yay!). The CSS code is a little messy in my opinion though because the parens are closed inline, but I guess it’s a matter of taste.

The little bit of Javascript used to highlight the divs and generate the whole “not lovin’ it” part uses jQuery (through Google’s CDN).

I’ll post more about the process of getting used with all the tools in a quite-near future. In the meantime, I’m looking for any comment, criticism or advice on that project. Tell me everything!

Application icons

Just a quick follow-up to this post about changing application icons:

  • Someone actually found this post by Googling for “creepy preview icon osx”. I love it. :D
  • You can find an astonishing number of great icons on DeviantArt. There is actually a whole category for Mac OS X icons, and given DeviantArt usual level of quality, most of these are insanely good.

Hats off to this nifty little customization for nostalgic users by the way. Not an icon stricto sensu, but definitely lovely.

Alfred – it’s like Quicksilver, but better

I recently started to use Alfred, first along Quicksilver, then as a full replacement. For those of you who never used any of these two, they act like Spotlight on steroids: invoke them with a shortcut, type in a few letters, and let the fun begin. Quicksilver lets you do the basics (launch programs, open folders…) and way, way more.

A simple example? You can type a picture name, then use Quicksilver to resize it at your will, then re-invoke Quicksilver to send it to someone in your Address Book. That’s already pretty impressive, but the amazing part is being able to do it all from the keyboard. Command-line gurus (and to be honest, even command-line regular users :) ) know that using the mouse is usually a waste of time.

Quicksilver bezel interface
Quicksilver bezel interface

However, Quicksilver development seemed to have stopped. Blacktree, QS developers (check out Nocturne, it’s awesome!), hasn’t given any sign of life for quite some time. Some development seems to be going on on GitHub, but nothing ground-breaking.

Considering I don’t use most of Quicksilver’s possibilities, and that I felt QS was getting slower and slower (an index-related issue maybe?), I tried Alfred.

Alfred interface
Alfred's interface (hat disabled). Hello minimalism!

Alfred is fast. Blazing fast. It’s especially impressive on a MacBook Air, with its slow-ish processor and sluggish hard drive.

Considering I use it often to launch programs and web searches (you can add you own search engines), the speed improvement is what sold me. Check it out!