Created
January 5, 2011 16:19
-
-
Save btedev/766531 to your computer and use it in GitHub Desktop.
command-line timer for OS X
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 | |
require 'optparse' | |
require 'time' | |
require 'real_growl' | |
require 'progressbar' | |
INCREMENTS=100 | |
class TimeFile | |
FILE_NAME = '.timer' | |
def self.record(minutes, elapsed_seconds) | |
File.open(FILE_NAME, 'w') do |file| | |
file.write([minutes, elapsed_seconds].join(',')) | |
end | |
end | |
def self.minutes | |
read[0].to_i | |
end | |
def self.remaining_seconds | |
read[1].to_i | |
end | |
private | |
def self.read | |
return [0,0] unless File.exist?(FILE_NAME) | |
File.read(FILE_NAME).chomp.split(',') | |
end | |
end | |
def formatted_minutes(seconds) | |
mins = seconds / 60 | |
seconds -= (mins * 60) | |
formatted_seconds = sprintf("%02d", seconds) | |
"#{mins}:#{formatted_seconds}" | |
end | |
options = {} | |
option_parser = OptionParser.new do |opts| | |
opts.banner = <<EOF | |
Usage: timer.rb [options] | |
Example: timer.rb 10 | |
timer.rb -l (--last) | |
timer.rb -r (--resume) | |
EOF | |
opts.on('-l', '--last', 'Last used time') do | |
options[:last] = true | |
end | |
opts.on('-r', '--resume', 'Resume time if last timer interrupted') do | |
options[:resume] = true | |
end | |
end | |
option_parser.parse! | |
unless ARGV.size == 1 || options[:last] || options[:resume] | |
puts option_parser.banner | |
exit 1 | |
end | |
if options[:last] || options[:resume] | |
minutes = TimeFile.minutes | |
raise ArgumentError, 'No last time recorded' unless minutes > 0 | |
else | |
minutes = ARGV[0].to_i | |
end | |
total_seconds = minutes * 60 | |
seconds = total_seconds | |
if options[:resume] | |
seconds = TimeFile.remaining_seconds | |
puts "Resuming at #{formatted_minutes(seconds)}" | |
raise ArgumentError, 'Nothing to resume' unless seconds > 0 | |
end | |
puts "\n" | |
pbar = ProgressBar.new("#{minutes} min:", INCREMENTS) | |
pbar.format = "%-#{@title_width}s %3d%% %s" | |
begin | |
while(seconds > 0) | |
if seconds % 5 == 0 | |
perc = INCREMENTS - ((seconds.to_f / total_seconds.to_f) * 100) | |
pbar.set(perc.to_i) | |
end | |
seconds -= 1 | |
sleep(1) | |
end | |
rescue SystemExit, Interrupt | |
# User interrupted script, e.g. ctrl-c. | |
# Display elapsed time to user | |
msg = "\nInterrupted after " | |
msg << formatted_minutes(total_seconds - seconds) | |
msg << " with " | |
msg << formatted_minutes(seconds) | |
msg << " remaining\n" | |
puts msg | |
# Record time to file | |
TimeFile.record(minutes, seconds) | |
exit 1 | |
end | |
pbar.set(INCREMENTS) | |
# Record last time | |
TimeFile.record(minutes, seconds) | |
# Growl | |
timer_app = RealGrowl::Application.new("Timer") | |
timer_app.notify(:title => "Elapsed", :description => "#{minutes} minutes", :priority => 0) | |
# OS X say | |
msg = "#{minutes} minutes elapsed" | |
`say -v Zarvox "#{msg}"` | |
puts "\n#{Time.now}\n\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment