Skip to content

Instantly share code, notes, and snippets.

View raven-rock's full-sized avatar

Levi Gillette raven-rock

View GitHub Profile
@raven-rock
raven-rock / myguard.rb
Created May 16, 2017 19:23
myguard.rb - a simple command line tool that acts like Guard
#!/usr/bin/env ruby
#
# a simple command line tool that acts like Guard, but you can run any arbitrary command on any set of arbitrary files that change.
#
# Example usage:
#
# # Run test when a certain 3 files are modified.
# myguard.rb "ruby test_foo.rb" foo.rb foo_test.rb foo.json
#
@raven-rock
raven-rock / ls.zsh
Created May 19, 2017 17:09
Smart ls color flag for your .zshrc/.bashrc that works on Mac or Linux
# Detect which `ls` flavor is in use (Mac or Linux)
if ls --color > /dev/null 2>&1; then
ls_color_flag="--color" # GNU/Linux
else
ls_color_flag="-G" # BSD/macOS
fi
# Always show color when interactive
alias ls="ls ${ls_color_flag}"
@raven-rock
raven-rock / inclusive_range.clj
Created November 30, 2017 09:29
Clojure inclusive-range function
(defn inclusive-range
"Like range, but STOP (when defined) is inclusive. Leads to simpler code
without all the inc'ing etc. E.g.,
(inclusive-range 1 5) #=> (1 2 3 4 5)"
([] (range))
([stop] (range (inc stop)))
([start stop] (range start (inc stop)))
([start stop step] (range start (inc stop) step)))
@raven-rock
raven-rock / xclip.rb
Last active June 21, 2018 14:08
Ruby integration with xclip. Good for monkeypatching .pryrc. This is great for Windows clipboard integration using Putty and VcXsrv.
def pbpaste
IO.popen(["xclip", "-o"]).read
end
alias paste pbpaste
class Object
def pbcopy
Thread.new do
@raven-rock
raven-rock / edit-command-line-in-editor.zsh
Created July 31, 2018 13:18
Zsh: Use `C-x C-e` or `C-x e` to edit current command in $EDITOR with multi-line support. Saving and quitting $EDITOR returns to command prompt with the edited command inserted (but does not execute it until you press enter). Handy!
autoload -U edit-command-line
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line
@raven-rock
raven-rock / redshift-week-starting-sunday.sql
Created October 12, 2018 14:25
Redshift: derive week starting Sunday date from a date or timestamp value
select
(date_col - (extract(dow from date_col) || ' day')::interval)::date as week_of_starting_sunday
from
mytable
...
@raven-rock
raven-rock / Makefile
Created October 12, 2018 15:09
Makefile example (extracting datasets from PostgreSQL/Redshift)
# Example Makefile
#
# Format:
# [target]: [dep1 [dep2 ...]]
# <tab>command(s) that build target
#
# Be sure to use actual tab characters for indentation before commands.
#
# Cheat sheet:
# $@ -> refers to the current target
@raven-rock
raven-rock / install-current-release-of-ripgrep-from-source.sh
Created November 7, 2018 17:15
Install current release of ripgrep from source
set -ex
sudo apt update
sudo apt install -y cargo
cd ~/repos
git clone --depth=1 https://github.com/BurntSushi/ripgrep
cd ripgrep
cargo build --release
./target/release/rg --version
sudo ln -s $PWD/target/release/rg /usr/local/bin/rg
@raven-rock
raven-rock / sqlite-now-mem.rb
Created December 28, 2018 20:07
Load TSV files into a memory-only or on-disk SQLite database, optionally indexing specified columns
#!/usr/bin/env ruby
# The goal here is a script that can be called like this:
#
# sqlite-now-mem users.tsv,users,id,login,email products.tsv,products,id,name
#
# For each comma-separated tuple passed are arguments, the input file
# will be loaded into the table, and any following values are the
# names of columns for which indexes will be created.
#
@raven-rock
raven-rock / mysql-nullify
Created January 2, 2019 14:59
My suite of TSV helper tools for working with PostgreSQL/Redshift, MySQL, SQLite
#!/usr/bin/env perl
use strict;
use File::Basename qw(basename);
my $PROGRAM = basename($0);
if ($ARGV[0] =~ /^(?:-h|--help)$/) {
print "Turn blanks from TSV input lines into \\N for mysqlimport consumption.\n";
print "Usage examples:\n";