Skip to content

Instantly share code, notes, and snippets.

@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 February 5, 2024 09:09
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 November 16, 2022 04:38
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
@mrclay
mrclay / fewest-meetings.js
Last active August 13, 2019 04:23
Exercise: From given list of meetings of varying lengths, find the shortest number of meetings that can fit in the day
// Recursively create all combinations of the remaining items--at least those
// that are valid and aren't longer than those we've tested so far.
function buildCombinations(remaining, combo, process) {
process.recordIteration();
for (let i = 0; i < remaining.length; i++) {
if (process.isDone()) {
// global short-circuit
return;
}
@mrclay
mrclay / ClassList.php
Created December 9, 2017 16:48
HTML class list based on DOMTokenList
<?php
namespace MrClay;
/**
* HTML class list based on DOMTokenList
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList
*/
class ClassList