May 1st, 2012 at 10:55 pm
If you want to find out a shell script’s own path, you might be tempted to use $(pwd). But this will not work when you call the script from a different directory than the one where the script resides. This is due to the fact that commands like pwd refer to the environment of the calling shell, not the script.
A better approach is to use the bash variable $0. If the script is in a different location, $0 will contain the relative path from the shell to the script. Combined with dirname and realpath, you get a waterproof solution for the path problem:
MYDIR="$(realpath $(dirname $0))"
$MYDIR will then hold the absolute path to the script, no matter from where it was called.
There is, however, one pitfall: realpath is not a bash builtin, but it must be installed via the package manager on most distributions. Therefore, this is not an ultimatively portable solution.
0
Code
bash, shell
February 13th, 2012 at 1:51 pm
Multi-select fields are still a big problem for users. For one, it is almost impossible to unselect all items. And users often don’t understand that they may select more than one item, or how they could accomplish this.
Therefore I’ve written a little helper which is automatically appended to multi-select fields. It allows to select/unselect all items, and it offers a little help icon that explains how to use the multi-select fields. I’m doing this with jQuery, but this can be done with any other framework or plain JavaScript.
Check out the demo and its source code.
0
Code
HTML, JavaScript, jQuery, select
February 12th, 2012 at 2:55 pm
OpenLayers is able to creates SVG elements (vectors) on a map and listen for “select” events on them – usually a mouseover or click. Unfortunately, it is not easy to find out the screen position of the event. This is strange, because it works well for other event types (like drag). There are some “hidden” methods like ….object.events.getMousePosition(), but they don’t seem to work as intended, at least for me.
However, the element passed to the event callback contains a geometry property, a reference to the Geometry object of the vector, which provides the getBounds(), which again can be used with getCenterLonLat(). Now we do at least know the longitude/latitude position of the center of the selected element.
To determine the actual position on the screen, we use a bit of jQuery magic in this example, but this will work with other frameworks or plain JavaScript, too.
jQuery(document).ready(function(jQ){
// put your other OpenLayers code here (omitted for simplicity)
// create a map, add a base layer, set center, etc.
var
vectorsLayer = new OpenLayers.Layer.Vector("Vectors Layer", {
styleMap: new OpenLayers.StyleMap({
fillColor: '#D6A000',
fillOpacity: 0.8,
strokeWidth: 3,
strokeColor: '#665932',
pointRadius: 4
}),
rendererOptions: {yOrdering: true}
}),
pointsRaw = [
[51.33453,7.32293],
[51.1071,7.1422],
[50.88998,6.67058],
[50.73246,6.11502]
],
point,
points = [];
map.addLayer(vectorsLayer);
for (var i = 0; i<pointsRaw.length; i++)
{
point = new OpenLayers.Geometry.Point(pointsRaw[i][1], pointsRaw[i][0]);
point.transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
);
points.push(new OpenLayers.Feature.Vector(point));
vectorsLayer.addFeatures(points);
}
var onSelectCallback = function(vector)
{
var
lonlat = vector.geometry.getBounds().getCenterLonLat(),
xy = map.getViewPortPxFromLonLat(lonlat),
mapOffset = jQ('#map').offset();
jQ('<div>foobar</div>').css({
position: 'absolute',
top: (xy.y + mapOffset.top)+'px',
left: (xy.x + mapOffset.left)+'px',
backgroundColor : '#ffffff',
border : '1px solid #ccc',
zIndex : 2000
}).appendTo('body');
};
var selectControl = new OpenLayers.Control.SelectFeature(vectorsLayer, {
clickout: true,
toggle: false,
multiple: false,
hover: false,
onSelect : onSelectCallback
});
map.addControl(selectControl);
selectControl.activate();
});
Why would we need the position? For example, to create custom popup-like elements on the map. Yes, OL provides its own popups, but they are … not so beautiful, and its not easy to style them properly.
1
Code
jQuery, OpenLayers
February 7th, 2012 at 2:16 pm
If you have developed a larger web application with support for multiple languages, you will certainly have a number of functions that help with language-specific formatting of numbers, dates, currency expressions.
For those of you using jQuery, check out Globalize, an add-on library for jQuery that supports lots of formatting structures in a whole lot of languages (or, “cultures” in Globalize’s terms).
jQuery(document).ready(function(){
// make sure to have jQuery, the Globalize library and the respective culture file loaded
Globalize.cultureSelector = 'de';
alert( Globalize.format( new Date(timestamp*1000), 'D' ) );
alert( Globalize.format(12345.67, "n2") );
Globalize.cultureSelector = 'en';
alert( Globalize.format( new Date(timestamp*1000), 'D' ) );
alert( Globalize.format(12345.67, "n2") );
});
However, the Globalize library along with its culture files is pretty heavy-weight. You should only consider using them if you plan support for lots of languages and have various data types to format.
0
Code
i18n, JavaScript, jQuery
November 30th, 2011 at 4:12 pm
I recently discovered a very nice tool for GUI prototyping, called Pencil. It can be installed as a Firefox AddOn or as a standalone software. The Firefox XPI is unfortunately not in the Mozilla repositories which means you have to install it from their site, which also means no package verification and no automated upgrades.
Apart from that, Pencil is a very nice tool. It’s a very easy to use it allows to create interface prototyping in no time. Of course, you should know what wireframes are and what they are used for.
To demonstrate how quick you can create drafts for a website layout, let’s assume we are creating a booking site for events and we want to create a first draft of the home page. Here’s what you could get after 15 minutes sketching with Pencil. (Yes, that is Comic Sans MS.
As awkward as it sounds: this font is actually suitable for this sort of work.)

Of course, Pencil doesn’t offer the fancy features of elaborate drawing software like, for example, Inkscape. In fact, it doesn’t offer much more than a collection of common web UI elements which can be easily placed, resized, aligned and colored. The great thing about this is that you don’t need a tutorial or FAQ to get started – Pencil is completely self-explanatory.
It has multi-sheet documents, and you can copy/paste elements from one sheet to another one, which allows to quickly create variations of one screen. A variety of exporters (ODF, PDF, HTML) will help you to discuss your ideas with co-workers or customers.
Although Pencil still misses a number of essential features like guides, more shortcuts or better configurability, it is an ideal tool to put your ideas to paper.
0
Technology
GUI, rapid prototyping, wireframes
November 8th, 2011 at 12:30 pm
As a Gentoo user, you sometimes emerge packages which you don’t want to be in the world set, you just emerged them because you needed to fix or test something. (Of course, there’s the -1 flag, but you don’t always remember to use it.) With time, these packages become unneccessary, pull in useless dependencies and cause stupid conflicts. Therefore you should remove them from the @world set.
The @world set is stored in a plain text file located at /var/lib/portage/world. You should be aware that manipulating it may cause problems, therefore you’re advised to make a backup before editing. Also, you should do a full update of your system with emerge -uND world. Now you can open the world file in an editor of your choice, as root.
You may delete entries for packages you don’t need anymore, line by line. In order to actually remove those packages and their dependencies from your system, you must run depclean and revdep-rebuild:
emerge -a --depclean
revdep-rebuild -- -q --keep-going
After this, you will have a much leaner system.
Bonus hint: Also check your /etc/make.conf as well as your /etc/portage/package.use for no longer needed USE flags. These also tend to cause conflicts and stuff your system with needless dependencies. (Remember to run emerge -auND world; emerge -a --depclean; revdep-rebuild -- -q --keep-going afterwards.)
0
Code
bash, Gentoo
November 1st, 2011 at 11:56 am

Google’s + operator is appearently no longer supported.
For those who didn’t know, Google used to have a + operator which you could place directly in front of a word and which had two features: it would make sure that the word was included in every result, and it would make sure that only results with the exact spelling of that word would be returned. This feature is not the same as using the quote operator, because that one only enforces the exact spelling – but it does not make sure the word is included in each result.
Of course, not always both features were desired at the same time – for example, you may have wanted to make sure the word was included in the results, but you would have accepted alternative spellings. But even then, the + operator was be a nice addition for a search strategy.
I like search engines who provide advanced tools for fine-tuning searches. And this move of Google is one in a direction I don’t like.
0
Technology
Google, Search Engine
October 31st, 2011 at 1:44 pm
There are lots of URL redirecting services – all of them do the job. But if you still want to do redirects yourself from your own domain (especially if you have a short domain name
), you may want to have your own little service for that. And, it just feels nice to have the power over redirects in your own hands.
I have created such a service for this website, so have a try right away: lxg.de/r/eff.
The URL should be as short as possible. Also, you don’t want the URL shortener to conflict with your other web stuff. Therefore, you should create a subdirectory r/ in your document root and put a .htaccess file with the following content there.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /r/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /r/index.php [L]
</IfModule>
Now create the file index.php with the following content:
<?php
$req = preg_replace('|[^a-z0-9\-\_\.]|i', '', strrchr($_SERVER['REQUEST_URI'], '/'));
$list = file(__DIR__.'/list.php', FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
$url = '';
foreach ((array)$list as $line)
{
if (strpos($line, "$req\t") !== 0)
continue;
$url = trim(strrchr($line, "\t"));
break;
}
if ($url)
{
header("Location: $url", true, 302);
printf('If your browser doesn\'t redirect you, click here: <a href="%1$s">%1$s</a>.', $url);
}
else
{
echo 'Sorry, no redirect found. Please check the spelling of the URL.';
}
exit;
?>
The actual mapping of redirection identifiers to target URLs is in the file list.php:
<?php die('Error') ?>
eff http://www.eff.org/
aclu http://www.aclu.org/
The first line of this file is important to make sure that nobody can simply download the list (this is also why we use a PHP file and no plain TXT). The other lines consist of arbitrary identifiers and the target URLs, separated by one or more tab characters. The identifiers may contain latin letters, numbers, and the dot, minus and underscore characters. It may be advisable to make them human readable, but you may also optimize them for length.
0
Code
htaccess, PHP
September 7th, 2011 at 7:51 pm
Who else with a developer account from eBay has gotten more than five dedicated e-mails from eBay about their freakin’ confercence within the last days? Everybody? I thought so.
Ok, eBay: so you’re holding a conference on X.com, the new blabla of eCommerce, whatever. Send me one e-mail, ok. Send me two e-mails, well. But is your crappy conference selling so badly that you need to constantly spam everybody about it? – Yes? I thought so.
Goodbye, eBay. Hope you’re announcing that you’re going the way of all dinosaurs.
0
Business
eBay, spam
August 29th, 2011 at 9:57 pm
I’ve seen horrible things today. Somebody (the file didn’t carry an author … good for him or her) tried to get the first element of a PHP array – using array_shift. Of course, array_shift modifies the source array, so he worked on a copy of his original array, as (of course) he didn’t want to alter the array itself. Looked something like this:
$copyOfArray = $array;
$firstElementOfArray = array_shift($copyOfArray);
That is obviously a waste of memory and CPU cycles. Usually not noticable, of course. But with large arrays or when frequently executed, it will add stress to the system.
There’s a very “cheap” way to get the first value of any given array. The reset function does just that. (end returns the last element.)
$firstElementOfArray = reset($array);
Now, reset will set the internal array pointer to the first value. However, unless your doing crazy things or writing sloppy code, you shouldn’t care about that anyway.
0
Code
array, PHP