Pages

Groovier date diff

HackerNews has weird habit of displaying date of the older comment in days, which sometimes is annoying especially when you see something like "1320 days ago | comments".

Worry not, Groovy to the rescue! On terminal, just write: 

$ groovy -e "println new Date() - 1320"


Which nicely tells you this (well depends on when you execute the commad :-)

Thu Jul 22 00:04:53 IST 2010

That said, it is my first instinct to use Groovy but you could do the same with date command as well

$ date -v-1320d

While we are on Date and Groovy, that just reminded me of a question that I answered on StackOverflow just a few days back. Iterating over a date rage with a week at a time :

(startDate..endDate).step(7) { println it }

Glide your apps on Google App Engine

I had been working on a script, which has turned into project Glide, to help me work with smallish gaelyk apps (like my homepage etc.).

While gaelyk template project provides excellent start; for simple website'ish projects, java webapp structure and the xml noise is sometimes a hindrance for non java background (web) developers.

What glide does is cut that noise off with simple defaults (while being still configurable). The only files your project is required to have is your web content (something like static html or php webapps). Plus there are some more goodies like preconfigured sitemesh etc.

I have created 4 minimal examples to give an idea of what I mean here. Each dir in the samples is a glide project and can be run locally and deployed to GAE.

Start with the `news` project a simple one filer to get sense of what I mean by no config noise. `expense` and `blog` take few steps ahead but are still very very small.

To run a glide project, just cd into the app directory and fire the 'glide' command.

Steps to install glide are mentioned in the README. It should be fairly straight forward if you have java/git/gae already installed. (if you dont use git, you can directly download the zip and extract it)

I look forward to feedback on the project.

PS :
  • Motivated to let it know to the world  by "F**k It, Ship It"
  • Content almost identical to what I posted on Gaelyk User Group

Java 7 and Groovy on Mac OS X Mountain Lion

I usually keep all my softwares and SDKs to the latest version but jdk7 was probably amongst the longest resisted update/upgrade. Mostly running groovy, I never got motivated enough to upgrade and then there have been several security and compatibility issues reported by many.

Anyways, today I just thought of doing it, primarily to reap benefits of invokedynamic, the thing I have not even looked at so far. The download was pretty simple in form of a .dmg and then the .pkg installer.

I fired up terminal with to check java and viola:
[~]
kunal@kaydee $ java -version
java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

The second natural thing was to just verify groovy, but what the heck, the JVM version I see is:
[~] 
kunal@kaydee $ groovy -version
Groovy Version: 2.0.6 JVM: 1.6.0_37 Vendor: Apple Inc. OS: Mac OS X

Apparently Java 7 got installed at
/Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home/

But the one available to groovy was something else:
[~]
kunal@kaydee $ groovysh
Groovy Shell (2.0.6, JVM: 1.6.0_37)
...
groovy:000> System.properties["java.home"]
===> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Setting JAVA_HOME Environment variable comes to rescue (in .zshrc or .bash_profile or whatever profile file you use.)
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home"

Reload the iTerm and

[~]
kunal@kaydee $ groovy -version
Groovy Version: 2.0.6 JVM: 1.7.0_11 Vendor: Oracle Corporation OS: Mac OS X


My ZSH theme

I was initially hesitant to make the move from bash to ZSH. But thanks to oh-my-zsh now I am just loving it. To spice up the terminal prompt it has a provision of themes (unlike PS1 environment variable in bash).

I looked around for themes and there are many great themes available in the oh-my-zsh repo. But I still wanted to customize the prompt a bit so I went ahead and created my own them called kaydee which looks something like this


It starts with a sweet (but unnecessary) apple, followed by current directory path and then git status.
Then the prompt comes on a new line preceded by username and hostname.


It leaves a blank line between last command's output and the prompt and the colors make this line stands out on black background screen and all user typed commands start at same vertical column (irrespective of what directory you are in), so that while scrolling you can keep eyes fixed and locate commands easily.


You can fork/clone my repo or just copy this theme to spice up your terminal.



Reverse scroll mouse and natural scroll Trackpad on Mac

I frequently connect an external Monitor, a mac keyboard and a (non-mac) mouse to my MacBook Pro while code pairing. I love the natural scroll direction on trackpad but not so for the mouse scroll wheel. I happy to live with regular direction for scroll for a mouse.

Unfortunately, Mac OS X (Lion, Mountain Lion) does not allow such setting out of the box. There is a nice utility app called Scroll Reverser, just a few KBs in size, which can do it for you.



With the config shown in the screenshot above, you can set natural scroll for Trackpad and the reverse scroll (which we are used to) for mouse.

Reading RSS Feed in Groovy

The more I explore, the more I love groovy. Last week while I was demonstrating a few colleagues of mine, that how simple things are really dead-simple in groovy, I wrote this small code that fetches the rss feed and prints the headlines. Just thought of sharing with all.
// setup proxy, if any
System.properties.putAll(["http.proxyHost":"your.proxy", "http.proxyPort":"8080"])             

// define the url, change it to whatever rss url you like
def url = "http://news.google.com/news?ned=us&topic=h&output=rss"

def rss = new XmlSlurper().parse(url) 

println rss.channel.title
rss.channel.item.each {
    println "- ${it.title}"
}
That's it! You have headlines in your output. How can you not love the brevity yet expressiveness of this code:)

ASCII Art Editor in Java

I am Huge fan of README files. When looking at any new library/software, I would at least skim through a README once, if not gulp it thoroughly. So I was writing a README for my project yesterday. I wanted to have a simple diagram in the readme, but it was getting sorta complex to do that with Notepad++, Netbeans or Eclipse. I have always wondered how people make nice ASCII Art Logos (I found that 'it' is called so after some google searches) and diagrams in Text Files.

Anyways, so I found out this cool ASCII Art Editor in java called JavE. And it really helped me to create the desired diagram in the text file. Bonus: I came to know how to create ASCII Banners as well. You have to download optional FIGlet fonts (available on the same website) to create text banners like this one.
 
   _   _      _ _        __        __         _     _
  | | | | ___| | | ___   \ \      / /__  _ __| | __| |
  | |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` |
  |  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |
  |_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_|