Skip to content

Instantly share code, notes, and snippets.

@Samuelfaure
Created October 12, 2024 17:23
Show Gist options
  • Select an option

  • Save Samuelfaure/b3384d29a475d78fb08bc6cf32087133 to your computer and use it in GitHub Desktop.

Select an option

Save Samuelfaure/b3384d29a475d78fb08bc6cf32087133 to your computer and use it in GitHub Desktop.
Simple pomodoro script
# frozen_string_literal: true
require 'optparse'
# Need to gem install those
require 'ruby-progressbar'
require 'colorize'
# Nice-to-have
class Integer
def seconds
self
end
def minutes
self * 60
end
def hours
self * 3600
end
end
# The Pomodoro timer
class Pomodoro
def start
define_options
@count = ARGV[0].to_i
@count = 1 if @count.zero?
@time = Time.now
@time = Time.now.strftime('%d/%m/%Y %H:%m')
if count_incorrect
puts 'Invalid input, please enter number between 1 and 20'
exit
end
pomodoro_loop
end
private
def define_options
# defaults
@time_work = 50.minutes
option_parsing
@time_break ||= @time_work / 5
@time_cycle = @time_work + @time_break
end
def option_parsing
OptionParser.new do |opts|
opts.banner = 'Usage: pomodoro.rb [count] [options]'
opts.on('-s', '--short', 'short cycle (25 mins)') { @time_work = 25.minutes }
opts.on('-w n', '--work-time=m', 'delay for work (minutes) (default 50)') { |m| @time_work = m.to_i.minutes }
opts.on('-b m', '--break-time=m', 'delay for break (minutes) (default w/5)') { |m| @time_break = m.to_i.minutes }
opts.on('-l t', '--log=t', 'log a certain time') { |t| just_log(t.to_i.minutes) }
end.parse!
end
def count_incorrect
!@count.to_i.between?(1, 20)
end
def pomodoro_loop
@count.times do
work_time
break_time
end
puts 'Congratulations ! All cycles done !'.green
end
def work_time
puts "Starting: ๐Ÿ… #{@count} left"
puts "##### WORK TIME! ##### (#{@time_work / 60} mins)".light_green
notif_work
time_loop(@time_work)
save_one_pomodoro_done
@count -= 1
end
def break_time
puts "##### BREAK TIME! ##### (#{@time_break / 60} mins)".light_blue
notif_break
time_loop(@time_break)
end
def time_loop(time)
@progressbar = ProgressBar.create(format: '%t: |%B| %a')
@progressbar.total = time
slowly_increment_progressbar(time)
end
def slowly_increment_progressbar(seconds)
seconds.times do
@progressbar.increment
sleep(1)
end
@progressbar.finish
end
def save_one_pomodoro_done
file = File.open('/home/zerh/Documents/work_done.txt', 'a')
file.write("#{time_now_formatted} ๐Ÿ… finished #{@time_cycle / 60} min\n".public_send(color_of_the_day))
file.close
end
def time_now_formatted
Time.now.strftime('%d/%m/%Y %H:%m')
end
def color_of_the_day
colors = %i[red green yellow blue magenta cyan white]
colors[Time.now.day % colors.length]
end
def notif_break
cmd = 'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/dunstify ' \
"-i /home/zerh/Dotfiles/assets/icons/triangle.svg 'Obey over' 'Time to consume' --urgency=normal"
`#{cmd}`
play_beep
end
def notif_work
cmd = 'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/dunstify ' \
"-i /home/zerh/Dotfiles/assets/icons/triangle.svg 'Consume over' 'Time to obey' --urgency=low"
`#{cmd}`
play_beep
end
def play_beep
cmd = 'aplay -q /home/zerh/Dotfiles/assets/sounds/beep.wav'
`#{cmd}`
end
def just_log(time)
@time_work = time
@time_break ||= @time_work / 5
@time_cycle = @time_work + @time_break
save_one_pomodoro_done
puts "Save time cycle of #{@time_cycle / 60} minutes"
exit 0
end
end
Pomodoro.new.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment