Skip to content

Instantly share code, notes, and snippets.

@088haizi
Created February 13, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 088haizi/10f67015612da7037cb39416eef8cd81 to your computer and use it in GitHub Desktop.
Save 088haizi/10f67015612da7037cb39416eef8cd81 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# timer.rb
# README
# usage:
# chmod +x timer.rb
# ./timer.rb <minutes>
# ex $./timer.rb 25
def flush_print(content)
print content + "\r"
$stdout.flush
end
def percent_string(percent, timer)
current = "#" * percent
others = " " * (100 - percent)
summary = "#{current}%"
"[#{current}#{others}] #{percent}% #{timestamp(timer)}"
# [######################################################################### ] 73% 00:06:35
# [####################################################################################################] 100% 00:00:00
end
def timestamp(time)
Time.at(time).utc.strftime("%H:%M:%S")
end
def count_down(minutes)
seconds = minutes * 60
percent = 0
seconds.times do |time|
current = (time / seconds.to_f * 100).to_i
sleep 1
if current != percent
percent == current
end
flush_print(percent_string(current, seconds - time))
end
puts percent_string(100, 0)
end
count_down(ARGV[0].to_i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment