-
-
Save akahn/702725 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'highline/system_extensions' | |
include HighLine::SystemExtensions | |
module Standup | |
module Control | |
extend self | |
def play | |
`osascript -e 'tell application "iTunes" to play'` | |
end | |
def pause | |
`osascript -e 'tell application "iTunes" to pause'` | |
end | |
def set_volume(level) | |
`osascript -e 'tell application "iTunes" to set sound volume to #{level}'` | |
end | |
end | |
class Timer | |
def initialize(length = 50, initial_volume = 20) | |
@length, @initial_volume = length.to_i, initial_volume.to_i | |
Control.set_volume(@initial_volume) | |
Control.pause | |
start | |
end | |
def start | |
@countdown = Thread.new do | |
countdown | |
fade_in | |
end | |
end | |
def stop | |
@countdown.kill | |
Control.pause | |
end | |
def restart | |
stop; start | |
end | |
def countdown | |
@length.downto(1) do |i| | |
p i | |
sleep 1 | |
end | |
Control.play | |
end | |
def fade_in | |
@initial_volume.upto 100 do |i| | |
Control.set_volume(i) | |
sleep 0.05 | |
i += 1 | |
end | |
end | |
end | |
def self.Timer(length, initial_volume) | |
timer = Standup::Timer.new(length, initial_volume) | |
begin | |
loop do | |
character = get_character | |
if [32, 13].include? character # Restart on space bar or enter | |
timer.restart | |
elsif character == 27 # Exit on escape | |
raise Interrupt | |
end | |
end | |
rescue Interrupt | |
Control.pause | |
exit | |
end | |
end | |
end | |
if $0 == __FILE__ | |
length, initial_volume = ARGV | |
Standup::Timer(length, initial_volume) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment