Skip to content

Instantly share code, notes, and snippets.

Avatar

Nathan Henrie n8henrie

View GitHub Profile
@n8henrie
n8henrie / Open in MacVim.js
Last active Apr 15, 2021
JXA to open finder selection in MacVim, or default to an empty buffer
View Open in MacVim.js
// For use in Hammerspoon via hs.osascript.javascriptFromFile
function run(argv) {
const finder = Application("Finder")
const currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true
let selection = finder.selection()
@n8henrie
n8henrie / permutations.rs
Last active Mar 24, 2021
Create all permutations from a collection
View permutations.rs
#![feature(test)]
/// My naive implementation based on
/// [this javascript implementation](https://gist.github.com/thebopshoobop/b9531ceb67faae0a48a9f4aadb1aed55#file-perms_recursive-js)
/// ([blog post here](https://medium.com/@rwillt/two-very-different-algorithms-for-generating-permutations-412e8cc0039c))
///
/// NB: Runs fine on stable rust, unstable only needed for the benchmarking
///
/// For comparison:
///
/// using [itertools](https://crates.io/crates/itertools):
@n8henrie
n8henrie / Make AppleTV Screensaver.applescript.js
Created Sep 15, 2020
Apple JXA script to create and populate an album with a random sample of favorite photos
View Make AppleTV Screensaver.applescript.js
// Apple JXA script to create and populate an album with a random sample of favorite photos
// Author: @n8henrie
// Rename from .applescript.js to .applescript (.js for GitHub syntax highlighting)
// https://stackoverflow.com/a/11935263/1588795
function getRandomSubarray(arr, size) {
var shuffled = arr.slice(0), i = arr.length, temp, index;
while (i--) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
View format_shell_cmd.py
#!/usr/bin/env python3
import argparse
import sys
import typing as t
__author__ = "Benjamin Kane"
__version__ = "0.1.0"
__doc__ = f"""
Pretty-print simple Bash command from one line of stdin
@n8henrie
n8henrie / congress.ipynb
Last active Dec 20, 2019
A quick look at congress bills in recent history by unified or divided congress
View congress.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View trash.py
#!/usr/bin/env python3
"""Send files to MacOS trash bin via applescript."""
import os
from pathlib import Path
import sys
import subprocess
from typing import Iterable, Union
def trash(paths: Iterable[Union[str, Path]]) -> int:
@n8henrie
n8henrie / mount-image.sh
Created May 6, 2019
Script to mount all partitions from an image file into a temporary directory
View mount-image.sh
#!/bin/bash
set -Eeuf -o pipefail
if [ ! "$(whoami)" = root ]; then
echo "Please run as root"
exit 1
fi
TMP="$(mktemp -d)"
@n8henrie
n8henrie / repair.sh
Last active Jul 7, 2021
Fix time machine backup
View repair.sh
#!/usr/bin/env bash
# http://tonylawrence.com/post/unix/fixing-corrupted-time-machine-backups/
set -euf -o pipefail
set -x
if [[ $(whoami) != 'root' ]]; then
echo "Please run as root" > /dev/stderr
exit 1
fi
@n8henrie
n8henrie / setup_linux.sh
Last active Nov 10, 2017
Set up basic Linux preferences and settings
View setup_linux.sh
#! /bin/bash
echo '# Begin n8henrie settings' >> "${HOME}"/.inputrc
echo '" Begin n8henrie settings' >> "${HOME}"/.vimrc
echo '# Begin n8henrie settings' >> "${HOME}"/.bash_aliases
echo '# Begin n8henrie settings' >> "${HOME}"/.bashrc
curl 'https://gist.githubusercontent.com/n8henrie/c69de3e3c9d99668965473d1d315c855/raw' >> "${HOME}"/.inputrc
curl 'https://gist.githubusercontent.com/n8henrie/3e251bfe9ac9d5ce7421/raw' >> "${HOME}"/.vimrc
curl 'https://gist.githubusercontent.com/n8henrie/6cf7785d0ae025e706522e6e64c3fba2/raw' >> "${HOME}"/.bash_aliases
@n8henrie
n8henrie / .bashrc
Last active Oct 31, 2019
My basic .bashrc for Linux systems
View .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# Reset path (or else it gets longer each time this is sourced)
export PATH=$(getconf PATH)
# http://unix.stackexchange.com/questions/40678/can-i-make-there-are-stopped-jobs-harder-to-kill
prompt_command() {
job_count=$(jobs | wc -l | tr -d ' ')
if [ $job_count -gt 0 ] ; then
prompt_job="[$job_count]"