Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

finish feature

  • Loading branch information...
commit 54b1f93d175f8c3dd580b6c56a8c31d10b35e9f8 1 parent 48a4d5b
Josep M. Bach authored
1  Readme.md
Source Rendered
@@ -85,3 +85,4 @@ server:
85 85 ## Copyright
86 86
87 87 Copyright (c) 2011 Josep M. Bach. Released under the MIT license.
  88 +
14 lib/domodoro/channel.rb
... ... @@ -1,23 +1,11 @@
1 1 module Domodoro
2 2 class Channel < EM::Channel
3 3 def broadcast(timestamp, schedule)
4   - if ENV['DEBUG']
5   - puts 'DEBUG MODE: Start on even minutes, stop on odd minutes'
6   - if min % 2 == 0
7   - puts "#{Time.now} - Starting pomodoro!"
8   - self << :start
9   - else
10   - puts "#{Time.now} - Pomodoro break!"
11   - self << :stop
12   - end
13   - return
14   - end
15   -
16 4 action = schedule.to_hash[timestamp]
17 5 if action
18 6 next_action = schedule.action_after(timestamp)
19 7 self << {
20   - :action => [timestamp, action],
  8 + :current_action => [timestamp, action],
21 9 :next_action => next_action
22 10 }
23 11 end
53 lib/domodoro/client.rb
@@ -12,6 +12,14 @@ def connected=(value)
12 12 @connected = true
13 13 end
14 14
  15 + def current_action
  16 + @current_action
  17 + end
  18 +
  19 + def current_action=(action)
  20 + @current_action = action
  21 + end
  22 +
15 23 def next_action
16 24 @next_action
17 25 end
@@ -36,25 +44,23 @@ def c.connection_completed
36 44 def c.receive_object(object)
37 45 case object[:current_action].last
38 46 when :start
39   - puts "[#{object[:current_action].first}] - Starting pomodoro!"
40 47 Client.work
41 48 when :stop
42   - puts "[#{object[:current_action].first}] - Pomodoro break!"
43 49 Client.break
44 50 when :lunch
45   - puts "[#{object[:current_action].first}] - Lunch time!"
46 51 Client.lunch
47 52 when :go_home
48   - puts "[#{object[:current_action].first}] - Time to go home!"
49 53 Client.home
50 54 end
51   - Client.next_action = object[:next_action]
  55 + Client.current_action = object[:current_action]
  56 + Client.next_action = object[:next_action]
  57 + puts
52 58 end
53 59 end
54 60 EM.add_periodic_timer(1) do
55 61 EM.next_tick do
56   - if Client.connected
57   - print_next_action
  62 + if Client.connected && Client.current_action
  63 + print_status
58 64 else
59 65 puts 'Cannot connect to server. Is it running?'
60 66 end
@@ -88,25 +94,36 @@ def message_for(action)
88 94 end
89 95 end
90 96
  97 + def name_for(action)
  98 + case action.to_s
  99 + when "start" then "Pomodoro"
  100 + when "stop" then "Pomodoro Break"
  101 + when "lunch" then "Lunch time"
  102 + when "go_home" then "Go home"
  103 + end
  104 + end
  105 +
91 106 def path_to(asset)
92 107 File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'assets', asset))
93 108 end
94 109
95   - def print_next_action
96   - timestamp = Timepoint.new(Client.next_action.first)
97   - action = case Client.next_action.last
98   - when :start then "Pomodoro"
99   - when :stop then "Pomodoro Break"
100   - when :lunch then "Lunch time"
101   - when :go_home then "Go home"
102   - end
  110 + def print_status
  111 + current_action = name_for(Client.current_action.last)
103 112
104   - time_left = timestamp.left_until
  113 + if Client.next_action
  114 + next_action = name_for(Client.next_action.last)
  115 +
  116 + # Calculate time left until next action
  117 + now = Timepoint.new(Time.now.hour, Time.now.min)
  118 + timestamp = Timepoint.new(Client.next_action.first)
  119 + time_left = now.left_until(timestamp)
  120 + end
