Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
pesterhazy / reagent-ref-functions.clj
Last active January 19, 2023 11:31
Using ref functions with reagent
;; React supports "refs" as a way for a component to get a
;; handle to its children. Classically, refs were string-based.
;; Recent versions of React support callback attributes as a
;; more elegant variant of accessing DOM notes or components.
;;
;; This example uses a Form-3 component as per
;; https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components
;;
;; For callback refs, see React's documentation
;; https://facebook.github.io/react/docs/more-about-refs.html
@pesterhazy
pesterhazy / async-post-commit
Last active January 14, 2023 11:05
Run git post-commit hook asynchronously
#!/usr/bin/env bb
;; This script is written in babashka
;; brew install babashka
;; brew install terminal-notifier
(require '[babashka.process :refer [shell process]])
;; assuming that the linter is called my-linter
@pesterhazy
pesterhazy / open-in-emacs.sh
Created January 13, 2023 10:06
Open file in emacsclient with optional line number. Start emacs if it's not running yet
#!/bin/bash
result=0
if [[ $# == 1 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n "$1" || result=$?
elif [[ $# == 2 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n +"$2" "$1" || result=$?
else
echo Unexpected args
exit 1
@pesterhazy
pesterhazy / wrong-dictionary.md
Last active December 21, 2022 09:20
You're using the wrong dictionary!

John McPhee famously considered a dictionary his secret weapon. What's your secret weapon?

If like me you're not a native speaker of English, you've probably used bilingual dictionaries to learn about English words. But at a certain level of proficiency, you almost certainly want to use a monolingual dictionary instead. You want to look up words the way a native speaker would.

Here are some of my favorites:

@pesterhazy
pesterhazy / select.cljs
Last active December 10, 2022 03:17
Using react-select with reagent
;; in your project definition
;; [cljsjs/react-select "1.0.0-rc.1" :exclusions [cljsjs/react]]
;; See react-select documentation: https://github.com/JedWatson/react-select
(ns example.select
(:require [reagent.core :as r]
[cljsjs.react-select]))
(defn select
@pesterhazy
pesterhazy / emacs-backtrace.txt
Created November 30, 2022 13:43
(wrong-type-argument stringp (require . package))
Debugger entered--Lisp error: (wrong-type-argument stringp (require . package))
string-match("\\(\\`\\|/\\)ffap\\(\\.so\\|\\.dylib\\|\\.elc\\|\\.el\\)?\\(\\.gz..." (require . package))
load-history-filename-element("\\(\\`\\|/\\)ffap\\(\\.so\\|\\.dylib\\|\\.elc\\|\\.el\\)?\\(\\.gz...")
eval-after-load("ffap" #f(compiled-function () #<bytecode -0x1d7b6acf0da24a99>))
byte-code("\300\301\302\"\210\303\304\305\306\307DD\310\311\312\313\314&\7\210\303\315\305\306\316DD\317\311\312\313\314&\7\207" [eval-after-load "ffap" #f(compiled-function () #<bytecode -0x1d7b6acf0da24a99>) custom-declare-variable python-check-command funcall function #f(compiled-function () #<bytecode 0x142936da65c0a642>) "Command used to check a Python file." :type string :group python python-check-buffer-name #f(compiled-function () #<bytecode 0x1c554684a9cc8f92>) "Buffer name used for check commands."] 8)
autoload-do-load((autoload "python" "Major mode for editing Python files.\n\n\\{python-mod..." t nil) python-mode)
command-exe
@pesterhazy
pesterhazy / yikes
Last active November 29, 2022 19:25
Yikes! A safer git push
#!/usr/bin/env bash
#
# When using "git push", I frequently find myself overlooking git's
# dreaded 'Updates were rejected because the remote contains work that
# you do not have locally' error message.
#
# After I push, I don't typically wait around for the command to
# complete. So while I _think_ that my changes have been pushed to
# remote branch, in fact the push wasn't successful. Because the
# branch has changed in the meantime, I need to `git pull --merge` to
@pesterhazy
pesterhazy / git xargs.md
Last active November 25, 2022 09:11
git xargs: execute utility on files under source control

git xargs: execute utility on files in repository

Provides a new git command, git xargs, that runs an arbitrary shell command on all files under source control. Optionally you can specify a pathspec (such as a subpath or a glob expression), restricting the operation to a subset of the repository files.

As git xargs delegates the work to xargs(1), it supports all options provided by the version of xargs installed on your system.

Installation

@pesterhazy
pesterhazy / indexeddb-multiple-tabs.md
Created February 26, 2019 08:37
Offline-first browser apps and multiple tabs

Offline-first browser apps and multiple tabs

For offline-first operation, browser apps cache data in an IndexedDb database. When the user makes a change while offline, they persist the change optimistically. When connectivity is restored, changes are synced to the server.

This pattern is well established today. However, given the possibility of opening the app in multiple tabs at the same time, we seem to be faced with a dilemma:

  • We allow users to use the app and to make changes in multiple tabs at the same time. But then two "threads" are writing to the same shared resource at the same time. Co-ordinating writes seems to be difficult.

    To make things worse, while the localStorage API allows you to register a callback that fires whenever an item is changed outside the current tab, the IndexedDb API doesn't, at least not in a widely-available way.

@pesterhazy
pesterhazy / print-to-pdf.md
Last active October 2, 2022 15:43
Print web pages to PDF for readability

It's often desirable to print articles or blog posts to PDF for easier reading. In fact you're almost always better off reading a cleaned-up printout than a noisy HTML page on a backlit screen.

Printing the web without all the noise

Why?

Offline reading has fewer distractions; you can mark up the article with your own notes; and it's easier on the eyes. Active reading, underlinding and annotating improves comprehension and retention. If you read on the reMarkable tablet, as I do, then you don't to print to actual paper and save trees.

If an article is worth reading, it's worth printing and reading with a pen in your hand.