Skip to content

Instantly share code, notes, and snippets.

View bgreenlee's full-sized avatar

Brad Greenlee bgreenlee

View GitHub Profile
@bgreenlee
bgreenlee / random_gif.sh
Last active August 29, 2015 14:27
Post a random gif (with random icon) to a Slack channel
HOOK_URL=https://hooks.slack.com/services/.../.../...
curl -X POST --data-urlencode "payload={\"channel\": \"#random\", \"username\": \"randombot\", \"text\": \"$(curl -s 'http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC' | sed -nE 's/^.*image_original_url":"([^"]+)".*$/\1/gp' | sed 's/\\//g')\", \"icon_emoji\": \":$(curl -s http://www.emoji-cheat-sheet.com/ | sed -n -E 's/^.*<span class="name".*>(.*)<\/span>.*$/\1/gp' | perl -MList::Util -e 'print List::Util::shuffle <>' | head -1):\"}" $HOOK_URL
@bgreenlee
bgreenlee / flashScreen.swift
Created April 1, 2015 23:31
Flashing the screen in Swift. Sticking this here because it took me a while to work out the right variable types and casting.
func flashScreen() {
let inDuration: CGDisplayFadeInterval = 0.5
let outDuration: CGDisplayFadeInterval = 0.5
let color = NSColor.redColor()
var fadeToken: CGDisplayFadeReservationToken = 0
let colorToUse = color.colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
let err = CGAcquireDisplayFadeReservation(inDuration + outDuration, &fadeToken)
if Int(err) != Int(kCGErrorSuccess.value) {
NSLog("Error acquiring fade reservation")
@bgreenlee
bgreenlee / slack.go
Last active August 29, 2015 14:10
Bare-bones slash command handler for Slack
package main
/**
* Bare-bones slash command handler for Slack
*
* Commands:
* /weather [city] - Return the current weather for the given city
*/
import (
@bgreenlee
bgreenlee / levenshtein.swift
Created June 27, 2014 05:54
Levenshtein Distance in Swift
/**
* Levenshtein edit distance calculator
* Usage: levenstein <string> <string>
*
* To compile:
* sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
* xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) levenshtein.swift
*/
import Foundation
@bgreenlee
bgreenlee / loc2tz.py
Last active August 29, 2015 14:02
Convert a location name to tab-separated place name, lat, lon, and time zone. Adapted from http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html
#!/usr/bin/env python
# Convert a location name to tab-separated place name, lat, lon, and time zone.
# Adapted from http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html
#
# Installation:
# 1. Get a free Geonames account: http://www.geonames.org/login
# Note: after you confirm your account, you'll need to enable it to use the
# free web service at http://www.geonames.org/manageaccount
# 2. Install/download geopy and geonames libraries:

Keybase proof

I hereby claim:

  • I am bgreenlee on github.
  • I am bgreenlee (https://keybase.io/bgreenlee) on keybase.
  • I have a public key whose fingerprint is C683 EE62 E928 D6F9 EEB7 9EB3 5545 85A5 A54D 1F04

To claim this, I am signing this object:

@bgreenlee
bgreenlee / boggler.rb
Created February 11, 2014 23:13
Rather inefficient Boggle puzzle solver
#!/usr/bin/env ruby
# Boggle(tm) puzzle solver
# by Brad Greenlee <brad@footle.org>
# (Boggle is a trademark of Hasbro Inc., who has no affiliation with this
# program and hopefully couldn't care less about its existence.)
BOARD_SIZE = 4
MAX_WORD_SIZE = 12
MIN_WORD_SIZE = 3
INSERT INTO 'snippets' ('title', 'body') VALUES
(':+1:', '👍'),
(':-1:', '👎'),
(':100:', '💯'),
(':1234:', '🔢'),
(':8ball:', '🎱'),
(':a:', '🅰'),
(':ab:', '🆎'),
(':abc:', '🔤'),
(':abcd:', '🔡'),
@bgreenlee
bgreenlee / gist:5305273
Last active December 15, 2015 18:39
Coalesce a list of overlapping start/end tuples
val ranges = List((2,3), (1,2), (5,11), (4,10), (3,3), (6,7), (15,16))
ranges.sorted.foldLeft(List[(Int, Int)]()) { (acc, t) =>
acc match {
case x :: xs if x._2 >= t._1 => (x._1, math.max(x._2, t._2)) :: xs
case x :: xs => t +: acc
case _ => List(t)
}
}.reverse
@bgreenlee
bgreenlee / crontab
Last active December 11, 2015 19:59
Poor man's Nagios
# logtail from logcheck (http://logcheck.org/)
*/15 * * * * /usr/sbin/logtail /path/to/some/log/file.log | egrep "WARN|ERROR|Exception" | head