Archive for September, 2010

Battery level warning for mac os x

Thursday, September 23rd, 2010

My girlfriend’s MacBook has an embarrassingly short battery life and has a habit of just shutting off without any warning. I wrote a little bash script to pop up a warning if her battery gets below a threshold. Here’s the script, I save it in a file called ~/.battery_warning.sh:


#!/bin/bash
min_percentage="15"
percentage=`pmset -g batt | grep -o "[0-9]\+%; discharging" | \
  sed -e "s/%.*$//"`
if [ "$percentage" ]; then
  if [[ $percentage -lt $min_percentage ]] ; then
    osascript -e "tell application \"System Events\"
        activate
        display dialog \"Battery almost dead: $percentage%\" \
          with icon 0 with title \"Warning\" buttons {\"OK\"} \
          default button 1
      end tell"
  fi
fi

Then I set it up as a cron job to check every 5 mins:


*/5 * * * *  bash ~/.battery_warning.sh

Michelada recipe

Thursday, September 23rd, 2010

Tonight I tried making Micheladas for the second time. I fell in love with these at Caracas, a Venezuelan arepa bar in the East Village. I don’t think that the ones I make are quite as good as theirs, but they’re already better than the ones at the hole-in-the-wall mexican restaurant down the street.
michelada in pint glass

  • Pint glass full of ice
  • 1 small lime
  • 1/2 bottle Pacifico (corona, etc. or any other light beer)
  • 2 shakes of soy sauce or fish sauce
  • 4 shakes JalapeƱo pepper hot sauce from Trader Joes (or the like)
  • 2 shakes Tabasco (or the like) or 1 pinch cayenne powder

I like to squeeze the lime over the ice, add a little beer, then the other ingredients, the the rest of the beer. The key it seems to making this not taste disgusting is to use a good amount of lime juice and to be careful with the hot sauces/powders. Don’t just use tabasco. Tabasco somehow has a very familiar taste, and when over used the whole drink just resembles drinking straight from the Tabasco bottle. I like to mix and match hot sauces/powders.

I also find that using a little fish sauce or Worcestershire sauce or soy sauce really helps even things out. I splash of sugar water (aka simple syrup) never hurts if you making these for the weak of tongue.

Vi(m) tip #10: Current file name

Monday, September 20th, 2010

A friend at work wanted to be able to see the name of the current file he was editing in vim without closing it or saving it. The solution he found was to enter in command mode:


<ctrl> <shift> G

This displays a little info bar on the bottom.

This made me remember that you can use % as a standin for the current file's name when issuing a command. As in:


:!echo %

which displays the current file name (up to bash gotchas).

Or you could write the current file to filename.temp with:


:w %.temp

I used this in an earlier post to typeset a latex document from vim as a oneliner.

Ruby plus equals (+=) versus append/concatenation shovel (<<)

Wednesday, September 8th, 2010

I was stunned to watch how slow a recent ruby program was. All it was doing was concatenating a bunch of string literals in a big loop. Originally I was using plus equals:


str = ""
1000.times do |i|
  str += "foo bar"
end

On a whim I tried switching to using an array then joining:


str = ""
str_array = []
1000.times do |i|
  str_array << "foo bar"
end
str = str_array.join

Already this was way faster. I wrote up a little benchmarking program to see just how badly "+=" performs compared to "<<". I compare string +=, to the array set up I have above, and just using "<<" on the string:


str = ""
1000.times do |i|
  str << "foo bar"
end

Here's my little test program.


#!/usr/bin/ruby

power = 20

power.times do |p|
  n = 2**p
  str = ""
  start_time = Time.now
  n.times do |i|
    str += "x"
  end
  duration = Time.now - start_time
  #puts "#{n} string appends took: #{duration}s"
  puts "#{n} #{duration}"
end

power.times do |p|
  n = 2**p
  str3 = ""
  start_time = Time.now
  n.times do |i|
    str3 << "x"
  end
  duration = Time.now - start_time
  puts "#{n} #{duration}"
end

power.times do |p|
  n = 2**p
  str2 = ""
  start_time = Time.now
  str_array = []
  n.times do |i|
    str_array << "x"
  end
  str2 = str_array.join
  duration = Time.now - start_time
  puts "#{n} #{duration}"
end

And here are the results:
ruby plus equals vs append

String += is asymptotically worse than <<. Reading through the ruby doc on strings its clear this is because:


str1 += str2

is syntactic sugar for something like


str1 = str1 + str2

whose "=" creates a new string object, hence the big computational cost.

But why?! I can't think of any reason why "+=" shouldn't be syntactic sugar for "<<". Can you? Update:
I get it!
Here's two short snippets that illustrate the difference:


a = "x"
b = a
b += "y"
a

Which results in "x"


a = "x"
b = a
b << "y"
a

Which results in "xy"

It's subjective whether x+=y should mean "append y to x" or always be syntactic sugar for "x = x + y". My vote is for the later, which means I must be content that in Ruby these operators do different things and thus have different speeds.

Max Schmeling #2

Monday, September 6th, 2010

Max Schmeling #2

http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/

Dorgan Brothers

Monday, September 6th, 2010

Dorgan Brothers

http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/

Max Schmeling #1

Monday, September 6th, 2010

Max Schmeling #1

http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/

Alex, Louis and Will with a tree and a dog

Monday, September 6th, 2010

Alex, Louis and Will with a tree and a dog

http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/

Blue tint on mac wake from sleep

Sunday, September 5th, 2010

If you wake your mac from sleep and suddenly your screen is all a slightly blueish hue. Open up Terminal.app and issue this command:


/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Resources/DMProxy

Update: This also works if you screen gets a funny bluish tint when Powerpoint slideshow mode goes back and forth from fullscreen.

OpenGL render GL_POINTS as circles not squares

Wednesday, September 1st, 2010

Bugged me into staying late today. To make OpenGL render points (GL_POINTS) as circles instead of squares be sure to add:


glEnable(GL_POINT_SMOOTH);

aka point antialiasing.