Ruby for Batching

September 16th, 2007

The other day I wanted to resize a bunch of images using ImageMagick’s convert command-line utility. I wanted to both resize and change their names from image_name.png to image_name-sm.png, but I couldn’t figure out how to guide ‘convert’ to use part of the original name in the new name. The next obvious tactic would be to use some common shell utilities like ‘for’ and ‘do’. My shell-fu is rusty however, and I still couldn’t get things quite the way I wanted them. Then I think to myself, this would be much easier to do with Ruby.

First, I use ‘puts’ to see if the syntax will come out right:

1
2
3
4
5
6
irb(main):017:0> Dir['*.png'].each { |f| puts "convert #{f} -resize 400x400 #{f[0..-5]}-sm.png" }
convert character_creation.png -resize 400x400 character_creation-sm.png
convert battle-mountain_pass.png -resize 400x400 battle-mountain_pass-sm.png
convert loot-drag_and_drop.png -resize 400x400 loot-drag_and_drop-sm.png
convert market.png -resize 400x400 market-sm.png
...(etc)...

Then I just run the same line with system instead of puts:


Dir['*.png'].each { |f| system "convert #{f} -resize 400x400 #{f[0..-5]}-sm.png" }

With a little practice, this can be done on the command line - who needs shell scripting?

1
2
# run from the shell prompt:
ruby -e 'Dir["*.png"].each { |f| system "convert #{f} -resize 400x400 #{f[0..-5]}-sm.png" }'


Mephisto - Stepping Stone Theme

January 11th, 2007

Like many others, I migrated my old Typo blog to Mephisto. I grabbed a new domain name, inspired by the photograph my fiancĂ©e took outside our house one January morning while I was getting ready for work. I’m pretty familiar with CSS, but I’m not a great designer. Fortunately, there are a lot of great design sites out there that have open source site templates. I went with a template called “Stepping Stone”, by Six Shooter Media. I liked this template because it’s very clean and uncluttered, and it’s pretty tame in terms of colors. I also was looking for something I could easily change and add the morning-birds photo to.

I’ve ported the Stepping Stone theme to Mephisto. I’ve made a few changes to the original design. The text was a little to light to be readable on the white background, and I wanted to make the content area wider. I also added the CodeRay CSS stuff so that you can use the CodeRay macros and get syntax highlighting (for the things CodeRay supports now – good stuff like ruby, html, rhtml, etc).

The Stepping Stone theme for Mephisto is available for download.

history

October 18th, 2006

From laundr.us:

jason@jubunt:~/dev/bridge$ history | awk '{print $2}' | awk 'BEGIN {FS="|"} \ 
{print $1}' | sort | uniq -c | sort -nr | head -10
     98 rake
     89 svn
     56 cd
     46 ls
     17 sudo
     17 ssh
     15 more
     15 ./bridge_dev_wmii.sh
     13 irb
     11 wget

wmii: auto-tagging

September 1st, 2006

Lately I’ve been playing with wmii, the window manager. The ruby-addons for wmii by mfp open up the base wmii quite a bit. Once I got used to this dynamic window manager, I can’t imagine going back to WIMP.

I started to poke around in the ruby-wmii config files, because I was curious about how whenever I opened Firefox, the client was automatically tagged with “web”. Since I was constantly tagging my other apps with names using MODKEY-ctrl-T, I decided I wanted some of them to auto-tag like the browsers do.

After initially just copying the existing code that tags the browsers, I finally worked out a way to get it to tag things the way I want them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 # *** excerpt of wmiirc-config.rb ***

 # first, I commented out this orginal tagger

 # {{{ Tag all browser instances as 'web' in addition to the current tag
 # browsers = %w[Firefox Konqueror]
 # browser_re = /^#{browsers.join("|")}/
 # on_createclient(condition{|c| browser_re =~ read("/client/#{c}/class")}) do |cid|
 #   write("/client/#{cid}/tags", normalize(read("/client/#{cid}/tags") + "+web"))
 # end

  #############################################
  # here's the stuff I added
        
  # these are tags for particular apps that I want to have 
  # new tags - note, they won't get appended to their current tag

  tag_for_apps = { 
    "irc" => "Xchat", 
    "gaim" => "Gaim", 
    "web" => "Firefox",
    "4-jedit" => "jedit"
  }
  
  # these are terminals that I've given specific titles to
  # so they can be tagged
  tag_for_named_terms = {
    "2-consoles" => "console:",
    "3-logs" => "log:",
    "1-terms" => "term:"
  }
    
  # now when a new client comes up, check for an autotag
  on_createclient do |cid|
    LOGGER.info "checking for autotag on class: #{read("/client/#{cid}/class")} " + 
        "and name: #{read("/client/#{cid}/name")} "
    # if this client is a terminal, check the 'name'
    if /terminal/ =~ read("/client/#{cid}/class")
      tag_for_named_terms.each do |tfn|
        names_re = /#{tfn[1]}/
        if names_re =~ read("/client/#{cid}/name")
          write("/client/#{cid}/tags", tfn[0])
          LOGGER.info "  ... autotag'd #{tfn[1]} with #{tfn[0]}!"
        end
      end
    # not a terminal, so go by the 'class'
    else
      tag_for_apps.each do |tfa|
        apps_re = /#{tfa[1]}/
        if apps_re =~ read("/client/#{cid}/class")
          write("/client/#{cid}/tags", tfa[0])
          LOGGER.info "  ... autotag'd #{tfa[1]} with #{tfa[0]}!"
        end
      end
    end
  end

Notice, for terminals, I check for certain terminal window titles. I changed my gnome-terminal script a bit, so now it looks like this:

#!/bin/sh

# set working dir
wd="/home/jason/dev/rails_app" 

gnome-terminal --working-directory=$wd --hide-menubar -e "mongrel_rails start" -t "log: web server" \
--window --working-directory=$wd --hide-menubar -e "tail -f log/development.log" -t "log: dev log" \
--window --working-directory=$wd --hide-menubar -e "script/console" -t "console: rails irb" \
--window --working-directory=$wd --hide-menubar -e "mysql -u root --password=mysecret myapp_development" -t "console: mysql" \
--window --working-directory=$wd --hide-menubar -t "term: approot"

For some of my tag names, I stick a number in the front – that’s just to force them into order so I can easily MODKEY-n over to them.

I’d like to work the auto-tagger out into a plugin, but after poking around some of the existing plugins I gave up. I have no idea how to check for on_createclient from within a plugin, and I’m pretty confused about how those long-winded option get passed to plugins. If anyone has any hints, I’d love to hear them.