Skip to content

Instantly share code, notes, and snippets.

View nornagon's full-sized avatar

Jeremy Rose nornagon

View GitHub Profile
@nornagon
nornagon / gist:1294723
Created October 18, 2011 06:06
Broken require
$ cat foo.js
require('share')
$ ls node_modules
share
$ head node_modules/share/package.json
{
"name": "share",
"version": "0.4.1",
"description": "A database for concurrent document editing",
"keywords": ["operational transformation", "ot", "concurrent", "collaborative", "database", "server"],
@nornagon
nornagon / atom.coffee
Created November 12, 2011 05:25
Tiny canvas game framework
window.atom = atom = {}
atom.input = {
_bindings: {}
_down: {}
_pressed: {}
_released: []
bind: (key, action) ->
@_bindings[key] = action
@nornagon
nornagon / sequence.js
Created January 8, 2012 08:38
Sequencing stuff in node.js
function getMoveFromPlayer(player_id, cb) {
process.spawn_child(whatever, cb)
}
function getNextMove() {
if (!gameStillRunning) { return }
getMoveFromPlayer(current_player, function (err, data) {
makeMove(data)
getNextMove()
})
@nornagon
nornagon / gist:2025487
Created March 12, 2012 23:49
Open related file in vim (a la Sublime Text 2's Alt-O)
" Opens a file in [path/to/current_file.*] that isn't already open.
function! RelatedFiles()
return filter(split(globpath(expand('%:h'), expand('%:t:r').'.*')), '!bufloaded(v:val)')
endfunction
function! OpenRelatedFile()
let files = RelatedFiles()
if len(files) > 0
" always open .h files on the left.
if match(files[0], "\.h$") >= 0
exec 'lefta vsplit '.files[0]
@nornagon
nornagon / server.coffee
Created March 19, 2012 11:34
Programmer nomic
coffee = require 'coffee-script'
vm = require 'vm'
connect = require 'connect'
browserChannel = require('browserchannel').server
comm = new (require('events').EventEmitter)
# TODO: sandbox
master = ((comm) ->
code = '''
<a href="http://segment.io" onclick="track('clicked on segment.io')">Yay!</a>
<script>
function track(msg) {
new Image().src = "http://segment.io/track?msg=" + encodeURLComponent(msg)
}
</script>
@nornagon
nornagon / _tag.coffee
Last active October 13, 2015 03:57
3l33t javascript tag creation
tag = (name, text) ->
parts = name.split /(?=[.#])/ # why yes, i am a ninja
tagName = "div"
classes = []
id = undefined
for p in parts
switch p[0]
when '#' then id = p.substr 1
when '.' then classes.push p.substr 1
else tagName = p
@nornagon
nornagon / new terminal.applescript
Created November 26, 2012 03:01
AppleScript to open a new iTerm session
tell application "System Events" to set terminal_open to process "iTerm" exists
if not terminal_open then
tell application "iTerm" to activate
else
tell application "iTerm"
set myterm to (make new terminal)
tell myterm
launch session "Default"
activate
end tell
@nornagon
nornagon / css_to_rgb.js
Created December 3, 2012 02:53
Parse CSS colors like 'hsla(32,50%,20%,0.4)' into RGB components
// Tested in Chrome 23, Firefox 16, and IE9 in standards mode (i.e. with <!DOCTYPE html>).
// Converts '#f00', 'red', 'hsl(0, 100%, 50%)' and 'rgb(255,0,0)' to {r:255,g:0,b:0}.
function cssColorToRGB(cssColor) {
var s = document.createElement('span')
document.body.appendChild(s)
s.style.backgroundColor = cssColor
var rgb = getComputedStyle(s).backgroundColor
document.body.removeChild(s)
var m = /^rgb\((\d+), (\d+), (\d+)\)$/.exec(rgb)
@nornagon
nornagon / gist:4472517
Created January 7, 2013 05:05
playing with bytebeat
T = function(s,v) { var x = t*v; return (x*(3+1*s[(x>>10)%s.length]))%255 },
S = function(s,v) { var x = t*v; return (x*(3+1*s[(x>>10)%s.length]))%255>127?255:0 },
(T("121312123321",1) + T("12131212332",1.005))/2