Skip to content

Instantly share code, notes, and snippets.

View brookinc's full-sized avatar

Brook McEachern brookinc

View GitHub Profile
@brookinc
brookinc / stashstaged.sh
Last active November 23, 2021 09:34
A script to `git stash` only the currently staged changes.
#!/usr/bin/env bash
# This script stashes the currently staged changes, and leaves everything else in the working directory as-is.
# (source: https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible/39644782#39644782)
# Prompt for the desired repo path
REPOPATH=
read -p "Enter the repo path, or press ENTER for current dir: " REPOPATH
# Read the desired stash description from the command line, or prompt the user for it if necessary
@brookinc
brookinc / gifsicle.bat
Last active June 14, 2018 18:41
An interactive batch file that simplifies resizing and compressing GIFs with gifsicle.
@echo off
set /p filename="Enter GIF filename (without .gif extension) to resize: "
set /p scale="Enter scale to resize by (eg. 0.5): "
set /p colors="Enter color palette size to use (between 2 and 256): "
@echo on
gifsicle-1.89-win64\gifsicle.exe --scale %scale% --colors %colors% -O3 --no-extensions %filename%.gif > %filename%-%scale%-%colors%.gif
@echo off
@brookinc
brookinc / -ECS.swift
Last active December 29, 2018 05:42
A simple Entity-Component System experiment in Swift.
import Foundation
#if swift(>=4.2)
// no shim necessary
#else
// add shims for swift-4.2-style random(in:) methods
// NOTE: unlike Swift 4.2's implementations, these versions are prone to modulo bias,
// are not cryptographically secure, and don't support the full range of Int64 values
#if os(Linux)
srandom(UInt32(time(nil)))
@brookinc
brookinc / -shell.swift
Last active December 29, 2018 05:43
A simple script to demonstrate some basics of Swift scripting.
#!/usr/bin/swift
// shell.swift
// A simple script to demonstrate some basics of Swift scripting.
import Foundation
// Docs: https://developer.apple.com/documentation/foundation
// ...and: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Status.md
extension CommandLine {
@brookinc
brookinc / .gitconfig
Last active February 22, 2023 16:16
Git config settings that I like to reuse.
[alias]
add-branch = "!f(){ git fetch origin $1 && git branch $1 FETCH_HEAD && git checkout $1; }; f"
alias = "!f(){ git config --global alias.$1 \"$2\"; }; f"
aliases = config --get-regexp ^alias\\.
aliasverbose = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -"
br = branch --contains
cherry-pick-external = "!f(){ git --git-dir=$1/.git format-patch -k -1 --stdout $2 | git am -3 -k; }; f"
cleanup = gc
cleanup-slow = gc --aggressive
curr-branch = symbolic-ref --short HEAD
@brookinc
brookinc / ArchiveInboxThreads.gs
Last active November 17, 2021 16:04
Google Apps Script that archives Gmail threads with a given label.
// ArchiveInboxThreads
//
// This Google Apps Script moves all threads containing any label in the labelsToArchive list out of the
// user's inbox, except for threads that also have a label from the labelsToPreserve list.
//
// If numInboxThreadsToProcess is set to a value greater than 0, the script will only examine that number
// of the most recent inbox threads when it runs; otherwise, it will examine every thread in the inbox.
//
// To use this script, visit https://script.google.com/home and click "New script",
// then paste this code into the text editor area and edit the "labelsToArchive" and "labelsToPreserve"
@brookinc
brookinc / ExifTag.swift
Created December 30, 2018 07:26
Apply macOS file system tags based on EXIF metadata.
#!/usr/bin/swift
// ExifTag.swift
// Scans files in the given directory and any subdirectories for EXIF metadata and applies
// macOS tags based on the results.
//
// Usage:
// swift ExifTag.swift [options] path
// or...
// chmod +x ExifTag.swift
@brookinc
brookinc / FormatCode.gs
Created June 27, 2019 01:14
Google Apps Script that adds a "Format Code" menu option for documents.
// Source: https://webapps.stackexchange.com/a/117682
// Docs: https://developers.google.com/apps-script/reference/document/
// Add new menu item
function onOpen() {
DocumentApp.getUi()
.createMenu('Styles')
.addItem('Format Code', 'formatCode')
.addToUi();
}
@brookinc
brookinc / PushBackEmplaceBack.cpp
Created July 31, 2019 17:15
Comparison of various push_back() / emplace_back() approaches.
// https://www.onlinegdb.com/online_c++_compiler
// https://stackoverflow.com/questions/35404932/stdvectoremplace-back-and-stdmove
// https://stackoverflow.com/questions/11572669/move-with-vectorpush-back
#include <iostream>
#include <utility>
#include <vector>
int main()
{
@brookinc
brookinc / ffmpeg-concat.zsh
Last active April 18, 2022 17:23
Script to concatenate a directory of mp4 files into one
#!/bin/zsh
# requires ffmpeg: https://ffmpeg.org/download.html
# clear the file list
echo "" > ./output.txt
# recursively find all mp4 files in the current directory and subdirectories
# (will preserve alphabetical order by full path)
for entry in ./**/*.mp4