Skip to content

Instantly share code, notes, and snippets.

@tjstankus
Last active Mar 23, 2020
Embed
What would you like to do?
# - [x] Display dialog at end
# - [x] Display dialog return value
# - [x] Capture output from dialog
# - [x] Actually snooze the timer
# - [ ] Smart update the UI
channel = Channel(String).new
now = Time.local
ends_at = now + 5.seconds
puts remaining(ends_at - now)
def remaining(time_span : Time::Span) : String
minutes = time_span.minutes.to_s.rjust(2, '0')
seconds = time_span.seconds.to_s.rjust(2, '0')
"#{minutes}:#{seconds}"
end
spawn do
loop do
time_span = ends_at - Time.local
sleep 1
channel.send(remaining(time_span))
end
end
loop do
cmd = "osascript -e " +
"'display dialog \"Are you sure!?\" buttons {\"Stop\", \"Snooze\"}' "
if Time.local >= ends_at # timer_expired?
response = `#{cmd}`
if response.includes?("Snooze")
puts "Snooze"
ends_at = Time.local + 5.seconds
channel.receive
else
puts "Stop"
exit
end
else
puts "\r\033[1A\033[0K#{channel.receive}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment