Skip to content

Instantly share code, notes, and snippets.

@tilgovi
tilgovi / gist:1342375
Created November 6, 2011 02:30
Function building with templates and overloading
#include <iostream>
template <typename Ret, typename Arg>
class Prod {
private:
Ret (*left)(Arg);
Ret (*right)(Arg);
public:
Prod(Ret (*l)(Arg), Ret (*r)(Arg)) {
this->left = l;
@tilgovi
tilgovi / case.js
Created December 2, 2011 03:12 — forked from jhs/case.js
A better case statement?
// Prettier case statement, but can also return a value since it is an expression.
//
first_condition
? first_action()
: second_condition
? second_action()
: third_condition && can && (be || compound)
? (
can(),
@tilgovi
tilgovi / gist:2239638
Created March 29, 2012 16:40
virtualenv lifting
"""
If a virtual environment is active, make sure it's being used.
Globally-installed console scripts use an absolute shebang path which
prevents them from importing packages in the virtualenv.
"""
import os
import os.path
if 'VIRTUAL_ENV' in os.environ:
@tilgovi
tilgovi / README.md
Created June 20, 2012 23:09 — forked from mbostock/.block
Fisheye Grid

The above grid shows the effect of fisheye distortion. Move the mouse to change the focal point.

@tilgovi
tilgovi / export.py
Created August 25, 2012 07:57
Open Annotation export from okfn/annotator-store
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
import json
import jsonld
import pprint
import rdflib
import rdflib_jsonld
@tilgovi
tilgovi / post-receive
Created September 26, 2012 08:21
python git deploy
#!/bin/bash
while IFS=" " read -ra LINE; do
REF=${LINE[2]}
HEAD=`echo ${REF} | awk '{print $NF}' | awk -F/ '{print $NF}'`
GIT_WORK_TREE=~/${HEAD} git checkout -f ${REF}
source ~/${HEAD}/bin/activate
pip install -r ~/${HEAD}/requirements.txt
pip install ~/${HEAD}
deactivate
@tilgovi
tilgovi / README.md
Last active December 22, 2015 16:58
Getting back to a commit after stripping the git hash from `git describe --always` output.

Background

For Hypothes.is, I've been using git describes --always option for generating version numbers. However, Chrome apps can only have digits (and '.') in their version identifiers. For that reason, instead of ending up with 0.0.6-1371-g93b5d9d (which is what versioneer generates for me), I've been publishing extensions that strip off the git hash.

Problem

When something goes wrong in production, the clear question is "what git hash was this extension built from?"

Solution

Here's my bash script to recover a git hash from a version like "0.0.6-1371" (in our case this means 1371 commits since v0.0.6).

It's easy: use git rev-list to count the commits since the tag. Then, subtract from that the trailing count in our version number to get the difference. Finally, skip back that amount from HEAD (important: in topological order).

@tilgovi
tilgovi / center-window.el
Last active June 9, 2019 07:56
Center Emacs Windows
(defun balance-margins ()
"This function balances the margins of all windows on the selected
frame such that the first column and the fill column are the same
distance from the left and right edge, respectively."
(walk-windows
(lambda (window)
(let* ((total-width (window-total-width window))
(fringe-width (apply '+ (butlast (window-fringes window))))
(scroll-bar-width (window-scroll-bar-width window))
(divider-width (window-right-divider-width window))
@tilgovi
tilgovi / application.coffee
Last active August 29, 2015 14:00
Annotator selector extension pseudocode
selectorExtension = require('./selectorExtension')
CustomAnnotator = Annotator.extend(selectorExtension)
annotator = new CustomAnnotator(...)
// This is our interaction point
position = null;
$(document.body).on('mouseup', function (event) {
});
var annotator = Annotator.create({
onSelection: function (event) {
// We have to trigger this event ourselves.
// Could be as simple as: