tty-progressbar 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +492 -126
  5. data/lib/tty-progressbar.rb +2 -2
  6. data/lib/tty/progressbar.rb +168 -68
  7. data/lib/tty/progressbar/configuration.rb +121 -27
  8. data/lib/tty/progressbar/converter.rb +16 -19
  9. data/lib/tty/progressbar/formats.rb +120 -0
  10. data/lib/tty/progressbar/formatter.rb +33 -38
  11. data/lib/tty/progressbar/formatter/bar.rb +74 -27
  12. data/lib/tty/progressbar/formatter/byte_rate.rb +6 -20
  13. data/lib/tty/progressbar/formatter/current.rb +4 -19
  14. data/lib/tty/progressbar/formatter/current_byte.rb +9 -17
  15. data/lib/tty/progressbar/formatter/elapsed.rb +9 -18
  16. data/lib/tty/progressbar/formatter/estimated.rb +14 -18
  17. data/lib/tty/progressbar/formatter/estimated_time.rb +37 -0
  18. data/lib/tty/progressbar/formatter/mean_byte.rb +6 -20
  19. data/lib/tty/progressbar/formatter/mean_rate.rb +6 -20
  20. data/lib/tty/progressbar/formatter/percent.rb +10 -16
  21. data/lib/tty/progressbar/formatter/rate.rb +5 -19
  22. data/lib/tty/progressbar/formatter/total.rb +10 -16
  23. data/lib/tty/progressbar/formatter/total_byte.rb +14 -18
  24. data/lib/tty/progressbar/formatters.rb +53 -0
  25. data/lib/tty/progressbar/meter.rb +2 -2
  26. data/lib/tty/progressbar/multi.rb +61 -21
  27. data/lib/tty/progressbar/pipeline.rb +13 -6
  28. data/lib/tty/progressbar/timer.rb +89 -0
  29. data/lib/tty/progressbar/version.rb +1 -1
  30. metadata +44 -103
  31. data/Rakefile +0 -8
  32. data/examples/color.rb +0 -18
  33. data/examples/failure.rb +0 -12
  34. data/examples/iterator.rb +0 -5
  35. data/examples/lazy.rb +0 -6
  36. data/examples/multi/main_bar.rb +0 -13
  37. data/examples/multi/simple.rb +0 -13
  38. data/examples/multi/width.rb +0 -13
  39. data/examples/simple.rb +0 -7
  40. data/examples/slow_process.rb +0 -29
  41. data/examples/speed.rb +0 -11
  42. data/examples/threaded.rb +0 -14
  43. data/examples/tokens.rb +0 -12
  44. data/examples/unicode.rb +0 -7
  45. data/spec/spec_helper.rb +0 -53
  46. data/spec/unit/advance_spec.rb +0 -25
  47. data/spec/unit/clear_spec.rb +0 -17
  48. data/spec/unit/complete_spec.rb +0 -16
  49. data/spec/unit/converter/to_bytes_spec.rb +0 -47
  50. data/spec/unit/converter/to_seconds_spec.rb +0 -15
  51. data/spec/unit/converter/to_time_spec.rb +0 -19
  52. data/spec/unit/custom_formatter_spec.rb +0 -26
  53. data/spec/unit/custom_token_spec.rb +0 -14
  54. data/spec/unit/events_spec.rb +0 -33
  55. data/spec/unit/finish_spec.rb +0 -15
  56. data/spec/unit/formatter/bar_spec.rb +0 -33
  57. data/spec/unit/formatter/byte_rate_spec.rb +0 -32
  58. data/spec/unit/formatter/current_byte_spec.rb +0 -16
  59. data/spec/unit/formatter/current_spec.rb +0 -14
  60. data/spec/unit/formatter/elapsed_spec.rb +0 -58
  61. data/spec/unit/formatter/estimated_spec.rb +0 -27
  62. data/spec/unit/formatter/mean_byte_spec.rb +0 -32
  63. data/spec/unit/formatter/mean_rate_spec.rb +0 -31
  64. data/spec/unit/formatter/percent_spec.rb +0 -16
  65. data/spec/unit/formatter/rate_spec.rb +0 -31
  66. data/spec/unit/formatter/total_byte_spec.rb +0 -16
  67. data/spec/unit/formatter/total_spec.rb +0 -16
  68. data/spec/unit/frequency_spec.rb +0 -27
  69. data/spec/unit/head_spec.rb +0 -32
  70. data/spec/unit/hide_cursor_spec.rb +0 -27
  71. data/spec/unit/inspect_spec.rb +0 -11
  72. data/spec/unit/iterate_spec.rb +0 -79
  73. data/spec/unit/log_spec.rb +0 -29
  74. data/spec/unit/meter_spec.rb +0 -70
  75. data/spec/unit/multi/advance_spec.rb +0 -123
  76. data/spec/unit/multi/events_spec.rb +0 -115
  77. data/spec/unit/multi/finish_spec.rb +0 -41
  78. data/spec/unit/multi/line_inset_spec.rb +0 -65
  79. data/spec/unit/multi/register_spec.rb +0 -35
  80. data/spec/unit/multi/reset_spec.rb +0 -28
  81. data/spec/unit/multi/stop_spec.rb +0 -15
  82. data/spec/unit/multi/width_spec.rb +0 -118
  83. data/spec/unit/new_spec.rb +0 -76
  84. data/spec/unit/pipeline_spec.rb +0 -19
  85. data/spec/unit/ratio_spec.rb +0 -31
  86. data/spec/unit/render_spec.rb +0 -25
  87. data/spec/unit/reset_spec.rb +0 -31
  88. data/spec/unit/resize_spec.rb +0 -35
  89. data/spec/unit/set_current_spec.rb +0 -43
  90. data/spec/unit/start_spec.rb +0 -14
  91. data/spec/unit/stop_spec.rb +0 -19
  92. data/spec/unit/update_spec.rb +0 -22
  93. data/spec/unit/width_spec.rb +0 -86
  94. data/tasks/console.rake +0 -9
  95. data/tasks/coverage.rake +0 -9
  96. data/tasks/spec.rake +0 -27
  97. data/tty-progressbar.gemspec +0 -32
