Tuesday, August 16, 2011

Toggle Airport power from emacs using shell commands

I can usually work for relatively long periods of time without needing the internet, or needing it only sporadically, and mostly while using emacs to edit various kinds of text files.  While I'm doing this, I usually turn off power to the Airport wireless radio on my MacBook Pro to save battery power, then turn it on only when I need it.

Using emacs trains you not to use the mouse unless absolutely necessary, and the small bit of time and effort required to turn the Airport off and on from the menu bar, added up over time, was sort of bothering me.  So, given sufficient desire to procrastinate, I found a way to toggle power to my Airport wireless radio from within emacs.

The first step is to write a shell script to do the toggling (see below).  After a bit of searching, I found the "networksetup" command in the discussion attached to this Macworld hint.  The trick is to use

networksetup -getairportpower en1 | cut -d : -f 2

to get the status and then

networksetup -setairportpower en1 Off

to turn it off, or "On" to turn it back on.

The second step was to insert it into my .emacs.el as a custom function with a key binding.  This was pretty straightforward and the code is in the comments in the shell script below.  The only potential sticky point is to make sure to declare the function as interactive so that it can be bound to a key.


# toggle-airport
# Toggles OS X airport power on or off.  dan.t.swain@gmail.com 8/14/11
# I built this to be used within emacs.  I bound it to C-c a with the
# following in my .emacs.el (obviously you need to change the path
# for your own use):
#  (defun dts-toggle-airport ()
#    (interactive)
#    (shell-command "~/bin/toggle-airport")
#    )
#  (global-set-key (kbd "\C-c a") 'dts-toggle-airport)
# Commands gleaned from the following mac hints discussion (near the bottom):
#   http://hints.macworld.com/article.php?story=20070728102702656

# get the current status, this will look like "AirPort Power (en1): Off"
#  so by cutting at the ':' and taking everything after that
#  we get " On" or " Off"
CURRENT_STATUS=`networksetup -getairportpower en1 | cut -d : -f 2`

# determine the new status from the old status - note the leading whitespace
if [[ "$CURRENT_STATUS" == " On" ]]

# set the new power status
networksetup -setairportpower en1 $NEW_STATUS
# display the results by querying the device again
networksetup -getairportpower en1

Monday, April 4, 2011

2D Matlab Plot With A Triangular Domain

I needed to generate a plot in Matlab for data over a triangular domain.  It took quite a lot of googling to find the answer, which ended up being relatively simple, so I thought I'd post the solution here.

The main trick was to use the fill command to draw a triangle over the unwanted parts of the plot.  Note that I also make the background color of the plot white, which I ALWAYS do because the default gray is hideous.  Here's some example code:

clear all;  close all;

x = linspace(-10, 10);
y = linspace(0, 20);
M = randn(length(y), length(x));

set(gcf, 'Color', 'w')
imagesc(x, y, M)
axis image
axis xy
set(gca, 'XAxisLocation', 'Top')
ylabel('X [units]', 'FontName', 'Times', 'FontSize', 14)
xlabel('Y [units]', 'FontName', 'Times', 'FontSize', 14)

hold on
xmin = min(xlim);
xmax = max(xlim);
ymin = min(ylim);
ymax = max(ylim);
h = fill([xmin xmax xmax xmin], [ymin ymin ymax ymin], 'w');
set(gca, 'Box', 'off')
set(h, 'EdgeColor', 'w')
plot([xmin xmax], [ymin ymax], 'k-')

This isn't a perfect solution. Changing the color of the background, plot box, etc, will mess it up.  It worked nicely for my application, though.  Here's what the output of the above looks like.

Your mileage will vary depending upon the renderer you use, etc.

Wednesday, March 16, 2011

Git Dashboard - A lightweight local git repository manager and Dashboard widget for OS X

The quick and dirty:  Git Dashboard is a lightweight git repository manager and Dashboard widget that I wrote to organize local repositories.

I use git for a lot of stuff.  I find it useful for document projects (latex mostly) as well as code projects and research projects.  The associated repositories are, to some extent, a snapshot of my work life, and therefore the more organized they are and the more diligent I am about keeping them up to date, the more sane my odd little world is.  But it's hard to keep track of them all.

I have a bad habit of doing a bunch of work and not committing.  Or stopping in the middle of a task --- intending to return to it the next day --- and ending up not coming back to it for days or weeks.  I kept thinking, "Man, it would be nice if I had a simple program that showed a list of my repositories and their statuses to keep track of all of this," but no such program seemed to exist.

So I wrote it myself.

I'm calling it "Git Dashboard".  It's very much a work in progress, but it's already pretty functional.  There's a screen shot of the app itself above and of the Dashboard widget below.  The basic idea is that you tell it where your repositories are (and/or you can have it scan directories recursively to discover repositories) and it keeps an up-to-date list telling you whether or not the repository is clean or dirty, when the last commit was, how many dirty or untracked files there are, etc.  You can double click a repository to launch a git client, and there's toolbar buttons to reveal the folder in Finder or launch Terminal in that directory.

Note that this is not meant to be a git client, like GitX.  Rather, it's a way to organize and keep track of your repositories.  I often use it as a launching point, too.  If I'm working on code for one project and want to commit, I double click on that repository in Git Dashboard to launch GitX so that I can organize my changes, etc.

If you want to check it out, go to my little working Git Dashboard web page and download the most recent dmg.   Note that it presently works only on OS X 10.6 (Snow Leopard), but I might be able to get it to work on 10.5 if there is some demand.  Please do tell me if you find it useful, if you find a bug, or have some ideas to make it better!