Skip to content
Browse files

finish feature

  • Loading branch information...
1 parent 48a4d5b commit 54b1f93d175f8c3dd580b6c56a8c31d10b35e9f8 @txus committed Aug 30, 2011
View
1 Readme.md
@@ -85,3 +85,4 @@ server:
## Copyright
Copyright (c) 2011 Josep M. Bach. Released under the MIT license.
+
View
14 lib/domodoro/channel.rb
@@ -1,23 +1,11 @@
module Domodoro
class Channel < EM::Channel
def broadcast(timestamp, schedule)
- if ENV['DEBUG']
- puts 'DEBUG MODE: Start on even minutes, stop on odd minutes'
- if min % 2 == 0
- puts "#{Time.now} - Starting pomodoro!"
- self << :start
- else
- puts "#{Time.now} - Pomodoro break!"
- self << :stop
- end
- return
- end
-
action = schedule.to_hash[timestamp]
if action
next_action = schedule.action_after(timestamp)
self << {
- :action => [timestamp, action],
+ :current_action => [timestamp, action],
:next_action => next_action
}
end
View
53 lib/domodoro/client.rb
@@ -12,6 +12,14 @@ def connected=(value)
@connected = true
end
+ def current_action
+ @current_action
+ end
+
+ def current_action=(action)
+ @current_action = action
+ end
+
def next_action
@next_action
end
@@ -36,25 +44,23 @@ def c.connection_completed
def c.receive_object(object)
case object[:current_action].last
when :start
- puts "[#{object[:current_action].first}] - Starting pomodoro!"
Client.work
when :stop
- puts "[#{object[:current_action].first}] - Pomodoro break!"
Client.break
when :lunch
- puts "[#{object[:current_action].first}] - Lunch time!"
Client.lunch
when :go_home
- puts "[#{object[:current_action].first}] - Time to go home!"
Client.home
end
- Client.next_action = object[:next_action]
+ Client.current_action = object[:current_action]
+ Client.next_action = object[:next_action]
+ puts
end
end
EM.add_periodic_timer(1) do
EM.next_tick do
- if Client.connected
- print_next_action
+ if Client.connected && Client.current_action
+ print_status
else
puts 'Cannot connect to server. Is it running?'
end
@@ -88,25 +94,36 @@ def message_for(action)
end
end
+ def name_for(action)
+ case action.to_s
+ when "start" then "Pomodoro"
+ when "stop" then "Pomodoro Break"
+ when "lunch" then "Lunch time"
+ when "go_home" then "Go home"
+ end
+ end
+
def path_to(asset)
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'assets', asset))
end
- def print_next_action
- timestamp = Timepoint.new(Client.next_action.first)
- action = case Client.next_action.last
- when :start then "Pomodoro"
- when :stop then "Pomodoro Break"
- when :lunch then "Lunch time"
- when :go_home then "Go home"
- end
+ def print_status
+ current_action = name_for(Client.current_action.last)
- time_left = timestamp.left_until
+ if Client.next_action
+ next_action = name_for(Client.next_action.last)
+
+ # Calculate time left until next action
+ now = Timepoint.new(Time.now.hour, Time.now.min)
+ timestamp = Timepoint.new(Client.next_action.first)
+ time_left = now.left_until(timestamp)
+ end
$stdout.print "\r"
- $stdout.print " " * 50
+ $stdout.print " " * 100
$stdout.print "\r"
- $stdout.print "[ #{Client.current_action} ] - Next: #{action} in #{time_left}"
+ $stdout.print "[ #{[Client.current_action.first, current_action].join(' | ')} ]"
+ $stdout.print " - Next: #{next_action} in #{time_left}" if Client.next_action
$stdout.flush
end
View
8 lib/domodoro/timepoint.rb
@@ -56,19 +56,19 @@ def to_s
end
def left_until(timestamp)
- secs = 60 - Time.now.sec
+ secs = 59 - Time.now.sec
hours = timestamp.hour - @hour
if timestamp.hour == @hour
- remaining_minutes = (timestamp.min - @min).to_s.rjust(2, '0')
- return "00:#{remaining_minutes}::#{secs}"
+ remaining_minutes = (timestamp.min - @min - 1).to_s.rjust(2, '0')
+ return "00:#{remaining_minutes}:#{secs.to_s.rjust(2, '0')}"
end
if timestamp.min < @min
hours -= 1
end
- mins = (timestamp.min - @min) % 60
+ mins = ((timestamp.min - @min) % 60) - 1
h = hours.to_s.rjust(2, '0')
m = mins.to_s.rjust(2, '0')
View
4 test/domodoro/channel_test.rb
@@ -22,11 +22,11 @@ module Domodoro
describe 'if theres an action for the timestamp' do
it 'broadcasts it' do
@channel.expects(:<<).with(
- :action => ["08:30", :start],
+ :current_action => ["08:30", :start],
:next_action => ["09:00", :stop]
)
@channel.expects(:<<).with(
- :action => ["09:00", :stop],
+ :current_action => ["09:00", :stop],
:next_action => nil
)
View
1 test/domodoro/server_test.rb
@@ -5,6 +5,7 @@ module Domodoro
include EM::MiniTest::Spec
before do
+ File.stubs(:exist?).returns(false)
@channel = Channel.new
@schedule = Schedule.new
@schedule.generate!
View
11 test/domodoro/timepoint_test.rb
@@ -86,16 +86,19 @@ module Domodoro
describe '#left_until' do
it 'returns the time left until another timestamp' do
+ Time.stubs(:now).returns stub(:sec => 55)
@a = Timepoint.new(8, 30)
@b = Timepoint.new(9, 45)
@c = Timepoint.new(11, 10)
@d = Timepoint.new(8, 32)
@e = Timepoint.new(15, 01)
+ @f = Timepoint.new(8, 31)
- assert_match /01:15:/, @a.left_until(@b)
- assert_match /01:25:/, @b.left_until(@c) #2:-35
- assert_match /00:02:/, @a.left_until(@d)
- assert_match /06:31:/, @a.left_until(@e)
+ assert_equal "01:14:04", @a.left_until(@b)
+ assert_equal "01:24:04", @b.left_until(@c)
+ assert_equal "00:01:04", @a.left_until(@d)
+ assert_equal "06:30:04", @a.left_until(@e)
+ assert_equal "00:00:04", @a.left_until(@f)
end
end
end

0 comments on commit 54b1f93

Please sign in to comment.
Something went wrong with that request. Please try again.