AeroSnap in Ubuntu: My Take
→ January 19th, 2010Based off of an OMG! Ubuntu! post, which was in turn based off of an Ubuntu forum post, here’s my take on Aero Snap type functionality in Ubuntu.
First, we need the utilities to edit Compiz settings and control the windows:
sudo apt-get install compizconfig-settings-manager wmctrl
Next, launch the Compiz Config Settings Manager, which can be found in System → Preferences. Go to the Commands section. For Command Line 0, use:
WIDTH=`xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 1 -d 'x'` &&
HALF=$(($WIDTH/2)) && wmctrl -r :ACTIVE: -b add,maximized_vert &&
wmctrl -r :ACTIVE: -e 0,0,0,$HALF,-1
For Command Line 1, use:
WIDTH=`xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 1 -d 'x'` &&
HALF=$(($WIDTH/2)) && wmctrl -r :ACTIVE: -b add,maximized_vert &&
wmctrl -r :ACTIVE: -e 0,$HALF,0,$HALF,-1
And for Command Line 2, use:
wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz
Command 0 is for snapping the window to the left. Command 1 is for snapping the window to the right. Command 2 is for maximizing the window.
Still in the Commands section, open the Edge Bindings tab. Attach Command 0 to the left-hand side, Command 1 to the right-hand side and Command 2 to the top. You might get warnings about bindings existing for these options. Make sure to force the commands to these bindings.
Back on the main screen, enter the General Options screen. Find the Edge Trigger Delay option and set it to at least 500. This is the delay before a command is executed when something hits the edge of the screen. I’ve found that lower values cause a lot of problems with accidental detections.
Here is where I deviate from the tutorials mentioned above. First, I found that Edge Flipping was not allowing the snapping to take place. From the main screen, go to the Desktop Wall plugin. In the Edge Flipping tab, uncheck the Edge Flip Move option.
Also, the Snapping Windows plugin was causing problems, since your windows might get close enough to the top the screen, which it will then snap into place, which will then maximize, which might not be what you wanted. I disabled this plugin.
Slightly on-topic, if you want this functionality on your Mac, Cinch is great program and it will only cost you $7.
Fixing Weird Formatting, The Programmers Way
→ January 8th, 2010Here’s a unique problem. A friend of mine suggested I get some Robert E. Howard stories for my Kindle and pointed me to the Project Gutenberg Australia website for some free eBooks by him. eBook is a loose term, since the versions that are available are either TXT or HTML versions.
I chose the TXT version, since I could just dump it on my Kindle and have a decent looking version. The problem: the TXT version is really an HTML page with a large chunk of preformatted text as the eBook. They also limited the sentence length to 80 columns. Even if I stripped the HTML from the source, the line breaks didn’t match with the width of the Kindle.
So I wrote a Ruby script to fix it. It reads every line of the file. If there’s a sentence, it prints it back out without a newline character. If there’s a newline character, it starts a new paragraph. The script also allows you to skip a certain number of lines in the beginning, since they usually represent the title and author information and should be on separate lines.
It was also a good excuse to learn Ruby’s OptionParser library, although I didn’t dive in too far.
Enjoy.
#!/usr/bin/env ruby
require 'optparse'
# Default options
dumped_line = false
input_file = STDIN
output_file = STDOUT
skip = 0
# Parse the options
ARGV.options do |o|
script_name = File.basename($0)
o.set_summary_indent(' ')
o.banner = "Usage: #{script_name} [OPTIONS] [input_file] [output_file]"
o.define_head 'Convert given Gutenberg txt file to a cleaner text file'
o.separator ''
o.on('-s', '--skip=val', Integer, 'Lines to skip') { |s| skip = s }
o.separator ''
o.on_tail('-h', '--help', 'Show this help message.') { puts o; exit }
o.parse!
end
if ARGV.count == 1
# One more argument means an input file is given
input_file = File.open ARGV[0]
elsif ARGV.count == 2
# Two more arguments means both input and output file are given
input_file = File.open ARGV[0]
output_file = File.open ARGV[1], 'w'
end
input_file.each_line do |line|
line.strip!
# Do we need to just pass the lines?
if skip != 0
output_file << line
output_file << "\n"
skip -= 1
next
end
if line == ''
# Blank lines mean new lines
output_file << "\n"
output_file << "\n" if dumped_line
dumped_line = false
else
# Just dump the line without a new line
output_file << line
output_file << ' '
dumped_line = true
end
end
input_file.close
output_file.close
Weeks in a Month Calculations
→ January 5th, 2010I was bitten by a nuance in Ruby where the Date “2010/01/01″ is actually the 53rd week in 2009. I probably don’t fully understand how the cweek method works, but to see it in action, fire up irb and try:
Date.civil(2010,1,1).cweek
I needed a new way to calculate all of the weeks in month and my old solution was a hack, so I came up with another quick hack to get it right. Below is my code to extend Date and Time to return an array of ranges for every week in a month.
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Date #:nodoc:
module MyCalculations
# Return an array of ranges with the weeks in the month
def weeks_in_month
weeks = []
start = finish = beginning_of_month
while finish != end_of_month
finish = start.end_of_week
finish = end_of_month if finish > end_of_month
weeks << (start..finish)
start = finish + 1.day
start = start.beginning_of_day
end
weeks
end
end
end
module Time #:nodoc:
module MyCalculations
# Return an array of ranges with the weeks in the month
def weeks_in_month
weeks = []
start = finish = beginning_of_month
while finish != end_of_month
finish = start.end_of_week
finish = end_of_month if finish > end_of_month
weeks << (start..finish)
start = finish + 1.day
start = start.beginning_of_day
end
weeks
end
end
end
end
end
class Date
include ActiveSupport::CoreExtensions::Date::MyCalculations
end
class Time
include ActiveSupport::CoreExtensions::Time::MyCalculations
end
Complaints For The Dell Inspiron Zino HD
→ January 4th, 2010I had completely forgot my boss had ordered two Dell Inspiron Zino HD’s to use in our conference room. How could forget this? I present Exhibit A:
A month and a day just to ship the device. In their rush to ship this system to us, they also threw in a weird gift. The system was built with Windows Vista Home Basic, but included in the box is a Quick Start Guide for Ubuntu, for a Netbook.
It’s always a shock to see the differences between Dell’s consumer and business lines. I was pretty spoiled to work for a large company who had contracts with Dell. You got what you needed in a great package in a timely manner. How do consumers put up with this?

