Skip to content

Instantly share code, notes, and snippets.

@paigeruten
Created October 30, 2013 05:09
Show Gist options
  • Save paigeruten/7227505 to your computer and use it in GitHub Desktop.
Save paigeruten/7227505 to your computer and use it in GitHub Desktop.
a simple command line alarm clock, I use it to play some music when the alarm goes off.
#!/usr/bin/ruby
#
# Usage: ruby alarm.rb <time>
#
# <time> is either a number of seconds (e.g. "50"), minutes and seconds
# (e.g. "5:30"), or hours minutes and seconds (e.g. "7:30:00"). After the
# specified amount of time has passed, ring_alarm will be called and the
# program will exit.
#
def ring_alarm
puts "BBRRRRRRRRIIINNNGGGG!!!!"
`mplayer /Users/jeremy/Music/Maps/Maps-Vicissitude/04\\ I\\ Heard\\ Them\\ Say.mp3`
end
def parse_time(string)
parts = string.split(":", -1).map(&:to_f)
raise ArgumentError, "too many parts" if parts.length > 3
raise ArgumentError, "can't parse negative numbers" if parts.any? { |x| x < 0 }
parts.unshift(0) until parts.length == 3
hours, minutes, seconds = parts
seconds = hours * 3600 + minutes * 60 + seconds
seconds.to_i
end
if ARGV.empty?
puts "Usage: #$0 <time>"
exit
end
alarm_at = Time.now + parse_time(ARGV.first)
puts "Setting alarm for #{alarm_at}."
loop do
sleep 1
if Time.now >= alarm_at
ring_alarm
exit
end
puts "#{alarm_at.to_i - Time.now.to_i}..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment