/
pompom.rb
233 lines (192 loc) · 4.85 KB
/
pompom.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'ffi-ncurses'
require 'artii'
require 'sequel'
require 'fileutils'
module Pompom
def self.format_time(number)
minutes = number / 60
seconds = number % 60
sprintf("%02d:%02d", minutes, seconds)
end
class Application
attr_reader :worklog
def initialize(options)
@options = options
if options[:no_log]
@worklog = NullWorkLog.new
else
@worklog = Worklog.new(options[:log])
end
end
def run
new_pomodoro do |pomodoro|
@worklog.start pomodoro
pomodoro.tick until pomodoro.finished?
end
play_sound
end
def cleanup
@worklog.finish_early
end
private
# There has to be a better way of playing sounds through ruby...
# This will only work if you have sox installed (or a command
# named /usr/bin/play) and a sound named sound.wav in your
# ~/.pompom directory.
def play_sound
if File.exists?(File.expand_path("~/.pompom/sound.wav")) && File.exists?("/usr/bin/play")
`play -q ~/.pompom/sound.wav`
end
end
def new_pomodoro
pomodoro = Pomodoro.new(@options[:time], @options[:message])
view = View.new(NCursesScreen.new)
pomodoro.add_observer(view)
view.run { yield pomodoro }
end
end
class Worklog
def initialize(path)
@path = File.expand_path(path)
@directory = File.dirname(@path)
end
def start(pomodoro)
ensure_present
@id = @db[:pomodoros].insert(:started_at => Time.now, :message => pomodoro.message, :finished_early => false)
end
def finish_early
@db[:pomodoros].where(:id => @id).update(:finished_early => true)
end
private
def ensure_present
if File.exists?(@path)
@db = Sequel.sqlite(@path)
else
FileUtils.mkdir_p(@directory) unless File.exists?(@directory)
@db = Sequel.sqlite(@path)
@db.create_table :pomodoros do
primary_key :id
Time :started_at
String :message
TrueClass :finished_early
end
end
end
end
class NullWorkLog
def start(pomodoro)
end
def finish_early
end
end
class Pomodoro
attr_reader :time_remaining, :message
attr_writer :system_clock
def initialize(time_remaining=1500, message=nil)
@time_remaining = time_remaining
@message = message
@system_clock = Kernel
@observers = []
@finished_early = true
end
def add_observer(object)
@observers << object
end
def tick
@system_clock.sleep 1
@time_remaining -= 1
@finished_early = false if @time_remaining == 0
notify_observers
end
def finish!
@time_remaining = 0
notify_observers
end
def finished_early?
@finished_early
end
def finished?
@time_remaining < 1
end
private
def notify_observers
@observers.each {|o| o.update(self) }
end
end
class View
attr_writer :asciifier
def initialize(screen)
@screen = screen
@asciifier = Asciifier.new
end
def run(&block)
@screen.run(&block)
end
def update(pomodoro)
@screen.display format_for_screen(pomodoro.time_remaining), color(pomodoro), blink(pomodoro)
end
private
def color(pomodoro)
if pomodoro.time_remaining > 60
:green
elsif pomodoro.time_remaining > 15
:yellow
else
:red
end
end
def blink(pomodoro)
pomodoro.time_remaining < 5
end
def format_for_screen(time)
@asciifier.asciify(Pompom.format_time(time))
end
end
class Asciifier
def initialize
@asciifier = Artii::Base.new
end
def asciify(text)
result = @asciifier.asciify(text).split("\n")
# Fix kerning for when 2nd character of minutes is a 4. This fix
# should really be moved back upstream to the figlet font.
result[3].sub!(/_\| /, '_|')
result.join("\n")
end
end
class NCursesScreen
include FFI::NCurses
def run
begin
initscr
cbreak
noecho
start_color
init_pair(1, COLOR_GREEN, COLOR_BLACK)
init_pair(2, COLOR_YELLOW, COLOR_BLACK)
init_pair(3, COLOR_RED, COLOR_BLACK)
curs_set 0
yield
ensure
endwin
end
end
def display(str, color=:green, blinking=false)
clear
text_style = blinking ? A_BLINK : A_NORMAL
attr_set text_style, color_pair_mapping[color], nil
y, x = getmaxyx(stdscr)
lines = str.split("\n")
padding = (x - lines.first.size) / 2
((y - lines.size - 1) / 2).times { addstr("\n") }
lines.each do |line|
addstr(" " * padding)
addstr(line + "\n")
end
refresh
end
def color_pair_mapping
{:green => 1,:yellow => 2,:red => 3 }
end
end
end