Skip to content

Instantly share code, notes, and snippets.

View samthor's full-sized avatar

Sam Thorogood samthor

View GitHub Profile
@samthor
samthor / code.js
Last active July 18, 2022 11:02
Async cancellable promises
// nb. This code is available in an ES module of Promise helpers, here:
// https://github.com/samthor/promises
// symbol returned to indicate that a call was cancelled
export const takeoverSymbol = Symbol('takeover');
/**
* Accepts a generator function, which yields Promises, and converts it to an async function
* that cancels any previous calls.
*/
@samthor
samthor / IDObserver.js
Created September 9, 2018 01:18
helper JS to observe $ changes
/**
* Raw IDObserver class. Use via default method below.
*/
export class IDObserver {
/**
* @param {!Node|!ShadowRoot} root
*/
constructor(root) {
@samthor
samthor / emscripten-metadata.js
Last active April 16, 2019 06:24
code to read emscripten_metadata section of Web Assembly files
function lebReader(array) {
const view = new Uint8Array(array);
let at = 0;
return () => {
if (at >= view.length) {
return -1;
}
let ret = 0;
@samthor
samthor / gstate
Last active June 17, 2019 01:57
Sam's git status replacement
#!/bin/bash
heading() {
# or "\e" on *nix
HEAD="\033[96m\033[1m"
DONE="\033[0m"
echo -e "${HEAD}${1}${DONE}"
}
# 0) find local git repo, or fail now
#!/bin/bash
# Useful bash functions. This is sourced by the environment file.
# These are available to scripts, but you shouldn't use them in scripts if you
# want them to be portable.
# Usage: pathremove /path/to/bin [PATH]
# Eg, to remove ~/bin from $PATH
# pathremove ~/bin PATH
function pathremove {
local IFS=':'
@samthor
samthor / crush
Last active June 6, 2019 05:00
Helpers to compress PNG images
#!/bin/bash
command_exists() {
type "$1" &> /dev/null
}
for X in "$@"; do
if [ ${X: -4} == '.png' ]; then
if command_exists zopflipng; then
zopflipng "$X" "$X"
@samthor
samthor / css-modules-plugin.mjs
Last active August 2, 2022 14:01
CSS Modules plugin for Rollup
import fs from 'fs';
// as per https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/master/CSSModules/v1Explainer.md
export default function cssModules() {
return {
name: 'css-modules',
async load(id) {
if (!id.endsWith('.css')) {
return;
@samthor
samthor / logcat
Last active January 29, 2020 03:29
Better logcat
#!/usr/bin/env python
import logging
import subprocess
import re
import sys
import datetime
RE_SPACES = re.compile('\s+')
let configured = false;
let finished = false;
const methodsToCall = [];
function setup() {
const Eleventy = require("@11ty/eleventy");
const originalWrite = Eleventy.prototype.write;
Eleventy.prototype.write = async function(...args) {
const out = await originalWrite.apply(this, args);
package loggerwrap
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"cloud.google.com/go/logging"