Created
April 25, 2012 22:35
-
-
Save hickford/2494028 to your computer and use it in GitHub Desktop.
keyboard churn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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