-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add :eta_time token to display the estimated time of day at completion
- Loading branch information
1 parent
dd8fe57
commit 35bc489
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module TTY | ||
class ProgressBar | ||
# Used by {Pipeline} to foramt :eta_time token | ||
# | ||
# @api private | ||
class EstimatedTimeFormatter | ||
include TTY::ProgressBar::Formatter[/:eta_time/.freeze] | ||
|
||
# Format :eta_time token | ||
# | ||
# @param [String] value | ||
# the value to format | ||
# | ||
# @api public | ||
def format(value) | ||
if @progress.indeterminate? | ||
return value.gsub(matcher, "--:--:--") | ||
end | ||
|
||
elapsed = Time.now - @progress.start_at | ||
estimated = (elapsed / @progress.ratio).to_f - elapsed | ||
estimated = (estimated.infinite? || estimated < 0) ? 0.0 : estimated | ||
|
||
time_format = if estimated >= 86_400 # longer than a day | ||
"%Y-%m-%d %H:%M:%S" | ||
else | ||
"%H:%M:%S" | ||
end | ||
completion_time = Time.now + estimated | ||
eta_time = completion_time.strftime(time_format) | ||
value.gsub(matcher, eta_time) | ||
end | ||
end # EstimatedTimeFormatter | ||
end # ProgressBar | ||
end # TTY |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe TTY::ProgressBar, ":eta_time token" do | ||
let(:output) { StringIO.new } | ||
|
||
it "displays estimated completion time of day" do | ||
time_now = Time.local(2020, 1, 5, 12, 0, 0) | ||
Timecop.freeze(time_now) | ||
bar = described_class.new(" :eta_time", output: output, total: 5) | ||
|
||
5.times do |sec| | ||
time_now = Time.local(2020, 1, 5, 12, 0, sec) | ||
Timecop.freeze(time_now) | ||
bar.advance | ||
end | ||
|
||
output.rewind | ||
expect(output.read).to eq([ | ||
"\e[1G 12:00:00", | ||
"\e[1G 12:00:02", | ||
"\e[1G 12:00:03", | ||
"\e[1G 12:00:03", | ||
"\e[1G 12:00:04\n" | ||
].join) | ||
Timecop.return | ||
end | ||
|
||
it "displays estimated completion time of day with date after 24 hours" do | ||
time_now = Time.local(2020, 1, 5, 12, 0, 0) | ||
Timecop.freeze(time_now) | ||
bar = described_class.new(" :eta_time", output: output, total: 5) | ||
|
||
2.times do |day| | ||
time_now = Time.local(2020, 1, day + 6, 12, 0, 0) | ||
Timecop.freeze(time_now) | ||
bar.advance | ||
end | ||
|
||
output.rewind | ||
expect(output.read).to eq([ | ||
"\e[1G 12:00:00", | ||
"\e[1G 2020-01-09 00:00:00", | ||
].join) | ||
Timecop.return | ||
end | ||
|
||
it "displays unknown estimated completion time of day as --:--:--" do | ||
time_now = Time.local(2020, 1, 5, 12, 0, 0) | ||
Timecop.freeze(time_now) | ||
bar = described_class.new(" :eta_time", output: output, total: nil) | ||
|
||
3.times { bar.advance } | ||
bar.update(total: 5) | ||
|
||
2.times do |sec| | ||
time_now = Time.local(2020, 1, 5, 12, 0, sec) | ||
Timecop.freeze(time_now) | ||
bar.advance | ||
end | ||
|
||
output.rewind | ||
expect(output.read).to eq([ | ||
"\e[1G --:--:--", | ||
"\e[1G --:--:--", | ||
"\e[1G --:--:--", | ||
"\e[1G 12:00:00", | ||
"\e[1G 12:00:01\n" | ||
].join) | ||
Timecop.return | ||
end | ||
end |