@@ -1,13 +0,0 @@
1
- require_relative '../../lib/tty-progressbar'
2
-
3
- bars = TTY::ProgressBar::Multi.new("main [:bar] :percent")
4
-
5
- bar1 = bars.register "foo [:bar] :percent", total: 150
6
- bar2 = bars.register "bar [:bar] :percent", total: 250
7
- bar3 = bars.register "baz [:bar] :percent", total: 100
8
-
9
- th1 = Thread.new { 15.times { sleep(0.1); bar1.advance(10) } }
10
- th2 = Thread.new { 50.times { sleep(0.1); bar2.advance(5)} }
11
- th3 = Thread.new { 50.times { sleep(0.1); bar3.advance(5) } }
12
-
13
- [th1, th2, th3].each(&:join)
@@ -1,7 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- bar = TTY::ProgressBar.new("downloading [:bar] :elapsed :percent", total: 30)
4
- 30.times do
5
- sleep(0.1)
6
- bar.advance
7
- end
@@ -1,29 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- CONTENT_SIZE = 2048
4
- CHUNK_SIZE = 255
5
-
6
- # Dummy "long responding server"
7
- def download_from_server(offset, limit)
8
- sleep(0.1)
9
- "<chunk #{offset}..#{offset + limit}>"
10
- end
11
-
12
- def download_finished?(position)
13
- position >= CONTENT_SIZE
14
- end
15
-
16
- downloader = Enumerator.new do |y|
17
- start = 0
18
- loop do
19
- y.yield(download_from_server(start, CHUNK_SIZE))
20
- start += CHUNK_SIZE
21
- raise StopIteration if download_finished?(start)
22
- end
23
- end
24
-
25
- bar = TTY::ProgressBar.new("[:bar] :current_byte/:total_byte", total: CONTENT_SIZE)
26
-
27
- response = bar.iterate(downloader, CHUNK_SIZE).to_a.join
28
-
29
- puts response
@@ -1,11 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- bar = TTY::ProgressBar.new "downloading [:bar] :rate/s :mean_rate/s" do |conf|
4
- conf.total = 100
5
- conf.interval = 1
6
- end
7
-
8
- 30.times do
9
- sleep(0.1)
10
- bar.advance(Random.rand(10))
11
- end
@@ -1,14 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- threads = []
4
-
5
- bar = TTY::ProgressBar.new("[:bar] :percent", total: 30)
6
-
7
- threads << Thread.new {
8
- 15.times { sleep(0.1); bar.update(complete: '-', head: '-'); bar.advance(); }
9
- }
10
- threads << Thread.new {
11
- 15.times { sleep(0.1); bar.update(complete: '+', head: '+'); bar.advance(); }
12
- }
13
-
14
- threads.map(&:join)
@@ -1,12 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- files = [
4
- 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt',
5
- 'file6.txt', 'file7.txt', 'file8.txt', 'file9.txt', 'file10.txt'
6
- ]
7
-
8
- bar = TTY::ProgressBar.new("downloading :file :percent", total: files.size)
9
- 10.times do |num|
10
- sleep(0.1)
11
- bar.advance(file: files[num])
12
- end
@@ -1,7 +0,0 @@
1
- require_relative '../lib/tty-progressbar'
2
-
3
- bar = TTY::ProgressBar.new("Unicode [:bar]", total: 30, complete: 'あ')
4
- 30.times do
5
- sleep(0.1)
6
- bar.advance
7
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV['COVERAGE'] || ENV['TRAVIS']
4
- require 'simplecov'
5
- require 'coveralls'
6
-
7
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
8
- SimpleCov::Formatter::HTMLFormatter,
9
- Coveralls::SimpleCov::Formatter
10
- ])
11
-
12
- SimpleCov.start do
13
- command_name 'spec'
14
- add_filter 'spec'
15
- end
16
- end
17
-
18
- require 'timecop'
19
- require 'tty-progressbar'
20
-
21
- class StringIO
22
- undef_method :tty?
23
- def tty?
24
- true
25
- end
26
- end
27
-
28
- RSpec.configure do |config|
29
- config.expect_with :rspec do |expectations|
30
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
- end
32
-
33
- config.mock_with :rspec do |mocks|
34
- mocks.verify_partial_doubles = true
35
- end
36
-
37
- # Limits the available syntax to the non-monkey patched syntax that is recommended.
38
- config.disable_monkey_patching!
39
-
40
- # This setting enables warnings. It's recommended, but in some cases may
41
- # be too noisy due to issues in dependencies.
42
- config.warnings = true
43
-
44
- if config.files_to_run.one?
45
- config.default_formatter = 'doc'
46
- end
47
-
48
- config.profile_examples = 2
49
-
50
- config.order = :random
51
-
52
- Kernel.srand config.seed
53
- end
@@ -1,25 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, '#advance' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "advances by custom value" do
5
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
6
- progress.advance(8)
7
- expect(progress.current).to eq(8)
8
- end
9
-
10
- it "allows to go back" do
11
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
12
- 5.times { progress.advance(1) }
13
- expect(progress.current).to eq(5)
14
- 5.times { progress.advance(-1) }
15
- expect(progress.current).to eq(0)
16
- end
17
-
18
- it "cannot backtrack on finished" do
19
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
20
- 10.times { progress.advance(1) }
21
- expect(progress.current).to eq(10)
22
- 5.times { progress.advance(-1) }
23
- expect(progress.current).to eq(10)
24
- end
25
- end
@@ -1,17 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, 'clear' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "clears progress bar when finished" do
5
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 5,
6
- clear: true)
7
- 5.times { progress.advance }
8
- output.rewind
9
- expect(output.read).to eq([
10
- "\e[1G[= ]",
11
- "\e[1G[== ]",
12
- "\e[1G[=== ]",
13
- "\e[1G[==== ]",
14
- "\e[1G[=====]\e[0m\e[2K\e[1G"
15
- ].join)
16
- end
17
- end
@@ -1,16 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, '#complete?' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "checks for completness" do
5
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 3)
6
- completes = []
7
- 3.times do
8
- completes << progress.complete?
9
- progress.advance
10
- end
11
- completes << progress.complete?
12
- expect(completes).to eq([
13
- false, false, false, true
14
- ])
15
- end
16
- end
@@ -1,47 +0,0 @@
1
- RSpec.describe TTY::ProgressBar::Converter, '#to_bytes' do
2
- subject(:converter) { described_class }
3
-
4
- it "converts 1000 to bytes" do
5
- expect(converter.to_bytes(1000)).to eq('1000B')
6
- end
7
-
8
- it "converts 1024 to bytes" do
9
- expect(converter.to_bytes(1024)).to eq('1.00KB')
10
- end
11
-
12
- it "converts 1234 to bytes" do
13
- expect(converter.to_bytes(1234)).to eq('1.21KB')
14
- end
15
-
16
- it "converts 12345 to bytes" do
17
- expect(converter.to_bytes(12345)).to eq('12.06KB')
18
- end
19
-
20
- it "converts 2000 to bytes" do
21
- expect(converter.to_bytes(2000)).to eq('1.95KB')
22
- end
23
-
24
- it "converts 1234567 to bytes" do
25
- expect(converter.to_bytes(1234567)).to eq('1.18MB')
26
- end
27
-
28
- it "converts 1234567 to bytes with :separator" do
29
- expect(converter.to_bytes(1234567, separator: ',')).to eq('1,18MB')
30
- end
31
-
32
- it "converts 1234567 to bytes with :unit_separator" do
33
- expect(converter.to_bytes(1234567, unit_separator: ' ')).to eq('1.18 MB')
34
- end
35
-
36
- it "converts 1234567 to bytes with comma as a separator" do
37
- expect(converter.to_bytes(1234567, decimals: 1)).to eq('1.2MB')
38
- end
39
-
40
- it "converts 10_000_000 to bytes" do
41
- expect(converter.to_bytes(10_000_000)).to eq('9.54MB')
42
- end
43
-
44
- it "convert 10_000_000_000 to bytes" do
45
- expect(converter.to_bytes(10_000_000_000)).to eq('9.31GB')
46
- end
47
- end
@@ -1,15 +0,0 @@
1
- RSpec.describe TTY::ProgressBar::Converter, '#to_seconds' do
2
- subject(:converter) { described_class }
3
-
4
- it "ensure 5 digit precision for < 1" do
5
- expect(converter.to_seconds(0.000005)).to eq("0.00001")
6
- end
7
-
8
- it "rounds 0 to 0.00" do
9
- expect(converter.to_seconds(0)).to eq(" 0.00")
10
- end
11
-
12
- it "ensures 2 digit precision for > 1" do
13
- expect(converter.to_seconds(11.2)).to eq("11.20")
14
- end
15
- end
@@ -1,19 +0,0 @@
1
- RSpec.describe TTY::ProgressBar::Converter, '#to_time' do
2
- subject(:converter) { described_class }
3
-
4
- it "converts seconds to seconds" do
5
- expect(converter.to_time(15)).to eq("15s")
6
- end
7
-
8
- it "converts seconds to minutes" do
9
- expect(converter.to_time(100)).to eq(" 1m40s")
10
- end
11
-
12
- it "converts seconds to small hours" do
13
- expect(converter.to_time(3600)).to eq(" 1h 0m")
14
- end
15
-
16
- it "converts secodns to hours" do
17
- expect(converter.to_time(100 * 3600)).to eq("100h")
18
- end
19
- end
@@ -1,26 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, 'custom formatter' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "allows for custom tag" do
5
- progress = TTY::ProgressBar.new(":hi", output: output, total: 10)
6
-
7
- stub_const("HiFormatter", Class.new do
8
- def initialize(progress)
9
- @progress = progress
10
- end
11
-
12
- def matches?(value)
13
- value.to_s =~ /:hi/
14
- end
15
-
16
- def format(value)
17
- value.gsub(/:hi/, "Hello")
18
- end
19
- end)
20
-
21
- progress.use(HiFormatter)
22
- progress.advance
23
- output.rewind
24
- expect(output.read).to eq("\e[1GHello")
25
- end
26
- end
@@ -1,14 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, 'custom token' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "allows to specify custom tokens" do
5
- progress = TTY::ProgressBar.new("(:current) :title", output: output, total: 4)
6
- progress.advance(title: 'Hello Piotr!')
7
- progress.advance(3, title: 'Bye Piotr!')
8
- output.rewind
9
- expect(output.read).to eq([
10
- "\e[1G(1) Hello Piotr!",
11
- "\e[1G(4) Bye Piotr! \n"
12
- ].join)
13
- end
14
- end
@@ -1,33 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, 'events' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "emits :progress event when advancing" do
5
- events = []
6
- bar = TTY::ProgressBar.new("[:bar]", output: output, total: 5)
7
- bar.on(:progress) { events << :progress }
8
-
9
- bar.advance
10
-
11
- expect(events).to eq([:progress])
12
- end
13
-
14
- it "emits :done event when finished" do
15
- events = []
16
- bar = TTY::ProgressBar.new("[:bar]", output: output, total: 5)
17
- bar.on(:done) { events << :done }
18
-
19
- bar.finish
20
-
21
- expect(events).to eq([:done])
22
- end
23
-
24
- it "emits :stopped event" do
25
- events = []
26
- bar = TTY::ProgressBar.new("[:bar]", output: output, total: 5)
27
- bar.on(:stopped) { events << :stopped }
28
-
29
- bar.stop
30
-
31
- expect(events).to eq([:stopped])
32
- end
33
- end
@@ -1,15 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, '#finish' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it 'finishes progress' do
5
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
6
- progress.advance
7
- progress.finish
8
- expect(progress.complete?).to be(true)
9
- output.rewind
10
- expect(output.read).to eq([
11
- "\e[1G[= ]",
12
- "\e[1G[==========]\n"
13
- ].join)
14
- end
15
- end
@@ -1,33 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, ':bar token' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- it "animates bar" do
5
- progress = TTY::ProgressBar.new("[:bar]", output: output, total: 5)
6
- 5.times { progress.advance }
7
- output.rewind
8
- expect(output.read).to eq([
9
- "\e[1G[= ]",
10
- "\e[1G[== ]",
11
- "\e[1G[=== ]",
12
- "\e[1G[==== ]",
13
- "\e[1G[=====]\n"
14
- ].join)
15
- end
16
-
17
- it "animates colors correctly" do
18
- red = "\e[31m \e[0m"
19
- green = "\e[32m \e[0m"
20
- progress = TTY::ProgressBar.new("[:bar]", total: 5, complete: green,
21
- incomplete: red, output: output)
22
-
23
- 5.times { progress.advance }
24
- output.rewind
25
- expect(output.read).to eq([
26
- "\e[1G[#{green}#{red}#{red}#{red}#{red}]",
27
- "\e[1G[#{green}#{green}#{red}#{red}#{red}]",
28
- "\e[1G[#{green}#{green}#{green}#{red}#{red}]",
29
- "\e[1G[#{green}#{green}#{green}#{green}#{red}]",
30
- "\e[1G[#{green}#{green}#{green}#{green}#{green}]\n",
31
- ].join)
32
- end
33
- end
@@ -1,32 +0,0 @@
1
- RSpec.describe TTY::ProgressBar, ':byte_rate token' do
2
- let(:output) { StringIO.new('', 'w+') }
3
-
4
- before { Timecop.safe_mode = false }
5
-
6
- it "shows current rate in bytes per sec" do
7
- time_now = Time.local(2014, 10, 5, 12, 0, 0)
8
- Timecop.freeze(time_now)
9
- progress = TTY::ProgressBar.new(":byte_rate", output: output, total: 10000, interval: 1)
10
- # Generate a serie of advances at 2s intervals
11
- # t+0 advance=0 total=0
12
- # t+2 advance=1000 total=1000
13
- # t+4 advance=2000 total=3000
14
- # t+6 advance=3000 total=6000
15
- # t+8 advance=4000 total=10_000
16
- # NOTE: mean_byte uses 1024 for the scale in K, M ...
17
- 5.times do |i|
18
- time_now = Time.local(2014, 10, 5, 12, 0, i * 2)
19
- Timecop.freeze(time_now)
20
- progress.advance(i * 1000)
21
- end
22
- output.rewind
23
- expect(output.read).to eq([
24
- "\e[1G0B",
25
- "\e[1G500B",
26
- "\e[1G1000B",
27
- "\e[1G1.46KB",
28
- "\e[1G1.95KB\n"
29
- ].join)
30
- Timecop.return
31
- end
32
- end