/
task.rb
177 lines (137 loc) · 3.86 KB
/
task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module Pomo
class Task
##
# Task name.
attr_accessor :name
##
# Length in minutes.
attr_accessor :length
##
# Verbose task description.
attr_accessor :description
##
# Task currently running bool.
attr_accessor :running
##
# Task completion bool.
attr_accessor :complete
##
# Initialize with _name_ and _options_.
def initialize name = nil, options = {}
@name = name or raise '<task> required'
@description = options.delete :description
@length = options.fetch :length, 25
@running = false
@complete = false
end
##
# Quoted task name.
def to_s
name.inspect
end
##
# Check if the task currently running.
def running?
running
end
##
# Check if the task has been completed.
def complete?
complete
end
##
# Check if the task is a github issue.
def github?
false
end
##
# Start timing the task.
def start(config, options = {})
list = options[:list]
progress = options[:progress]
@running = true
list.save unless list.nil?
if progress
foreground_progress(config)
else
background_progress(config)
end
end
private
def foreground_progress(config)
notifier = Pomo::Notifier.new(config)
say "Started #{self}, you have #{length} minutes :)"
complete_message = "Time is up! Hope you are finished #{self}"
format_message = '(:progress_bar) :remaining minutes remaining'
progress(
(0..length).to_a.reverse,
:format => format_message,
:tokens => { :remaining => length },
:complete_message => complete_message
) do |remaining|
if remaining == length / 2
notifier.notify 'Half way there!', :header => "#{remaining} minutes remaining"
elsif remaining == 5
notifier.notify 'Almost there!', :header => '5 minutes remaining'
end
sleep 60
{ :remaining => remaining }
end
notifier.notify "Hope you are finished #{self}", :header => 'Time is up!', :type => :warning
list = Pomo::List.new
if task = list.running
task.running = false
task.complete = true
list.save
end
end
def background_progress(config)
notifier = Pomo::Notifier.new(config)
notifier.notify "Started #{self}", :header => "You have #{length} minutes"
pid = Process.fork do
length.downto(1) do |remaining|
write_tmux_time(remaining) if config.tmux
refresh_tmux_status_bar if config.tmux
if remaining == length / 2
notifier.notify 'Half way there!', :header => "#{remaining} minutes remaining"
elsif remaining == 5
notifier.notify 'Almost there!', :header => '5 minutes remaining'
end
sleep 60
end
write_tmux_time(0) if config.tmux
refresh_tmux_status_bar if config.tmux
notifier.notify "Hope you are finished #{self}", :header => 'Time is up!', :type => :warning
list = Pomo::List.new
if task = list.running
task.running = false
task.complete = true
list.save
end
end
Process.detach(pid)
end
def tmux_time(time)
case time
when 0
"#{time}:00"
when 1..5
"#[default]#[fg=red]#{time}:00#[default]"
when 6..100
"#[default]#[fg=green]#{time}:00#[default]"
end
end
def write_tmux_time(time)
path = File.join(ENV['HOME'],'.pomo_stat')
File.open(path, 'w') do |file|
file.write tmux_time(time)
end
end
def refresh_tmux_status_bar
pid = Process.fork do
exec "tmux refresh-client -S -t $(tmux list-clients -F '\#{client_tty}')"
end
Process.detach(pid)
end
end
end