Created
February 13, 2020 15:25
-
-
Save 088haizi/10f67015612da7037cb39416eef8cd81 to your computer and use it in GitHub Desktop.
[Command Line Countdown Coding Gadgets](https://www.amazon.com/Digital-Kitchen-Countdown-Cooking-Gadgets/dp/B078SNB7DH)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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