Skip to content

Instantly share code, notes, and snippets.

@mrclay
mrclay / teaful-sync.js
Last active August 8, 2022 03:20
Minimal teaful-like store using useSyncExternalStore()
import {useSyncExternalStore} from 'use-sync-external-store/shim';
const DOT = '.';
export default function createStore(defaultStore = {}) {
let allStore = defaultStore;
const listeners = new Set();
function subscribe(listener) {
listeners.add(listener);
@mrclay
mrclay / root-dir.sh
Last active May 2, 2022 23:58
Shell: Getting directory of code or active script
#!/bin/bash
# Execution-relative (all shells). Get dir path of the script executed
# by the user.
__0="$(CDPATH= cd -- $(dirname -- $0) && pwd -P)"
echo "$__0"/bar
# Code-relative (bash/zsh only). Get dir path of *this file*,
@mrclay
mrclay / ddev-build.sh
Created April 13, 2022 16:21
Allows inspecting docker builds in ddev projects
#!/bin/bash
# Like docker compose build [SERVICE].
#
# USAGE: ddev-build [SERVICE]
#
function ddev-build {
~/.ddev/bin/docker-compose -f .ddev/.ddev-docker-compose-full.yaml build $1 --no-cache --progress=plain
}
@mrclay
mrclay / svg-progress.html
Created November 11, 2021 19:05
Progress circle SVG with 0% at top
<body>
<svg viewBox="0 0 200 200" height="200">
<circle cx="100" cy="100" r="90" pathLength="1" transform="rotate(-90 100 100)" class="percentage" stroke-dashoffset="0.9"></circle>
</svg>
<p>
Set percentage:
<button type="button">0</button>
<button type="button">15</button>
<button type="button">50</button>
@mrclay
mrclay / README.md
Created May 20, 2021 17:17
Build set of JSON files for specifying US state grid map positions
type RGB = [number, number, number];
export function parseRgb(color: RGB | string): RGB {
if (Array.isArray(color)) {
return color;
}
let c = color;
let m;
if (
@mrclay
mrclay / flush-iptables.sh
Last active April 20, 2025 12:25
Flush IP tables and restart docker
#!/bin/bash
# Script is needed because my default firewall rules are messed up and after
# every restart, docker containers can't make connections to the host, notably
# preventing debuggers like xdebug from attaching.
# If networking fails in your containers but works in others, rm and re-create the
# docker network that container is bound to.
set -euo pipefail
@mrclay
mrclay / usePrevious.ts
Created May 5, 2020 15:05
usePrevious in TypeScript with init value
import { useEffect, useRef } from 'react';
export default function usePrevious<T, U>(value: T, init: U): T | U {
const ref = useRef<T | U>(init);
useEffect(() => {
ref.current = value;
});
return ref.current;
}
@mrclay
mrclay / exceljs-extras.js
Last active January 12, 2025 01:13
Convert Exceljs rich text values to HTML or plaintext strings
function isRichValue(value) {
return Boolean(value && Array.isArray(value.richText));
}
function richToString(rich) {
return rich.richText.map(({ text }) => text).join('');
}
function richToHtml(rich) {
let str = rich.richText.map(({ font = {}, text }) => {
@mrclay
mrclay / dclogs.sh
Last active September 16, 2022 13:38
docker compose logs -f but auto restart if all containers stop and come back up
#!/bin/bash
# Like docker compose logs -f but it waits for containers to come back up
# if they stop or haven't been started.
#
# USAGE: dclogs [SERVICE]
#
function dclogs {
# If in ddev project, use ddev's full config file
if [ -d ".ddev" ]; then