Friday, December 23, 2011

Bash script can tell its location

You can get the path of the directory in which a bash script is located FROM that bash script.

dir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
view raw gistfile1.txt hosted with ❤ by GitHub

Read more about this problem on stackoverflow.

Thursday, December 22, 2011

Loading an agent on OSX

launchctl load -w ~/Library/LaunchAgents/[FILE].plist
view raw gistfile1.txt hosted with ❤ by GitHub

Tuesday, December 20, 2011

Print out more than 20 items in mongodb shell

When you're listing a large dataset in the shell you'll only see 20 items by default and a 'has more' text indicating that there are more.
If you want to change this limit you can use

DBQuery.shellBatchSize = 300

in the shell.

Saturday, December 17, 2011

Loading CDN hosted jQuery with local fallback

I found this piece of art in html5boilerplate.
It tries to load jQuery from a CDN and if it fails for whatever reason it tries a local one.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
view raw gistfile1.html hosted with ❤ by GitHub

Monday, December 12, 2011

Replace line break with comma in bash

# Inline
echo 'id1\nid2\nid3' | tr -s '\n\r' ','
# From file
cat ids_width_linebreaks.txt | tr -s '\n\r' ','
# From/to a file
tr -s '\n\r' ',' < ids_with_linebreaks.txt > ids_with_commas.txt
view raw gistfile1.txt hosted with ❤ by GitHub

Friday, December 9, 2011

Creating a custom google maps marker with canvas

Generating a custom marker with canvas is handy when you want to change the marker size/color dynamically.

The following code snippet contains a 'createMarker' function that creates a canvas, draws the marker graphics and returns back the Data URL encoded version of that canvas which can be used as an input at the marker creation.

See the example below.
<!DOCTYPE html>
<html>
<head>
<style>
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
margin: 0;
}
#map_canvas {
width: 100%;
height: 100%;
}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
<script>
function createMarker(width, height, radius) {
var canvas, context;
canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
context = canvas.getContext("2d");
context.clearRect(0,0,width,height);
// background is yellow
context.fillStyle = "rgba(255,255,0,1)";
// border is black
context.strokeStyle = "rgba(0,0,0,1)";
context.beginPath();
context.moveTo(radius, 0);
context.lineTo(width - radius, 0);
context.quadraticCurveTo(width, 0, width, radius);
context.lineTo(width, height - radius);
context.quadraticCurveTo(width, height, width - radius, height);
context.lineTo(radius, height);
context.quadraticCurveTo(0, height, 0, height - radius);
context.lineTo(0, radius);
context.quadraticCurveTo(0, 0, radius, 0);
context.closePath();
context.fill();
context.stroke();
return canvas.toDataURL();
}
// example
function initialize() {
// creating a map
var map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: 4,
center: new google.maps.LatLng(-25.363882,131.044922),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// creating a marker
var marker = new google.maps.Marker({
position: new google.maps.LatLng(-25.363882,131.044922),
map: map,
title:"Hello World!",
icon: createMarker(25, 25, 4)
});
}
</script>
</body>
</html>