105 121
106 122 $stdout.print "\r"
107   - $stdout.print " " * 50
  123 + $stdout.print " " * 100
108 124 $stdout.print "\r"
109   - $stdout.print "[ #{Client.current_action} ] - Next: #{action} in #{time_left}"
  125 + $stdout.print "[ #{[Client.current_action.first, current_action].join(' | ')} ]"
  126 + $stdout.print " - Next: #{next_action} in #{time_left}" if Client.next_action
110 127 $stdout.flush
111 128 end
112 129
8 lib/domodoro/timepoint.rb
@@ -56,19 +56,19 @@ def to_s
56 56 end
57 57
58 58 def left_until(timestamp)
59   - secs = 60 - Time.now.sec
  59 + secs = 59 - Time.now.sec
60 60 hours = timestamp.hour - @hour
61 61
62 62 if timestamp.hour == @hour
63   - remaining_minutes = (timestamp.min - @min).to_s.rjust(2, '0')
64   - return "00:#{remaining_minutes}::#{secs}"
  63 + remaining_minutes = (timestamp.min - @min - 1).to_s.rjust(2, '0')
  64 + return "00:#{remaining_minutes}:#{secs.to_s.rjust(2, '0')}"
65 65 end
66 66
67 67 if timestamp.min < @min
68 68 hours -= 1
69 69 end
70 70
71   - mins = (timestamp.min - @min) % 60
  71 + mins = ((timestamp.min - @min) % 60) - 1
72 72
73 73 h = hours.to_s.rjust(2, '0')
74 74 m = mins.to_s.rjust(2, '0')
4 test/domodoro/channel_test.rb
@@ -22,11 +22,11 @@ module Domodoro
22 22 describe 'if theres an action for the timestamp' do
23 23 it 'broadcasts it' do
24 24 @channel.expects(:<<).with(
25   - :action => ["08:30", :start],
  25 + :current_action => ["08:30", :start],
26 26 :next_action => ["09:00", :stop]
27 27 )
28 28 @channel.expects(:<<).with(
29   - :action => ["09:00", :stop],
  29 + :current_action => ["09:00", :stop],
30 30 :next_action => nil
31 31 )
32 32
1  test/domodoro/server_test.rb
@@ -5,6 +5,7 @@ module Domodoro
5 5 include EM::MiniTest::Spec
6 6
7 7 before do
  8 + File.stubs(:exist?).returns(false)
8 9 @channel = Channel.new
9 10 @schedule = Schedule.new
10 11 @schedule.generate!
11 test/domodoro/timepoint_test.rb
@@ -86,16 +86,19 @@ module Domodoro
86 86
87 87 describe '#left_until' do
88 88 it 'returns the time left until another timestamp' do
  89 + Time.stubs(:now).returns stub(:sec => 55)
89 90 @a = Timepoint.new(8, 30)
90 91 @b = Timepoint.new(9, 45)
91 92 @c = Timepoint.new(11, 10)
92 93 @d = Timepoint.new(8, 32)
93 94 @e = Timepoint.new(15, 01)
  95 + @f = Timepoint.new(8, 31)
94 96
95   - assert_match /01:15:/, @a.left_until(@b)
96   - assert_match /01:25:/, @b.left_until(@c) #2:-35
97   - assert_match /00:02:/, @a.left_until(@d)
98   - assert_match /06:31:/, @a.left_until(@e)
  97 + assert_equal "01:14:04", @a.left_until(@b)
  98 + assert_equal "01:24:04", @b.left_until(@c)
  99 + assert_equal "00:01:04", @a.left_until(@d)
  100 + assert_equal "06:30:04", @a.left_until(@e)
  101 + assert_equal "00:00:04", @a.left_until(@f)
99 102 end
100 103 end
101 104 end

0 comments on commit 54b1f93

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