Friday, February 1, 2008

Rhino with readline - Hurray for rlwrap

Stumbled across dr.bob's sweet post on getting a readline wrapper around rhino.

Prior to reading his post, I had no idea rlwrap even existed, I'm simply amazed. GNU readline is IMO one of the most useful abstractions in all of *nix-dom. If I had known that I could use it even with tools that weren't readline-enabled, my previous lives would have been much easier. For example, looks like some foo-equipped DBAs (inspired by one of my heroes Tom Kyte) have found a way to use rlwrap to get a command history for sqlplus. It's been ages since I've actually used sqlplus (most time these days is in psql thankfully which natively supports readline), but this would be step 0 in my Oracle client install.

Anyway, back to Rhino. rlwrap + rhino is lethal. Gives me vi key bindings in rhino.

My setup ended up like so:

* Installed Rhino to ~/java/rhino
* Created script in ~/bin which was already on the path
* chmodded said script
* Added vi editing mode to ~/.inputrc
* Edited rhino commands with vi bindings
* Lived the good life

The various files looked like so:

~/.inputrc
set editing-mode vi

~/bin/rhino
#!/bin/bash
for jar in `find ~/java -type f -iname '*.jar'`; do
export CLASSPATH=$CLASSPATH:$jar;
done
rlwrap /opt/java/current/bin/java -cp $CLASSPATH org.mozilla.javascript.tools.shell.Main -strict "$@"


Drop an exploded rhino anywhere into ~/java, and you're good to go.

Of course, GNU Readline supports hundreds of options including other bindings like emacs if your are so inclined.

No comments: