Skip to content

Instantly share code, notes, and snippets.

@adambair
Created October 25, 2011 15:14
Show Gist options
  • Select an option

  • Save adambair/1313082 to your computer and use it in GitHub Desktop.

Select an option

Save adambair/1313082 to your computer and use it in GitHub Desktop.
# Yourinal; Your Terminal
#
# A collection tools and utilities for your terminal needs
#
# Name credit goes to Jim Albano (@JimDAlbano).
# I was going to call it Coctane; Console Octane.
# Yourinal; Your Terminal - is far better.
#
# Backup name via Flip Sasser (@flipsasser): PimpMyConsole
#
module Yourinal
# Terminal Progress Indicator
#
# Displays a starting message and an indicator each interval until the
# passed block resolves. Good for showing that something takes a while
# and is expected. Essentially a progress bar for unknown total/finish.
#
# message - The string to be displayed at start
# interval - The time in between each indicator mark
# indicator - The string displayed on each interval
#
# Examples:
#
# connection_progress = Yourinal::Progress.new("Connecting to service, please wait")
# response = connection_progress.start {
# # Code that takes a while...
# # That you want to wait for...
# }
#
# Returns "Connecting to service, please wait" with "." for each second that passes
# Then connection_progress.start returns the result of the block.
#
# Output "Connecting to service, please wait...........done (10.69 seconds)"
class Progress
attr_accessor :results
def initialize(message='Starting', interval=1.0)
@message = message
@interval = interval
end
def start
setup
@results = yield
teardown
rescue => e
raise "Failed: #{e.message}"
ensure
kill_indicator
end
def kill_indicator
return unless @indicator
Process.kill 'TERM', @indicator
end
def start_indicator
@indicator = fork { loop {print '.'; sleep @interval}}
end
def setup
print @message
@start_time = Time.now
start_indicator
end
def teardown
@end_time = Time.now
kill_indicator
print "done (#{sprintf('%.2f', total_time)} seconds)\n"
@results
end
def total_time
@end_time - @start_time
end
end
end
@adambair
Copy link
Copy Markdown
Author

I probably did something stupid or wrong and stupid. Comments welcome ;)

@adambair
Copy link
Copy Markdown
Author

This has been wrapped in a gem and now lives at http://github.com/adambair/yourinal

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