Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hickford
Created April 25, 2012 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hickford/2494028 to your computer and use it in GitHub Desktop.
Save hickford/2494028 to your computer and use it in GitHub Desktop.
keyboard churn
#!/usr/bin/env coffee
process.stdin.resume()
require('tty').setRawMode(true)
history = {}
plans = {}
spew = () -> console.log(new Date)
setInterval spew, 1000
process.stdin.on 'keypress', (char, key) =>
if (key && key.ctrl && key.name == 'c')
process.exit()
console.log("Event '#{char}'")
clearTimeout plans[char]
if ! history[char]?
history[char] = []
history[char].push new Date
delay = 6000 # timespans in milliseconds
if history[char].length > 1
mean = ( history[char][history[char].length-1] - history[char][0] ) / history[char].length
delay = 3*mean+500
task = () =>
console.log("Churn '#{char}'")
history[char] = []
plans[char] = setTimeout task, delay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment