Created
November 13, 2013 17:33
-
-
Save borkabrak/7453043 to your computer and use it in GitHub Desktop.
Output text so that it looks like someone is typing it. (Filters STDIN to STDOUT with random short intervals between characters)
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 ruby | |
# | |
# Print output as if someone was typing it. | |
# | |
# * random short intervals between characters | |
# | |
# I have no idea why this seems so neat to me. | |
# Try piping the output of w3m -dump to it. | |
# | |
# -jdc 2013-06-20 | |
trap 'INT', 'SYSTEM_DEFAULT' # ^C exits. | |
speed = ARGV.length > 0 ? ARGV[0].to_i : 3 | |
lastchar = '' | |
STDIN.read.split(//).each do |char| | |
print char | |
# Normal delay | |
sleep (rand / speed) + 0.05 | |
# Occasional pauses between words | |
if ( (char != ' ') and | |
(lastchar == ' ') and | |
(rand(5) == 0) ) | |
# Sometimes the pause is long | |
if (rand(5) == 0) | |
sleep 16 / speed | |
else | |
sleep 4 / speed | |
end | |
end | |
lastchar = char | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a fun toy in Ruby. I'm posting it primarily to fiddle with the gist interface to github.