Skip to content

Instantly share code, notes, and snippets.

@samthor
samthor / AsyncHandler.java
Created May 30, 2012
Handler-compatible AsyncHandler which runs tasks on some other thread
View AsyncHandler.java
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 Jan 28, 2013
CastGroup, something that lets listeners subscribe to broadcast events. Built for the garage door; will be used to support hanging GETs on some event.
View broadcast.go
package main
import (
"log"
"time"
)
func main() {
c := NewCastGroup()
@samthor
samthor / js-compile.py
Last active Jul 28, 2018
Shell interface to Closure compiler (in Python)
View js-compile.py
#!/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 Aug 29, 2015
tool for merging git upstream
View merge-upstream.sh
#!/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 Apr 12, 2015
nanosecond mtime to console
View nano.c
// 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 Apr 13, 2015
library to run subprocesses in bash
View runner.sh
#!/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 Aug 29, 2015
renders svg or other images to mobile device sizes
View prepare-images.py
#!/usr/bin/env python
import os
import sys
SCALES = {
1: ['mdpi/', ''],
1.5: ['hdpi/'],
2: ['xxdpi/', '@2x'],
View validate-test.html
<!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 Jun 15, 2015
python counter helper
View counterdict.py
#!/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 Jul 21, 2016
Minimal gulpfile for building JS
View gulpfile.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