Skip to content

Instantly share code, notes, and snippets.

@sstephenson
sstephenson / back_forward.js
Created December 13, 2010 21:55
How to detect whether a hash change came from the Back or Forward button
var detectBackOrForward = function(onBack, onForward) {
hashHistory = [window.location.hash];
historyLength = window.history.length;
return function() {
var hash = window.location.hash, length = window.history.length;
if (hashHistory.length && historyLength == length) {
if (hashHistory[hashHistory.length - 2] == hash) {
hashHistory = hashHistory.slice(0, -1);
onBack();
@sstephenson
sstephenson / Simple Encryption.md
Created April 11, 2013 23:48
Simple file/stream encryption using OpenSSL

Simple file/stream encryption using OpenSSL

Create and store a 512-byte random encryption key named secret:

$ mkkey secret

Encrypt the contents of file with the secret key and write it to file.enc:

$ encrypt secret < file > file.enc
#!/usr/bin/env bash
#
# Wraps curl with a custom-drawn progress bar. Use it just like curl:
#
# $ curl-progress -O http://example.com/file.tar.gz
# $ curl-progress http://example.com/file.tar.gz > file.tar.gz
#
# All arguments to the program are passed directly to curl. Define your
# custom progress bar in the `print_progress` function.
#
@sstephenson
sstephenson / gist:1120938
Created August 2, 2011 19:08
Quick guide to installing rbenv
# Clone rbenv into ~/.rbenv
git clone git@github.com:sstephenson/rbenv.git ~/.rbenv
# Add rbenv to your PATH
# NOTE: rbenv is *NOT* compatible with rvm, so you'll need to
# remove rvm from your profile if it's present. (This is because
# rvm overrides the `gem` command.)
echo 'export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile
exec $SHELL
#!/bin/sh
#
# Usage: git semver-tags [-p|--pre]
#
# Lists semver tags in the repository in order from newest to oldest.
#
# Useful for e.g. programmatically finding the latest release tag:
# `git semver-tags | head -n 1`.
#
# Tag names must be valid according to the SemVer 1.0.0 specification
@sstephenson
sstephenson / let.tcl
Created March 17, 2011 14:04
Lexical scoping in Tcl
#!/usr/bin/env TEST=1 tclsh
# Lexical scoping in Tcl
proc let {block args} {
try {
set captured_vars [uplevel [list capture [block args]]]
set all_var_names [uplevel {info vars}]
foreach var [block args] value $args {
uplevel [list catch [list unset $var]]
@sstephenson
sstephenson / gist:1143900
Created August 13, 2011 14:20
Using multiple versions of Rails without gemsets
# RubyGems has this functionality built-in. Just specify
# the particular version you want to use as the first argument
# of the command, surrounded by underscores.
$ gem install rails --version 3.0.9
...
$ gem install rails --pre
...
$ rbenv rehash
$ rails --version
From: Sam Stephenson <sstephenson@gmail.com>
Subject: Re: ruby-build hint
Date: Sun, 10 Feb 2013 12:25:56 -0600
To: Michał Papis <mpapis@gmail.com>
Hi Michał,
Thanks for the email.
You're right that `[[` is not POSIX sh-compliant. Nor are the following
@sstephenson
sstephenson / test_case.coffee
Created January 10, 2012 19:31
Tiny CoffeeScript testing
#!/usr/bin/env coffee
class TestCase
@run: (print, callback) ->
tests = (new this testName for testName in @getTestNames())
results = []
passed = true
print "1..#{tests.length}"
#!/usr/bin/env bash
# Reads lines from stdin in the format `VAR=value` and escapes
# them for the shell, prepending each line with `export`.
# Parameter substitution is allowed in `value` with `$VAR` and
# `${VAR}` syntax. You can escape `$` and `\` with a backslash.
sed \
-e "/^[ "$'\t'"]*[A-Za-z_][0-9A-Za-z_]*=/ !d" \
-e "s/'/'\\\\''/g" \