Skip to content

Instantly share code, notes, and snippets.

@swissmanu
swissmanu / zephyros.rb
Created August 14, 2013 08:52
a quck'n'dirty prototype for chaining more than one command to a key/modifier combination for the zephyros window manager.
require '/Applications/Zephyros.app/Contents/Resources/libs/zephyros.rb'
require 'time'
@last_time = Time.now - 10
@times = 0
##
# Simply returns a Proc instance with the passed block.
def command
Proc.new
@swissmanu
swissmanu / gist:6110904
Last active December 20, 2015 09:49
load a font from a custom ios/mac resource .bundle-file. via: http://stackoverflow.com/a/15510259/368959
- (void) loadCustomFont:(NSString*)fontFileName ofType:(NSString*)extension bundle:(NSBundle*)bundle {
NSString *fontPath = [bundle pathForResource:fontFileName ofType:extension];
NSData *inData = [NSData dataWithContentsOfFile:fontPath];
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
@swissmanu
swissmanu / create.sh
Last active December 17, 2015 15:09
create.sh creates a git repository which automatically publishs its contents over a webserver when receiving changes from its remotes. It's like gh-pages, but on your own server and without an extra branch.
#!/bin/bash
if [ -z "$1" ]; then
echo "Supply a name for the repo/site to publish"
exit 1;
fi
URLROOT=gitpages.mydomain.com
ROOT=/home/www/gitpages.mydomain.com
REPOS=$ROOT/repositories
@swissmanu
swissmanu / templatePrecompiler.js
Last active December 16, 2015 18:58
Creates precompiled handlebar templates and makes them available as a basic common js module. Change the paths templatesSource and templatesDestination to your needs. Simply execute with "node templatePrecompiler.js"
/** File: Template Preprocessor
* Creates precompiled handlebar templates and makes them available as a simple
* common js module.
*/
var Handlebars = require('handlebars')
, _ = require('underscore')
, path = require('path')
, fs = require('fs')
, templatesSource = path.join(__dirname, 'src', 'templates')
, templatesDestination = path.join(__dirname, 'src', 'templates', 'precompiledTemplates.js')
@swissmanu
swissmanu / gist:5382296
Created April 14, 2013 11:00
simple startup script for sabnzbd, couchpotato and sickbeard on a synology nas.
#!/bin/sh
SABNZBD_CONFIG=/root/.sabnzbd/sabnzbd.ini
SABNZBD_HOST=--HOST_ADDRESS--
SABNZBD_PORT=9200
SABNZBD_APIKEY=--YOUR_API_KEY--
CP_PID=/var/run/couchpotato.pid
CP_CONFIG=/usr/local/couchpotato.config.ini
CP_DATADIR=/root/.couchpotato
@swissmanu
swissmanu / gist:4943356
Created February 13, 2013 09:38
check if a CLLocationCoordinate2D is inside MKCoordinateRegion. this is a simplified version of http://stackoverflow.com/a/10574327/368959
-(BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate insideRegion:(MKCoordinateRegion)region {
CLLocationCoordinate2D center = region.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude = center.latitude - (region.span.latitudeDelta / 2.0);
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude = center.latitude + (region.span.latitudeDelta / 2.0);
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);
return(coordinate.latitude >= northWestCorner.latitude &&
@swissmanu
swissmanu / NSArray+ExtractValuesAsArray.h
Last active December 12, 2015 07:38
Create a NSArray with a specific value of the items of another NSArray.
@interface NSArray (ExtractValuesAsArray)
/** Creates a new NSArray with the specific value of the items of another array. Ensure that the items of the array are KVC compliant since a key path is used to gather the values.
*/
+(NSArray*)arrayWithValuesOfArrayItems:(NSArray*)array keyPath:(NSString*)keyPath;
@end
@swissmanu
swissmanu / gist:4682477
Created January 31, 2013 12:18
Following functions make the the creation and drawing of programmatical generated UIImages a little bit easier. Call `CreateBitmapContext(CGSize)` to get a CGContextRef you can draw into. Afterwards, just call `CreateUIImageFromBitmap(CGContextRef)` to return a common usable UIImage instance and free up any used resources. This Gist is based on h…
CGContextRef CreateBitmapContext(CGSize size) {
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
return ctx;
}
UIImage* CreateUIImageFromBitmapContext(CGContextRef ctx) {
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
@swissmanu
swissmanu / gist:4663178
Last active December 11, 2015 21:29
A global helper function to draw a vertical gradient using CoreGraphics. Pass a NSArray with UIColors and a plain C-array with step-location for each color. Don't invoke too often and when, don't use to many color steps. Each color needs to be converted using an expensive for-iteration.
// NSArray *colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
// CGFloat locations[] = {0,.5,1};
// DrawVerticalGradient(rect, colors, locations, ctx);
void DrawVerticalGradient(CGRect rect, NSArray* colors, const CGFloat steps[], CGContextRef ctx) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:colors.count];
for (UIColor *color in colors) {
[cgColors addObject:(__bridge id)(color.CGColor)];
}
@swissmanu
swissmanu / Helpers.h
Last active December 11, 2015 04:29
Assuming you have a crappy model which contains properties with a locale identifier as suffix (like "title_de", "title_en" etc), `localizedProperty(NSString*, id)` returns a specific translation of that property. This Gist provides a global function and is ready to use :-)
NSString* localizedProperty(NSString* propertyName, id fromObject);