Skip to content

Instantly share code, notes, and snippets.

View dgraham's full-sized avatar
💭

David Graham dgraham

💭
View GitHub Profile
@dgraham
dgraham / git-cowork
Created September 8, 2023 23:18
git cowork
#!/bin/sh
if [ "$1" = "--stop" ]
then
git config --unset-all cowork.author
elif [ -n "$1" ]
then
author=$(git log --no-merges --author "$1" -1 --pretty="%an <%ae>")
if [ -n "$author" ]
then
@dgraham
dgraham / range-set.rb
Created November 12, 2021 23:55
Merge intersecting ranges
require "minitest/autorun"
class RangeSet
include Enumerable
def initialize(ranges = [])
@ranges = merge(ranges)
end
def <<(range)
@dgraham
dgraham / const-expr.c
Created April 18, 2017 00:52
Constant expression inlining optimization.
// Compile without optimizations:
// cc -S -O0 -masm=intel const-expr.c
// Compile with optimizations:
// cc -S -O3 -masm=intel const-expr.c
int main() {
int x = 11;
int y = 12;
int z = x + y;
return z;
}
@dgraham
dgraham / bench-function-pointer.c
Created April 15, 2017 18:27
Benchmark function pointer versus direct call.
#include <stdio.h>
#define MAX 100000000
int calculate() {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i;
}
return sum;
@dgraham
dgraham / osx-setup.sh
Last active April 6, 2024 17:18
A setup script for macOS development.
xcode-select --install
if [ ! -x /opt/homebrew/bin/brew ]; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew tap 'homebrew/bundle'
brew tap 'homebrew/services'
brew install automake clang-format cmake icu4c libressl libxml2 openssl pkg-config readline sqlite yajl
@dgraham
dgraham / delegated.js
Last active August 29, 2015 14:25
Simple delegated event handling.
(function() {
const events = new Map();
const stopped = new WeakMap();
function before(subject, verb, fn) {
const source = subject[verb];
subject[verb] = function() {
fn.apply(subject, arguments);
return source.apply(subject, arguments);
};
@dgraham
dgraham / copy-to-clipboard.js
Last active March 21, 2019 02:17
Copy attribute value, form input, or element text to the clipboard.
(function() {
function createNode(text) {
var node = document.createElement('pre');
node.style.width = '1px';
node.style.height = '1px';
node.style.position = 'fixed';
node.style.top = '5px';
node.textContent = text;
return node;
}
@dgraham
dgraham / make-icns
Created February 8, 2015 00:26
png -> icns
if [ $# -ne 1 ]; then
echo "Usage: make-icns icon.png"
exit 1
fi
IMAGE=$1
OUT=`basename ${IMAGE%\.*}`.iconset
mkdir $OUT
sizes=(16 32 128 256 512)
@dgraham
dgraham / anchor.js
Last active October 5, 2015 03:15
Simulate browsers' scroll-to-anchor behavior on page load.
(function() {
function hashchange() {
if (!location.hash) {
return;
}
// Don't do anything if the current target exists.
if (document.querySelector(":target")) {
return;
}
var name = "user-content-" + decodeURIComponent(location.hash.slice(1));
@dgraham
dgraham / fetch.js
Last active March 24, 2023 15:44
Simple window.fetch wrapper.
(function() {
function status(response) {
if (response.ok) {
return response
} else {
var error = new Error(response.statusText || response.status)
error.response = response
throw error
}
}