Skip to content

Instantly share code, notes, and snippets.

View dimfeld's full-sized avatar

Daniel Imfeld dimfeld

View GitHub Profile
@dimfeld
dimfeld / switch-win.zsh
Created November 15, 2023 22:09
Quickly create and switch between Kitty os windows by name
sw() {
if [[ -n "$1" ]]; then
SWITCH_WIN=$(kitten @ ls | jq ".[] | select(.wm_name == \"$1\") | .tabs | .[]| select(.is_active) | .windows |.[]| select(.is_active) | .id" | head -n1)
if [[ -n "$SWITCH_WIN" ]]; then
kitten @ focus-window -m "id:${SWITCH_WIN}"
else
kitten @ launch --type os-window \
--os-window-title "kitty: $1" \
--os-window-name "$1"
fi
@dimfeld
dimfeld / retry.rs
Created November 25, 2021 13:37
Simple Rust Retry Code
use bytes::Bytes;
use futures::Future;
pub async fn retry_with_data<F, Fut, V, E>(data: Vec<Bytes>, f: F) -> Result<V, E>
where
F: Fn(Vec<Result<Bytes, std::io::Error>>) -> Fut,
Fut: Future<Output = Result<V, E>>,
{
retry(|| async {
let data = data
@dimfeld
dimfeld / parse_line.rs
Last active January 18, 2021 00:11
nom parser that parse a block of text containing a mix of plaintext and directives
/// Parse a line of text, counting anything that doesn't match a directive as plain text.
fn parse_inline(input: &str) -> IResult<&str, Vec<Expression>> {
let mut output = Vec::new();
let mut current_input = input;
while !current_input.is_empty() {
let mut found_directive = false;
for (current_index, _) in current_input.char_indices() {
// println!("{} {}", current_index, current_input);
@dimfeld
dimfeld / index.js
Last active January 5, 2021 00:17
Rush Dev Dependencies Runner
#!/usr/bin/env node
const rushLib = require('@microsoft/rush-lib');
const execa = require('execa');
const path = require('path');
const { default: Dag } = require('dag-map');
const { Transform } = require('stream');
const { EventEmitter } = require('events');
const chalk = require('chalk');
const yargs = require('yargs');
@dimfeld
dimfeld / build-global-css.mjs
Created December 18, 2020 21:36
Tailwind "Global" CSS builder adapted from @babichjacob
import fs from 'fs';
import postcss from 'postcss';
const { readFile, unlink, writeFile } = fs.promises;
const main = async () => {
let [sourcemap, postcssConfigPath, input, output] = process.argv.slice(2);
let { default: postcssConfig } = await import(postcssConfigPath);
if (sourcemap === 'true') sourcemap = true;
<script lang="typescript">
import debounce from 'just-debounce-it';
import compare from 'just-compare';
import type {
LoadableModel,
LoadableScenario,
Value,
} from '@carevoyance/reactive-data';
import type { Graph } from '@carevoyance/reactive-data';
import {
@dimfeld
dimfeld / App.svelte
Last active November 19, 2020 22:16
An abbreviated version of my tabs components
<Tabs>
<Tab id="settings" name="Settings">Content</Tab>
<Tab id="sharing" name="Sharing">Content2</Tab>
</Tabs>
@dimfeld
dimfeld / Tooltip.svelte
Created November 19, 2020 00:13
Layercake Tooltip
<script>
import { getContext } from 'svelte';
export let evt = undefined;
export let offset = 35;
export let headerFn = undefined;
export let nestedAccessors = false;
export let dimensions;
let { custom } = getContext('LayerCake');
</script>

The "each Leaflet object as a Svelte component" thing wasn't the first I tried. My first attempt manages the state manually with a process similar to what I described here: https://imfeld.dev/writing/leaflet_with_svelte#syncing-local-state-to-the-map-through-reactive-statements. Essentially I had some code blocks that keep track of which objects I had created in Leaflet, and manually reconciled that with the declarative state from the reactive statements, adding or removing markers/lines/etc. I had a code block like this for each type of object that the app used.

// A Map containing all the markers that we have created.
let markers = new Map();
let allLocations = {...};

// Create the markers.
for (let [id, data] of Object.entries(allLocations)) {
  let marker = L.marker(data.location, { icon: data.icon });
@dimfeld
dimfeld / just.d.ts
Last active October 28, 2020 02:38
Type Definitions for some just-* packages
// See https://github.com/angus-c/just for the functions themselves.
declare module 'just-capitalize' {
function capitalize(value: string): string;
export = capitalize;
}
declare module 'just-clamp' {
function clamp(min: number, value: number, max: number): number;
export = clamp;