I’ve come across multiple neat tools. Given all of my configs are literate programming configs, this sets up those scripts. Otherwise, I’d either not have those tools, or I’d have two processes for getting my dotfiles setup.

snapshot (~/bin/snapshot)#

A simple screenshot tool. Uses screencapture on macOS or imagemagick’s import on Linux.

#!/bin/bash
name=$1
if [[ "$name" != *"png"* ]]; then
    name="$name.png"
fi

if command -v screencapture &>/dev/null; then
    screencapture -i ~/Desktop/$name
else
    import ~/Desktop/$name
fi

Copy-pasta $ (~/bin/$)#

Allow copying things from the internet which have a preceding $.

#!/usr/bin/env bash
exec "$@"

timestamp conversion (~/bin/epoch)#

I need to convert epochs all the time from log diving. Thanks to Aaron Flatten for the script.

#!/bin/bash
# > epoch # Return the current timestamp
# > epoch <number> # Convert the numeric unix timestamp to a readable date string
# > epoch <str> # Converts the readable date string to a timestamp

if [ $# -eq 0 ]; then
  date +%s
elif [[ $1 == *[-T:Z]* ]]; then
  python3 -c "from datetime import datetime; import sys; print(int(datetime.fromisoformat(sys.argv[1].replace('Z','+00:00')).timestamp()))" "$1"
else
  VAL=$1
  if [[ $1 -gt 1000000000000 ]]; then
    VAL=$(( $1 / 1000 ))
  fi
  # macOS date -r, falling back to GNU date -d
  date -r "$VAL" 2>/dev/null || date -d "UTC 1970-01-01 $VAL secs" 2>/dev/null
fi

git-churn (~/bin/git-churn)#

When doing some code analysis suggested by the talk Sandi Metz gave at DeconstructConf 2018, I needed to figure out how to calculate the churn of a particular file. This script helps in that regard.

#!/bin/bash
# Written by Corey Haines
# Scriptified by Gary Bernhardt
#
# Put this anywhere on your $PATH (~/bin is recommended). Then git will see it
# and you'll be able to do `git churn`.
#
# Show churn for whole repo:
#   $ git churn
#
# Show churn for specific directories:
#   $ git churn app lib
#
# Show churn for a time range:
#   $ git churn --since='1 month ago'
#
# (These are all standard arguments to `git log`.)

set -e
git log --all -M -C --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort -n | awk 'BEGIN {print "count\tfile"} {print $1 "\t" $2}'