Skip to content

Instantly share code, notes, and snippets.

@rtyler
Last active September 14, 2022 20:00
Show Gist options
  • Save rtyler/4343450 to your computer and use it in GitHub Desktop.
Save rtyler/4343450 to your computer and use it in GitHub Desktop.
Simple pomodoro CLI tool for Linux
#!/usr/bin/env ruby
require 'time'
module Pomodoro
INTERVAL = 25 # minutes
TOTAL_INTERVALS = 4
BREAK_TIME = 5 # minutes
def self.notify(title, body)
system("notify-send -a pomodoro -t 5000 -u critical -i appointment '#{title}' '#{body}'")
end
def self.play_sound
filepath = File.expand_path('~/bin/pomodoro-ring.wav')
if File.exists? filepath
fork do
exec('aplay', '-q', filepath)
end
end
end
def self.pomodoro!
INTERVAL.times.reverse_each do |minute|
# Account for the 0-based offset
minute = minute + 1
clear!
clear!
tomato! "#{minute} min"
sleep 60
end
notify('Pomodoro!', 'A pomodoro has ended, time for a break!')
play_sound
end
def self.wait_for_enter
key = nil
until key == "\n"
key = gets
end
nil
end
def self.bold(msg)
"\033[1m#{msg}\033[22m"
end
def self.red(msg)
"\033[31m#{msg}\033[0m"
end
def self.green(msg)
"\033[32m#{msg}\033[0m"
end
def self.clear!
print "\033[6A"
print "\033[2J"
end
def self.tomato!(msg=nil)
banner_size = 8
unless msg.nil?
# Center the `msg` inside of the tomato
if msg.size < banner_size
padding = banner_size - msg.size
left = true
padding.times do
if left
msg = " #{msg}"
else
msg = "#{msg} "
end
left = !(left)
end
end
else
msg = ' ' * banner_size
end
print green(""" __\W/__
""")
print red(""" .'.-'""")
print green('|')
print red("""'-.'.
/ \\
| #{msg} |
\\ /
'-.___.-'
""")
end
def self.minitomato!
print red('(')
print green('`* ')
print red(') ')
end
def self.run!
TOTAL_INTERVALS.times do |cycle|
count = cycle + 1
pomodoro!
# Display our current progress in the intervals
puts '-----------------------'
count.times { minitomato! }
puts
# Only display the prompt if we're on the last pomodoro
unless TOTAL_INTERVALS == count
puts "Take a break! Meet me back here in #{BREAK_TIME} minutes"
sleep (BREAK_TIME * 60)
play_sound
puts "Press enter to start another pomodoro"
wait_for_enter
end
end
finish_message = "#{TOTAL_INTERVALS} pomodoros have been completed, take a 30 minute break and come on back"
puts finish_message
notify('Pomodoro!', finish_message)
play_sound
end
end
begin
Pomodoro.run!
rescue Interrupt
puts
puts 'exiting..hope you got your work done!'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment