Skip to content

Instantly share code, notes, and snippets.

@borkabrak
Created November 13, 2013 17:33
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 borkabrak/7453043 to your computer and use it in GitHub Desktop.
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)
#!/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
@borkabrak
Copy link
Author

This was a fun toy in Ruby. I'm posting it primarily to fiddle with the gist interface to github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment