Skip to content

Instantly share code, notes, and snippets.

@andrew-carroll
Created December 2, 2015 05:32
Show Gist options
  • Save andrew-carroll/6fc44046898631538995 to your computer and use it in GitHub Desktop.
Save andrew-carroll/6fc44046898631538995 to your computer and use it in GitHub Desktop.
require 'music_player'
require 'timer'
class AlarmClock
def initialize(music_player, timer)
@music_player = music_player
@timer = timer
end
def start
@timer.start { @music_player.start }
end
def silence
@music_player.stop
end
end
class MusicPlayer
def initialize(mp3)
@mp3 = mp3
end
def start
at_exit { stop }
@player_pid = spawn("cvlc #@mp3 2> /dev/null")
end
def stop
unless @player_pid.nil?
Process.kill('QUIT', @player_pid + 1)
end
end
end
#!/usr/bin/env ruby
$:.unshift File.dirname __FILE__
require 'alarm_clock'
require 'pry'
mp3, *time = ARGV
time = time.join ' '
player = MusicPlayer.new(mp3)
timer = Timer.new(time)
alarm = AlarmClock.new(player, timer)
system('clear')
puts "Sweet dreams! You will be woken at #{Chronic::parse time}"
alarm.start
system('clear')
30.times { puts "WAKE UP! " * rand(1..5); sleep 2 }
60.times { puts "WAKE UP! " * rand(1..5); sleep 1 }
120.times { puts "WAKE UP! " * rand(1..5); sleep 0.5 }
Process.wait
require 'chronic'
class Timer
def initialize(time)
@time = time
end
def start
sleep until_alarm(@time)
yield
end
private
def until_alarm(time)
Chronic::parse(time) - Time.now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment