Chip's Tips for Developers

Contains coding, but not narcotic.

Jab: a minimal chat client of, by, and for Ruby

June 28th, 2010 9:36:01 am pst by Sterling Camden

I don’t chat a lot. It’s too disruptive to use for most communications. But I do have a few friends and colleagues with whom I enjoy the occasional real-time text conversation. In order to make myself available for those people to ping, I have to keep a chat client open. Up until a few days ago, that client was Pidgin.

Pidgin is a nice piece of software — reliable, and pretty well designed from a user interface perspective. But there are a couple of things I don’t like about it. Pidgin has a large memory footprint — it’s like having another browser open. Granted, on my 4GB system, I don’t really miss 100+MB, but it’s the whole idea that you could blow 100MB on something a simple as chat that makes me uncomfortable.

A bigger concern for me is screen real estate. I like to keep chat and email open on the same workspace, which I divided with xmonad into 80% mutt, 20% pidgin. Pidgin, though, is not very happy with 20%. Perhaps if I could figure out a way to use a smaller font it would be OK, but as it stands you can’t see much of a conversation in that space, and not all the buttons are visible. Plus, each conversation wants to pop up a new window, which crunches all the others down. I really don’t want to dedicate more space to chat.

Like most X11 programs, Pidgin favors the mouse. I think there probably are ways to use the keyboard to achieve most tasks, but Pidgin was definitely designed for use with the mouse. The X11 requirement also means that you can’t use it from a virtual console. Sure there’s finch, but I didn’t find that very impressive.

What I needed was a lean, compact, text-based, customizable chat client that runs in a terminal window. So I wrote one.

I call it jab, because it uses XMPP (Jabber), and it’s written in Ruby (the contrarian in me decided to drop the ‘r’ in Jabber rather than adding one). All chat actions are Ruby statements that you enter as commands within a single terminal window. Responses are displayed in the same window, but they begin with a ‘>’ to distinguish them. You can also apply color filters to help visually separate conversations with different people, or different kinds of notifications.

Because jab runs as a read-eval loop in Ruby, it’s essentially a specialized irb. In fact, I’ve separated the Jab class into its own file, so if you prefer you could even run your chat session from irb instead of jab. Because you have the full power of Ruby in your hands, you can customize jab in an infinite number of ways. Naturally, it supports rc files to set up your preferences for each session.

I’ve tested jab with Jabber and Gmail accounts on both ends, running on FreeBSD 8 and Windows Vista (though Vista reports errors from Ruby’s Readline module if you don’t have terminfo and stty — I use MKS’s version). I haven’t tried any other hosts or client platforms, so if you do please let me know your results.

For full documentation, see the README. For concise documentation, start jab and type ‘help’.

You can download the tarball below, or clone the sources from BitBucket.

download
Tags: , , , , , ,

Posted in Ruby, Unix | 1 Comment » RSS 2.0 | Sphere it!

Preview your Rump before posting it

June 19th, 2010 4:26:31 pm pst by Sterling Camden

Right after posting my previous entry, I realized that rump needs a preview capability. Embedded code is hard to get right the first time, and so are any other embedded HTML tags. Plus, you want to see how images line up and check the links. I’d like to be able to make any corrections to those in vim before submitting a draft to WordPress.

So, I’ve added the -p switch to preview the post. You have to supply an RHTML file for formatting, and then you can generate HTML (or anything else, for that matter) from your post. I’ve also provided some scripts to make it easy to see how to send that output to Firefox, w3m, or whatever browser you choose. See the README file for details.

If you want your RHTML template to produce a page that looks just like your blog, you can do what I did:

1. Fetch a single, existing post from your blog. For example:

fetch -o preview.rhtml "http://chipstips.com/?p=511"

2. Edit the file to:
a. add the necessary require (as per the README): <% require "rump_preview" %>
b. change all occurrences of the title to <%=title%>
c. replace the content of the post with <%=content%>
d. replace the categories with <%=categories.length > 0 ? categories.join(",") : "Uncategorized" %>
e. replace the tags with <%=tags.size > 0 ? tags : "None"%>

3. Voila! As long as your stylesheets and scripts are linked by absolute URL, your previews should look just like your blog posts.

You can download the updated tarball below, or get it from the BitBucket repository.

download

Posted in Ruby, Web | 1 Comment » RSS 2.0 | Sphere it!

Rump – Ruby Upload Metaweblog Post

June 16th, 2010 3:14:42 pm pst by Sterling Camden

Ever since moving to vim as my primary editor, I’ve wished that I could also use it as my blogging platform, without having to copy/paste and reformat everything. Well, now I can — in fact, I’m posting this directly from vim.

I created a Ruby script named rump — it’s an acronym for Ruby Upload Metaweblog Post, but it also reflects the fact that blogging is a form of showing your ass. You can download this script below, or access it from its Bitbucket repository.

See the included README file for details. Essentially, you write your blog post as Ruby code — but rump provides some methods to keep that code to a minimum. For instance, in this post, I have a command for title, categories, tags, and content — the latter using a Here document as its parameter. Let me illustrate:

title "Rump - Ruby Upload Metaweblog Post"

tags "ruby, blogging, metaweblogapi, vim"
category "Ruby"
category "Web"

content <<EOF

(content goes here)

EOF 

The rump script takes this file, or any number of files, as arguments. So, I use a configuration file as the first argument to set up static things like what server to access, followed by the file that contains the post. You can also override settings in the command line with -e “commands”. The special file name “-” represents STDIN.

To make this even easier, I set up a separate directory on my local system for each blog, then put all the configuration commands into a file named .rump. Then, I mapped F12 in vim to post using that file, by adding the following to my .vimrc:

:map <F12> :w !rump .rump -^M

That “^M” is an actual return character.

So, now that I’m done with this post, I’ll press F12 and it will magically appear on Chip’s Tips.

download

Posted in Ruby, Web | 10 Comments » RSS 2.0 | Sphere it!

Better Tag Cloud