Skip to content

Instantly share code, notes, and snippets.

View szhu's full-sized avatar

Sean Zhu szhu

  • Google
  • NYC / SF
  • 02:54 (UTC -05:00)
  • X @sfzhu
View GitHub Profile
@szhu
szhu / preallocate.c
Created February 17, 2025 16:57
Create a file that takes up space, a little faster than using `mkfile`.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// Converts a size string (e.g., "20g", "500m", "1024") into bytes.
off_t parse_size(const char *sizeStr) {
char *end;
-- ~/.hammerspoon/init.lua
-- https://chatgpt.com/share/679c6282-322c-8007-9421-d7a0513d1a5f
hs.window.filter.default:subscribe(hs.window.filter.windowCreated, function(win)
if win:isStandard() and (win:tabCount() == nil or win:tabCount() <= 1) and win:isMaximizable() then
local screen = win:screen():frame()
local frame = win:frame()
frame.x = (screen.w - frame.w) / 2 + screen.x
frame.y = (screen.h - frame.h) / 2 + screen.y
win:setFrame(frame)
end
#!/usr/bin/env deno run --allow-env --allow-read --allow-run
// Was this file modified in any PR?
import { readline } from "https://deno.land/x/[email protected]/mod.ts";
declare global {
export const Deno: any;
export interface ArrayConstructor {
fromAsync<T>(asyncIterable: AsyncIterable<T>): Promise<T[]>;
}

Update!

I just found rclone, which seems to do what my script does, and more:

  • No need to set up sharing a particular way
  • Won't time out, you can just leave it running until it's done
  • Seems to run much much faster
  • Keeps modified times on files the same

The only drawback is that it takes a bit longer to setup.

type ItemInfo =
| {
type: "element";
key: string | undefined;
}
| { type: "other" };
function getItemInfo(item: unknown): ItemInfo {
if (typeof item === "object" && item != null) {
if ("key" in item) {
#!/opt/homebrew/bin/bun run
// <xbar.title>CPU Usage, Kill process</xbar.title>
// <xbar.version>v1.0</xbar.version>
// <xbar.author>Sean Zhu</xbar.author>
// <xbar.author.github>szhu</xbar.author.github>
// <xbar.desc>Shows the top 5 highest-CPU processes. Select a process to kill it.</xbar.desc>
// <xbar.image>https://raw.githubusercontent.com/Aleksandern/devman/master/images/BitBarCpuUsageKill.png</xbar.image>
// <xbar.dependencies>deno</xbar.dependencies>
// Based on: https://xbarapp.com/docs/plugins/System/cpu-usage-kill.5s.sh.html
@szhu
szhu / makeSetValue.ts
Created March 10, 2024 20:35
Immutably set a nested key on an object.
type PickByNullableType<D, T> = {
[K in keyof D]: NonNullable<D[K]> extends T ? K : never;
};
type PickKeysWithNullableValueTypes<D, T> = keyof D &
PickByNullableType<D, T>[keyof D];
function setLevel1Value<D extends object, K1 extends keyof D>(
data: D,
key1: K1,
# This does a dry run; it prints out what should be run. You can then run it yourself.
for branch in (git branch --format "%(refname:short)")
set sha (git rev-parse $branch)
if git log -1 $branch --pretty=format:'%cr' | grep -q -E 'weeks|months'
set date (git log -1 $branch --pretty=format:'%cs')
echo
echo '#' $branch
echo git tag $date-$branch $sha
echo git branch -D $branch
end
set -e
verbose() {
echo $ "$@" >&2
"$@"
}
COMPARISON="${1:-origin/prod...origin/main}"
OUT=".git/changelog.out.html"
import { useRef } from "react";
/**
* Use this hook to quickly answer the question: Why did my useEffect or useMemo
* callback run again?
*
* Usage:
*
* // If you have this code...
* useEffect(() => {