Skip to content

Instantly share code, notes, and snippets.

@gampleman
Created February 7, 2012 12:25
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 gampleman/1759451 to your computer and use it in GitHub Desktop.
Save gampleman/1759451 to your computer and use it in GitHub Desktop.
Auto expiring message on command line
def expiring_message(msg, time)
message = Thread.new {
time.downto(0) do |t|
# You can use some code to get terminal width here instead of the 15 spaces I use
# however that usually isn't terribly portable.
printf "#{msg}#{' ' * 15}\r", t
sleep 1
end
puts
}
interupt = Thread.new {
message.exit if gets
}
interupt.join
message.exit
end
begin
SomeWebService.get_data # fails when network or server is down
rescue Exception => e
expiring_message "#{e.message}. Retrying in %d seconds. Press ENTER to retry immediately.", 60
retry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment