Skip to content

Instantly share code, notes, and snippets.

View dbushong's full-sized avatar

David Bushong dbushong

View GitHub Profile
require "option_parser"
require "http/params"
def_fields = "ip,ident,user,year,mon,day,hour,min,sec,method,path,query," +
"http,code,bytes,referrer,ua"
quiet = false
fields_str = def_fields
output = STDOUT
print_header = false
DASH_FIELDS = %w(ident user referrer ua bytes)
'use strict';
const fs = require('fs');
const AH = require('async_hooks');
const net = require('net');
const { inspect } = require('util');
function log(...args) {
fs.writeSync(process.stdout.fd, `${args.map(x => inspect(x, { depth: null, colors: true })).join(' ')}\n`);
}
$ cat foo.js
'use strict';
const asyncHooks = require('async_hooks');
const { writeSync } = require('fs');
const Memcached = require('memcached');
function debugHooks(types) {
// will contain metadata for all tracked events

Getting seconds or milliseconds since January 1, 1970, 00:00:00 GMT in various languages

Second-Native

C (1972)

// ms
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
#f '123456789',100
def f(d,t)(0...3**(d.size-1)).map{|c|d.chars.reduce{|s,g|o='+-'[c%3]||'';c=(c/3).floor;s+o+g}}.select{|e|e.scan(/\D?\d+/).map(&:to_i).reduce(:+)==t}end
if __FILE__ == $0
p f('123456789', 100)
end
@dbushong
dbushong / holdem.rb
Created October 22, 2014 00:51
old holdem implementation in ruby; used in an irc bot at one point
module TexasHoldEm
class RulesViolation < RuntimeError
end
class RingArray < Array
def [](n)
at n % size
end
end
@dbushong
dbushong / stubbable.coffee
Created December 27, 2013 19:44
Mini-lib to make exported libraries more stubbable w/ e.g. bondjs
###
replace yourlib's:
module.exports = { foo, bar }
with:
module.exports = require('./stubbable') { foo, bar }
now when some file does:
{ foo } = require './yourlib'
you can still stub it with:
yourlib = require './yourlib'
bond(yourlib._fns, 'foo').return 42
@dbushong
dbushong / iced.coffee
Last active January 1, 2016 11:19
Vanilla/Iced/Promise comparison for a git-exec'ing operation. Given an origin and a tag, return the SHA of the tag if it exists, else create a new tag and return that SHA
cp = require 'child_process'
git = (cmd, args, cb) -> # same as vanilla
cp.execFile 'git', [cmd].concat(args), (err, stdout) -> cb err, stdout.trim()
createTagIfMissing = (origin, tag, cb) ->
await git 'show-ref', [tag], defer err, showRef
if err?
await git 'tag', [tag], defer err
return cb err if err?
@dbushong
dbushong / iced.coffee
Last active January 1, 2016 11:19
Comparison of vanilla callback-passing vs. iced coffeescript vs. Q/promises. Mission is: 1. prompt for a url prefix 2. fetch (in parallel) two URLs based on that prefix 3. sleep for a number of seconds equal to the sum of the lengths of the results 4. print something announcing we're done
request = require 'request'
prompt = (str, cb) -> # same as vanilla
in = process.stdin
process.stdout.write "#{str}: "
in.setEncoding 'utf8'
in.once 'error', cb
in.once 'data', (res) ->
in.pause()
cb null, res.trim()
@dbushong
dbushong / compact-campfire.user.css
Created October 12, 2013 17:51
A userstyle for Stylish for Campfire in a chromeless browser to make it fit better in small spaces
#Container > div.Left {
margin-left: -25px;
width: 87%;
}
#room_title h2#topic { font-size: 8px; }
div#Sidebar { width: 16%; }
#room_title h1#room_name { font-size: 18px; }