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
export default function (
Component: typeof SvelteComponent,
events?: { [svelteEvent: string]: string }
) {
return class {
$element: Element[];
initialProps: { [key: string]: any };
component: SvelteComponent;
constructor($element) {
'ngInject';
@dimfeld
dimfeld / mostVisibleElement.ts
Created May 28, 2020 19:25
Svelte IntersectionObserver action example
/**
* This is intended to be used with the svelte `use` syntax. It examines all
* child elements of the element that match `selector`, and calls the callback
* when the child element with the highest percentage overlap with the visible
* part of the container changes.
* <div use:mostVisibleElement={{ cb: setActive }}><section><section></div>
*/
export default function mostVisibleElement(
container: Element,
{ selector, cb }
@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 / slog-logging-middleware.rs
Last active August 27, 2021 04:50
Simple actix-web middleware for custom KV logging with slog
use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{ok, FutureResult};
use futures::{Future, Poll};
use slog::info;
// There are two step in middleware processing.
// 1. Middleware initialization, middleware factory get called with
// next service in chain as parameter.
// 2. Middleware's call method get called with normal request.
@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>