Skip to content

Instantly share code, notes, and snippets.

View samthor's full-sized avatar

Sam Thorogood samthor

View GitHub Profile
@samthor
samthor / AsyncHandler.java
Created May 30, 2012 14:12
Handler-compatible AsyncHandler which runs tasks on some other thread
package foo;
import android.os.Handler;
import android.os.HandlerThread;
/**
* Subclass of {@link Handler} that does things on some background thread.
*
* @author Sam Thorogood (sam.thorogood@gmail.com)
*/
@samthor
samthor / broadcast.go
Created January 28, 2013 14:32
CastGroup, something that lets listeners subscribe to broadcast events. Built for the garage door; will be used to support hanging GETs on some event.
package main
import (
"log"
"time"
)
func main() {
c := NewCastGroup()
@samthor
samthor / js-compile.py
Last active August 19, 2023 09:26
Shell interface to Closure compiler (in Python)
#!/usr/bin/env python
import json
import sys
try: # Py3
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import Request
except ImportError: # Py2
@samthor
samthor / merge-upstream.sh
Last active August 29, 2015 14:08
tool for merging git upstream
#!/bin/bash
git status 1>/dev/null || exit 1
git fetch upstream
if [ "$?" != "0" ]; then
echo >&2
echo "fatal: no upstream remote configured" >&2
ORIGIN=$(git remote -v | grep ^origin | cut -f2 | cut -f1 -d' ' | head -n 1)
@samthor
samthor / nano.c
Created April 12, 2015 00:52
nanosecond mtime to console
// nano.c: writes nanosecond mtime of first arg to console
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#define SEC_TO_NANO 1000000000
@samthor
samthor / runner.sh
Created April 13, 2015 00:04
library to run subprocesses in bash
#!/bin/bash
# Library which causes a bash script to block until killed, shutting down its
# child processes when complete.
function _death {
echo "killing all subprocesses" 1>&2
pkill -P $$
exit 0 # still need to quit
}
trap _death SIGINT SIGTERM
@samthor
samthor / prepare-images.py
Last active August 29, 2015 14:20
renders svg or other images to mobile device sizes
#!/usr/bin/env python
import os
import sys
SCALES = {
1: ['mdpi/', ''],
1.5: ['hdpi/'],
2: ['xxdpi/', '@2x'],
@samthor
samthor / validate-test.html
Created May 22, 2015 23:29
validation test
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,400,400italic" rel="stylesheet" type="text/css" />
<style type="text/css">
.formrow {
min-height: 32px;
line-height: 40px;
@samthor
samthor / counterdict.py
Created June 15, 2015 07:41
python counter helper
#!/usr/bin/env python3
import collections
class counterdict(collections.defaultdict):
def __init__(self):
collections.defaultdict.__init__(self, counterdict)
def __sub__(self, value):
"""Upgrade this counter to a number.
@samthor
samthor / gulpfile.js
Created July 21, 2016 01:53
Minimal gulpfile for building JS
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software