Skip to content

Instantly share code, notes, and snippets.

View stormbrew's full-sized avatar

stormbrew stormbrew

  • Edmonton, Alberta, Canada
View GitHub Profile
typedef int AA;
void foo()
{
AA AA; /* OK - define variable AA of type AA */
int BB = AA * 2; /* OK - AA is just a variable name here */
}
PS1_LASTEXIT='$(LE=$?; if [ ${LE} -ne 0 ]; then LED=$(perl -le "\$!+=${LE};print \$!"); echo "\[\e[31;1m\]^${LE}:${LED}\[\e[32;1m\]> "; fi)'
PS1_USERNAME="\[\e[37;1m\]\u\[\e[32;1m\]> "
PS1_JOBS='$(JB=$(jobs -l | wc -l); if [ ${JB} -gt 0 ]; then echo "\[\e[37;1m\]&\j\[\e[32;1m\]> "; fi)'
PS1_GIT='$(GPS=$(__git_ps1 %s); if [ -n "${GPS}" ]; then STAT=$(git status --porcelain | cut -c1-2 | sed -E "s/ /-/" | sort | uniq | xargs echo | cut --delimiter=" " -f1- --output-delimiter=","); echo "\[\e[37;1m\]git:${GPS}:\[\e[35;1m\]${STAT}\[\e[32;1m\]> "; fi)'
PS1_WD="\[\e[37;1m\]\w\[\e[32;1m\]> "
PS1_PROMPTNUM="\[\e[37;1m\]!\!\[\e[32;1m\]"
# \! in PS2 actually prints the next command number because the command so far has already been appended to the history.
# Instead, get it from fc, which can tell you the last command number.
PS2_PROMPTNUM="\[\e[37;1m\]!\$(fc -l -1 | head -1 | awk '{print \$1}')\[\e[32;1m\]"
#include <vector>
template <typename T>
void blah(T) {}
template <typename T>
std::vector<T> blorp(T) {
std::vector<T> x;
return x;
}
@stormbrew
stormbrew / cdo.sh
Last active December 27, 2015 10:29
# Changes to directory passed in $1 and runs the command specified
# by the remaining arguments.
function cdo() {
at=${1}
shift
( # enter a subshell to perform the action
cd "${at}"
"$@"
)
}
# Run this to get a default autocomplete that, if you press
# tab on an empty prompt it will expand to the first term of
# the last command you ran.
#
# Inspired by http://thechangelog.com/interactive-shell-git/
# where I don't really want to have to go in and out of a mode
# when I don't necessarily know it'll be worth it, but being
# able to skip typing git every command would be nice. This way
# you can go (using their example):
#
@stormbrew
stormbrew / .i3config
Created March 18, 2014 07:53
my i3wm.conf
# This file has been auto-generated by i3-config-wizard(1).
# It will not be overwritten, so edit it as you like.
#
# Should you change your keyboard layout somewhen, delete
# this file and re-run i3-config-wizard(1).
#
# i3 config file (v4)
#
# Please see http://i3wm.org/docs/userguide.html for a complete reference!
@stormbrew
stormbrew / test.cpp
Created March 21, 2014 12:05
So recent versions of C++ kind of let you do something obscene that's very reminiscent of ruby blocks...
template <type tNodeType, typename tLambda>
void when(tLambda func)
{
if (tNodeType == node_type) {
func(*reinterpret_cast<node<tNodeType>*>(&data));
}
}
// this is an obscene hack to let you do something like
// the lambda-using when above, but a bit cleaner if more
#!/usr/bin/env bash
set -o pipefail
# Find the repo, fail if we're not in a git repo or the origin has a weird name.
# TODO: Add other kinds of version control
# TODO: Make the remote name configurable with git config
repo_dir=$(git rev-parse --show-toplevel)
[[ $? != 0 ]] && { echo "Not in a git directory!"; exit $?; }
origin=$(git remote -v | egrep '^origin.+\(fetch\)' | awk '{print $2}')
[[ $? != 0 ]] && { echo "Couldn't find an origin."; exit $?; }
def destructive_merge(a,b)
res = []
while !a.empty? && !b.empty?
while !a.empty? && a.first < b.first
res << a.shift
end
a, b = b, a
end
res
end
@stormbrew
stormbrew / randombytes.php
Last active August 29, 2015 14:03
Twitter algorithmic code review
<?php
function getRandomRangedBytes($length, $max) {
// inspired by: http://stackoverflow.com/a/2999130/3803650
// This safely generates a series of random bytes that are all within
// the range [0,$max) with an even distribution
$divisor = intval(255/$max);
$results = array();
// This looks like it could be a loop that never terminates,
// but that would only be true if openssl was not returning us