#!/usr/bin/ruby
require 'open-uri'
site = ARGV[0]
query = URI.escape(ARGV[1..-1].join(" "))
case site
when "google": url = "http://www.google.com/search?q=#{query}"
when "wikipedia": url = "http://en.wikipedia.org/wiki/Special\:Search?search=#{query}"
when "torrentz": url = "http://www.torrentz.com/search?q=#{query}"
when "isohunt": url = "http://isohunt.com/torrents/?ihq=#{query}"
when "mininova": url = "http://www.mininova.org/search/?search=#{query}"
end
system("elinks #{url}")
Monday, October 20, 2008
Better search script (ruby)
Turns out I can kick off a text based browser from Ruby just fine. The trick is to use sytem("elinks") instead of `elinks`. Here's the Ruby version of my search script (cleaner):
Saturday, October 18, 2008
Setting destination directory for wget
The phrase "destination directory" does not appear in the wget man page. Turns out they call it the "directory prefix":
wget -P ~/dest/dir www.foo.com/myfile.png
Saturday, August 9, 2008
Remap keys in Windows with SharpKeys
Caps lock is such a useless key. I almost never hit it intentionally. When I do strike it, the results are at best annoying (I LOOK LIKE I'M SHOUTING) and at worst are catastrophic! (try hitting caps lock in the middle of a vim session sometime. Or better yet, try hitting it in the middle of someone else's. I guarantee you there will be profanity. :)
Given my vim-ish tendencies, I decided to map Caps Lock to Escape (a very important key in vim, which is in a crappy location on many keyboards). There are several ways to do this under Linux. Under Windows, registry magic is needed.
Fortunately, a free program called SharpKeys will do this for you. (It looks like it can map just about any key to any other key. I haven't played with it.)
Given my vim-ish tendencies, I decided to map Caps Lock to Escape (a very important key in vim, which is in a crappy location on many keyboards). There are several ways to do this under Linux. Under Windows, registry magic is needed.
Fortunately, a free program called SharpKeys will do this for you. (It looks like it can map just about any key to any other key. I haven't played with it.)
Thursday, August 7, 2008
Updated weather script
Since the weather page I used for my previous weather script changed, reducing half of my scripts output to an ascii-splosion, I decided to take another stab at it. Turns out Google supports weather searching. I rewrote the regular expressions and incorporated some new ruby tricks I've learned since. I'm actually rather pleased with the result: weather.rb
The regular expressions are a bit yucky, but that's to be expected. I think the rest of the code is pretty readable. Cool feature: that bit with the join and URI.escape at the top lets you use it like so:
"san", "francisco,", and "ca" will be passed as separate arguments, but the line at the top will join them together and convert the resulting string into something you could type in a URL bar. (ie: spaces -> %20).
Makes it easy to be sloppy on the command line. :)
The regular expressions are a bit yucky, but that's to be expected. I think the rest of the code is pretty readable. Cool feature: that bit with the join and URI.escape at the top lets you use it like so:
weather san francisco, ca"san", "francisco,", and "ca" will be passed as separate arguments, but the line at the top will join them together and convert the resulting string into something you could type in a URL bar. (ie: spaces -> %20).
Makes it easy to be sloppy on the command line. :)
Monday, August 4, 2008
Interesting case of "for x in xs" in Javascript
Recently, I began experimenting with Javascript (mostly to see what the fuss was all about.) I'm making a web page form to help students computer their current grade in a class, based on the grading (weighting) system outlined in the syllabus and the known scores on assignments. (Pretty simple, but hopefully useful.) More on that in a future post.
As I was working, I stumbled on some rather bizzare behavior (at least to me, Javascript neophyte) that I figured was worth remarking on, in case someone else (or my future self) encounters a similar problem.
In part of my code, I wanted to be able to search the array by matching against member variables of its elements. After hunting around online a bit, I was led to the conclusion that extending the Array class was the accepted (and clever?) thing to do here:
I got busy with other things, and weeks went by. Tonight, I resumed work on the code. I was trying to generalize the task of dynamically adding rows to tables, since this was going to happen in several places on the page. This task involves appending children to a node, so I wrote the row adding code to iterate over an array of cell objects and add each of them to the node. Seemed simple enough.
However, the alignment was off. After poking around a bit with Firebug (a lifesaver, by the way; I see us becoming close friends), I determined that an extra node was being added to the beginning of the row.
After assuring myself that my indexes all started where I intended them to, I decided to add an "alert('col = ' + col)" line to the each phase of the adding process, to show me what was going on (a variant of the classic "print("x = " + x)" line for debugging purposes.) The alerts showed me the indexes as:
...wtf?
I didn't research this in depth (this entry was mostly for future reference in case I hit this problem again...sometimes school/work makes me go a while between pet projects). However, my theory is that my custom "getNdx" function was picked up as another element of cells by the "for" iterator. Which...sort of...makes sense. I guess. In a free-form, loosely typed, Javascripty sort of way.
At any rate, maybe that will clear this up if anyone else hits this issue. Simplified code to show this behavior:
As I was working, I stumbled on some rather bizzare behavior (at least to me, Javascript neophyte) that I figured was worth remarking on, in case someone else (or my future self) encounters a similar problem.
In part of my code, I wanted to be able to search the array by matching against member variables of its elements. After hunting around online a bit, I was led to the conclusion that extending the Array class was the accepted (and clever?) thing to do here:
Array.prototype.getNdx = function(id) {
for (i in this) {
if (this[i].id == id) {
return parseInt(i);
}
}
return -1;
}
I got busy with other things, and weeks went by. Tonight, I resumed work on the code. I was trying to generalize the task of dynamically adding rows to tables, since this was going to happen in several places on the page. This task involves appending children to a node, so I wrote the row adding code to iterate over an array of cell objects and add each of them to the node. Seemed simple enough.
for (col in cells) {
var cell = row.insertCell(col);
...
cell.appendChile(node);
}
However, the alignment was off. After poking around a bit with Firebug (a lifesaver, by the way; I see us becoming close friends), I determined that an extra node was being added to the beginning of the row.
After assuring myself that my indexes all started where I intended them to, I decided to add an "alert('col = ' + col)" line to the each phase of the adding process, to show me what was going on (a variant of the classic "print("x = " + x)" line for debugging purposes.) The alerts showed me the indexes as:
0...1...2...3...*etc*...getNdx...wtf?
I didn't research this in depth (this entry was mostly for future reference in case I hit this problem again...sometimes school/work makes me go a while between pet projects). However, my theory is that my custom "getNdx" function was picked up as another element of cells by the "for" iterator. Which...sort of...makes sense. I guess. In a free-form, loosely typed, Javascripty sort of way.
At any rate, maybe that will clear this up if anyone else hits this issue. Simplified code to show this behavior:
Array.prototype.getNdx = function(id) {
for (i in this) {
if (this[i].id == id) {
return parseInt(i);
}
}
return -1;
}
var col = 0;
var cells = new Array();
cells[col++] = "asdf"
cells[col++] = "fdsa"
for (j in cells) {
alert(j);
}
Tuesday, July 15, 2008
Bring some sanity to Windows window management
I am fed up with all the precision mouse-work required to simply move and resize windows under most non-X11 window managers. (Read: Windows and Mac). On most X11 window managers, you can hold down the "alt" key and click and drag on the window. Left clicking moves the window, and right clicking resizes the window. So much more humane than trying to grab those edges!
In the midst of frustration today at too-tiny default windows, I hunted around for a solution. Behold: KDE-SizerXP.
I myself never associated this behavior with KDE specifically, but I'm not one to complain!
(Mac users, take a look at Window Dragon. I haven't used it, but I believe it solves the same problem).
In the midst of frustration today at too-tiny default windows, I hunted around for a solution. Behold: KDE-SizerXP.
I myself never associated this behavior with KDE specifically, but I'm not one to complain!
(Mac users, take a look at Window Dragon. I haven't used it, but I believe it solves the same problem).
Monday, June 9, 2008
Fixing DPI changes in Linux
(or, how to enforce a constant DPI)
Lately, I found that repeatedly enabling and disabling an external monitor for my laptop caused the fonts to get smaller and smaller. It appears this was linked to the DPI getting changed somehow. With a bit of hunting, I found the following useful bit of info:
"...some distributions launch X using "-dpi 100". Fonts will appear as intended..."
(http://scanline.ca/dpi/)
Indeed, adding
Lately, I found that repeatedly enabling and disabling an external monitor for my laptop caused the fonts to get smaller and smaller. It appears this was linked to the DPI getting changed somehow. With a bit of hunting, I found the following useful bit of info:
"...some distributions launch X using "-dpi 100". Fonts will appear as intended..."
(http://scanline.ca/dpi/)
Indeed, adding
-dpi 100 to defaultserverargs in /usr/bin/startx did the trick. (At least for now...we'll see if it holds.)
Subscribe to:
Posts (Atom)