Created
February 7, 2012 12:25
-
-
Save gampleman/1759451 to your computer and use it in GitHub Desktop.
Auto expiring message on command line
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
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 |
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
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