Skip to content

Instantly share code, notes, and snippets.

View kgn's full-sized avatar

David Keegan kgn

View GitHub Profile
@kgn
kgn / gist:738831
Created December 13, 2010 09:33
Display uti information for a file on osx
mdls
@kgn
kgn / Template.py
Created December 15, 2010 20:15
A simple solution for templates
import re
k_regex = re.compile('{{\s*(\w+)\s*}}')
k_template = '''
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{{content}}
@kgn
kgn / .gitconfig
Last active May 9, 2016 20:43 — forked from henrik/.bashrc
My user setup
[alias]
cl = clone --recursive
un = reset --hard
st = status -s
aa = add --all
ci = commit -m
co = checkout
cob = checkout -b
br = branch
rbr = branch -r
@kgn
kgn / jquery.elasout.js
Created January 6, 2011 07:24
jQuery elasout easing function that is condensed but passes JSLint
//borrowed from jQuery easing plugin
//http://gsgd.co.uk/sandbox/jquery.easing.php
$.easing.elasout = function(x, t, b, c, d){
var s=1.70158, p=0, a=c;
if(t===0){return b;}
if((t/=d)===1){return b+c;}
if(!p){p=d*0.3;}
if(a < Math.abs(c)){a=c; s=p/4;}else{s = p/(2*Math.PI) * Math.asin(c/a);}
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
};
@kgn
kgn / jquery.prettyDate.js
Created March 4, 2011 20:24
Nice date delta string: 3 minutes ago, 1 hour ago, 16 days ago
// modified from: http://ejohn.org/blog/javascript-pretty-date/
(function($){
$.prettyDate = function(time){
var date = new Date((time || ''));
var diff = (((new Date()).getTime() - date.getTime())/1000);
var day_diff = Math.floor(diff / 86400);
if(diff === 0 || isNaN(day_diff) || day_diff < 0){
return;
}
import sys
import os
import subprocess
def git(args, **kwargs):
environ = os.environ.copy()
if 'repo' in kwargs:
environ['GIT_DIR'] = kwargs['repo']
if 'work' in kwargs:
environ['GIT_WORK_TREE'] = kwargs['work']
@kgn
kgn / preload.js
Last active September 27, 2015 05:28
A javascript snippet that will preload all images defined in the styles sheets.
(function(){
var styles, matches, match, url, img = new Image(), loaded = [];
function forEach(a,c){if(a&&c){for(var i=0;i<a.length;++i){c.call(a[i])}}};
forEach(this.styleSheets, function(){
forEach(this.rules, function(){
forEach(styles=this.style, function(){
if(this.toString() !== 'background-image')return;
if(!(matches=styles[this].match(/url\([^\)]+\)/g)))return;
forEach(matches, function(){
if(!(match=this.match(/url\(([^\)]+)\)/)))return;
@kgn
kgn / gist:1465424
Created December 12, 2011 06:35
Drawing an NSDrawNinePartImage from a single NSImage
static NSImage *croppedImageWithRect(NSImage *image, NSRect rect){
NSImage *subImage = [[NSImage alloc] initWithSize:rect.size];
NSRect drawRect = NSZeroRect;
drawRect.size = rect.size;
[subImage lockFocus];
[image drawInRect:drawRect
fromRect:rect
operation:NSCompositeSourceOver
fraction:1.0f];
[subImage unlockFocus];
@kgn
kgn / gist:1484377
Created December 16, 2011 03:50
Correctly remove the space taken up by a menu bar item when the app is terminated.
- (void)applicationWillTerminate:(NSNotification *)notification{
[[NSStatusBar systemStatusBar] removeStatusItem:self.statusItem];
}
@kgn
kgn / gist:1558664
Created January 4, 2012 05:37
Animated NSTableView Scrolling
// Thanks to CuriousKea for getting this started!
// http://stackoverflow.com/a/8480325/239380
- (void)scrollRowToVisible:(NSInteger)rowIndex animate:(BOOL)animate{
if(animate){
NSRect rowRect = [self rectOfRow:rowIndex];
NSPoint scrollOrigin = rowRect.origin;
NSClipView *clipView = (NSClipView *)[self superview];
scrollOrigin.y += MAX(0, round((NSHeight(rowRect)-NSHeight(clipView.frame))*0.5f));
NSScrollView *scrollView = (NSScrollView *)[clipView superview];