Chip's Tips for Developers

Contains coding, but not narcotic.

When — I just can’t leave it alone

May 28th, 2010 3:11:29 pm pst by Sterling Camden

I’ve added some new features to my add-ons for the when calendar program.  You can download the tarball below, or pull it from BitBucket.

Audible reminders

I found that I wasn’t always noticing my reminders in xmobar, so I decided to add sound effects.  I changed remindwhen.rb to look for a file named ~/.when/reminder.wav.  If it exists, that file is played (using mplayer) whenever a reminder is output.

Since my xmobar checks reminders every minute, and I sometimes want advance warning on a reminder, I found that the beeping once a minute can get pretty aggravating.  So I wanted a way to mute the alarms temporarily.  Thus, I created a shell script to turn the alarm on or off (by renaming reminder.wav) and tied it to a key shortcut in xmonad.  I then added a visual indicator to xmobar when alarms are muted.  How that works is all described in the README.

I also found that for some reminders I don’t want an audible alarm ever.  So I added a ‘quiet’ option within the text of the reminder, and I changed remindwhen.rb to strip that out (as well as any advance) before outputting the event.

Calendar format

I was so proud of my enhancements to when that I showed my wife my new scheduling tools.  “Yuck,” she said, “how can I see it as a calendar?”

Hmm, that’s true.  When looking for open days, you need to see the whole grid with weekdays – not just the already scheduled events.

So I wrote a couple of more scripts to pipe when’s output to pcal to create an HTML calendar that includes the event text on the right days.  Then I created another script that, with one command, invokes when piping the result through the first script and into w3m for viewing on a terminal.

By-product

As part of the implementation of the alarm support, I wrote a very handy little script to do an in-line, ternary conditional echo.  It’s called, oddly enough, “echoif”:

echoif “likeit” “applause” “`criticism`”

For details on how to use these new scripts, see the README.

download

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

Synthesis 2.1.4: split the difference

May 26th, 2010 10:12:32 am pst by Sterling Camden

Richard Barndt sent me a first pass implementation of a split method for Regex – that is, the ability to split a string at the location of an expression.  He was also nice enough to include several test cases using assertions.  I’ve adapted his syntax to my own preferences and included Regex.split in version 2.1.4 of Synthesis, which you can download below.

Whereas Richard’s version returns an ArrayList of strings, I elected to return an ls of Vars.  An ls can be used as an Arraylist, but it adds many useful methods.  Var is the normal way to store strings in an ls, for various reasons.  I also elected not to include the static Regex.split method – it seems needlessly repetitive, since you have to supply an expression anyway.  I did, however, add a split method to Var for convenience.

This implementation of split uses the GlobalSearch option (g) to determine whether to split at every occurrence of the expression or only the first one.  Captured groups within the expression are returned in the array as well at their respective positions – otherwise the match to the expression is not returned.  There’s also an option to include empty strings that result from the split, which is true by default.

Thanks, Richard, for the idea, the code, and the test cases!

download

Posted in Regexen, SynergyDE | 1 Comment » RSS 2.0 | Sphere it!

When you forget to ask when, use remindwhen: reminders for the when calendar

May 6th, 2010 10:41:24 am pst by Sterling Camden

In my quest to find more lightweight, componentized, customizable tools for my work, I decided to use when for my calendar.  When uses a simple text file for calendar events and sports a command-line interface, yet its handling of dates and recurrence is quite sophisticated.

When doesn’t have any mechanism for reminders, which is something my quinquagenarian brain finds necessary.  But that turns out to be good Unix design, because you can easily layer your own reminder mechanism over when, as I have in the scripts you can download as a tarball below, or pull the files from the BitBucket repository.

When’s event format is simple:  each line begins with a specification of the date(s) on which the event occurs, followed by a comma, followed by a free-text description.  There is no requirement or convention for specifying a time for the event.   It therefore just ends up as part of the event’s text.

Some users invoke when as part of their shell profile, so they’re reminded of upcoming events whenever they log on.  But I stay logged on all day, so I need more frequent opportunities for reminders.  Thus, I created remindwhen.rb, which I run once a minute from xmobar so I get a bright yellow reminder at the top of my screen as events become due:

image

In keeping with the Unix philosophy, though, you can send the output of the script to whatever notification mechanism you prefer.  The script merely parses the when output looking for events that are due within a range of the current time and sends them to stdout.  For an event to be considered, it has to contain a time in the format HH:MM[pm][+n], where HH is the hour, MM is the minute, “pm” is optional, and n optionally specifies an advance warning, in minutes.

To avoid cluttering the regular calendar with daily reminders, these scripts assume that you have at least two calendar files: the default (~/.when/calendar), and one just for reminders (~/.when/reminders).  The remindwhen.rb script searches the latter first, then the former.  So if you just want a reminder, add it to reminders.  If you also want to see it on your daily calendar, add it to calendar instead.

I’ve also created some supporting scripts to make reminders even easier to manage:

remind time description

adds a reminder to the reminders calendar for the specified time on today’s date.

reminders

shows today’s reminders form the reminders calendar

reminders e

edits the reminders calendar.

download

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

Getlessmail gets more scripts

May 3rd, 2010 3:24:24 pm pst by Sterling Camden

After using my getlessmail filter for getmail for a few days, I began to notice a usability issue.  Whenever I’d identify mail as from a spammer while viewing it in mutt, I’d have to do the following to add the sender to my filter:

  1. Highlight the sender’s address
  2. !vim ~/.getlessmail
  3. Gospam if from '\b (middle-click) \b' ESC :x

Since mutt supports piping messages thorough a filter, and allows you to create macros that bind keys to lengthy key sequences, I decided to write some supporting scripts.  I’ve added these to getlessmail, which you can download below.

By the way, Chad Perrin also created a BitBucket repository for this project, so you can pull it from there if you prefer.

The first script, glmpipe.rb, reads stdin parsing it as an email and looks for the sender’s address.  You can pass it a switch to indicate what you want to do with that address:

  • -k:  keep if from this address
  • -K:  keep if from this domain
  • -s:  spam if from this address
  • -S:  spam if from this domain

glmpipe.rb in turn calls another script (which you can also invoke from a command line):

glmadd.rb address –switch

where address is the email address, and switch is one of the options listed above, or –a to ask you for the option (which also happens if you don’t pass a switch).

So now, I’ve mapped ‘M’ (capital M, not the lowercase which is used for composing new mail) in mutt to launch the command |glmpipe.rb –, which waits for me to enter the desired switch and press Enter.  I can’t use -a\n here, because stdin is being filled by the message.  You can, of course, map individual keys to each function and include the newline if you so desire.  See the README for details.  I like to be asked, because that gives me a chance to back out if I hit capital M by mistake.

download

Posted in Ruby, Unix | 2 Comments » RSS 2.0 | Sphere it!

Better Tag Cloud