Skip to content

Instantly share code, notes, and snippets.

@adam-zethraeus
adam-zethraeus / sanitize.sh
Last active August 16, 2023 08:04
sanitize file names, or sanitize recursively
#!/usr/bin/env bash
sanitize_node() {
shopt -s extglob
filename=$(basename "$1")
directory=$(dirname "$1")
if [ -f "$1" ]; then
extension=".${filename##*.}"
else
@adam-zethraeus
adam-zethraeus / Breakpoints.js
Last active July 25, 2023 14:21
media-query based conditional React components
import { useState, useEffect } from "react";
const makeObservable = (target) => {
let listeners = [];
let value = target;
const getter = () => {
return value;
};
@adam-zethraeus
adam-zethraeus / usePolling.js
Last active July 15, 2023 03:16
usePolling React hook
import { useEffect, useState } from "react";
const usePolling = (interval = null, pollingFunction, deps = []) => {
const [subscription, setSubscription] = useState(null);
useEffect(() => {
if (!!interval) {
const id = setInterval(pollingFunction, interval);
setSubscription(id);
}
return () => {
@adam-zethraeus
adam-zethraeus / useEventListener.js
Created July 15, 2023 03:14
useEventListener hook
import { useCallback, useEffect } from "react";
const useEventListener = (targetElement, eventType, callback, deps = []) => {
const callbackHandle = useCallback(callback, deps);
useEffect(() => {
targetElement.addEventListener(eventType, callbackHandle);
return () => {
targetElement.removeEventListener(eventType, callbackHandle);
@adam-zethraeus
adam-zethraeus / attribute-grid.css
Last active July 10, 2023 11:20
Attribute Grid — a simple 12 column css grid which uses html tag attributes (not classes).
html, body {
margin: 0;
padding: 0;
}
*[grid] {
box-sizing: border-box;
margin: 0 0;
padding: 0 0;
width: 100%;
}
@adam-zethraeus
adam-zethraeus / qr-wifi.sh
Last active June 25, 2023 20:56
Generate a WiFi-auto-connect qrcode
#!/usr/bin/env bash
usage() { echo "Usage: $0 [-o <output file>] [-n <wifi name>] [-p <password>] [-t <WPA|WEP>]" 1>&2; exit 1; }
while getopts ":n:p:t:o:h" param; do
case "${param}" in
n)
n="${OPTARG}"
;;
p)
@adam-zethraeus
adam-zethraeus / AnyEncodable.swift
Created June 13, 2023 21:57
AnyEncodable — a type erased Encodable wrapper
extension Encodable {
fileprivate func encode(to container: inout SingleValueEncodingContainer) throws {
try container.encode(self)
}
}
/// A wrapper allowing Encodable types to be encoded — even
/// when only references as an existential `any Encodable`.
///
/// source: https://forums.swift.org/t/how-to-encode-objects-of-unknown-type/12253/5
extension Optional {
func orThrow(_ error: (any Error)? = nil) throws -> Wrapped {
switch self {
case .none:
if let error {
throw error
} else {
throw OptionalUnwrappingError(Wrapped.self)
}
@adam-zethraeus
adam-zethraeus / clip.sh
Last active June 11, 2023 21:38
clip — mac clipboard pbcopy/pbpaste wrapper
#!/usr/bin/env bash
if [ -t 0 ]; then # if no stdin to clip...
if [ -z "$*" ]; then # if no args to clip, paste
# paste
pbpaste
else # if there are args, execute as command and copy output
eval "${*}" | pbcopy
fi
@adam-zethraeus
adam-zethraeus / .zshrc
Created June 11, 2023 21:17 — forked from tanzeeb/.zshrc
vi-mode cursors with zsh + starship
eval "$(starship init zsh)"
function _cursor_block() { echo -ne '\e[1 q' }
function _cursor_bar() { echo -ne '\e[3 q' }
function _cursor_beam() { echo -ne '\e[5 q' }
function zle-keymap-select zle-line-init
{
case $KEYMAP in
vicmd) _cursor_block;;