tty-progressbar 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 356470af4dabd6c724cfbd789049429e2e000a0c
4
- data.tar.gz: f4cdce3fbfeaa7b2bced0ffb067982a850fb7358
3
+ metadata.gz: 2eaaf2c7343d9acdfb829ee31ce58673a0e57874
4
+ data.tar.gz: 6b7b3f3e8ccef0d0ce4eadb728404b11113330b3
5
5
  SHA512:
6
- metadata.gz: 2e5ab9947f5627d78b9bb1d9471b8c166dc856c35ec224b19fd37481938bf00c1efda00dfce1ab6de056d5da7f56c72b6a5ec1acdb7008b36833adfc7f917dd1
7
- data.tar.gz: 8ff67c8713892e9ed15b7c046ce0d48c3f88fb6fae7153895d56faa9bc2f7c38c4b0ab127f6d5ceab6dd9292c81fdd55ffb3af11755a4206917bce5962d46412
6
+ metadata.gz: c51b18a4f26219bf19628fd1c406e2cfd94d6857fb236c304a535a68723828b21e86f851d8ef524453c77761cada81e1804162d5278f06149921b31c66964ffe
7
+ data.tar.gz: b6b3f6800919a9b5dbc31edf85b35c23da38760c3dd580aa611febd0870fa42424bfcb7e44b61dbdd7a14d02d08f05ccd915fb1e5a4e1399b3840e33897c1c1d
data/.travis.yml CHANGED
@@ -6,9 +6,10 @@ script: "bundle exec rake ci"
6
6
  rvm:
7
7
  - 1.9.3
8
8
  - 2.0.0
9
- - 2.1.0
10
- - 2.2.0
11
- - 2.3.0
9
+ - 2.1.10
10
+ - 2.2.6
11
+ - 2.3.3
12
+ - 2.4.0
12
13
  - ruby-head
13
14
  - jruby-9000
14
15
  - jruby-head
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.11.0] - 2017-04-04
4
+
5
+ ### Added
6
+ * Add :decimals, :separator, :unit_separator to Converter#to_bytes
7
+ * Add ability to Converter#to_bytes to calculate higher sizes TB, PB & EB
8
+
9
+ ### Changed
10
+ * Change files loading
11
+ * Change Converter to be a module
12
+
13
+ ### Fixed
14
+ * Fix :byte_rate token to correctly format bytes
15
+
3
16
  ## [v0.10.1] - 2016-12-26
4
17
 
5
18
  ### Fixed
@@ -95,6 +108,7 @@
95
108
 
96
109
  * Initial implementation and release
97
110
 
111
+ [v0.11.0]: https://github.com/peter-murach/tty-progressbar/compare/v0.10.1...v0.11.0
98
112
  [v0.10.1]: https://github.com/peter-murach/tty-progressbar/compare/v0.10.0...v0.10.1
99
113
  [v0.10.0]: https://github.com/peter-murach/tty-progressbar/compare/v0.9.0...v0.10.0
100
114
  [v0.9.0]: https://github.com/peter-murach/tty-progressbar/compare/v0.8.2...v0.9.0
data/Gemfile CHANGED
@@ -3,15 +3,15 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem 'rspec', '~> 3.4.0'
7
- gem 'yard', '~> 0.8.7'
8
- gem 'timecop', '~> 0.7.1'
9
- gem 'pastel', '~> 0.6.0'
6
+ gem 'rspec', '~> 3.5.0'
7
+ gem 'timecop', '~> 0.8.1'
8
+ gem 'pastel', '~> 0.7.0'
10
9
  gem 'coveralls', '~> 0.8.13'
11
10
  gem 'simplecov', '~> 0.11.2'
12
11
  gem 'term-ansicolor', '=1.3.2'
13
12
  end
14
13
 
15
14
  group :metrics do
15
+ gem 'yard', '~> 0.8.7'
16
16
  gem 'yardstick', '~> 0.9.9'
17
17
  end
data/README.md CHANGED
@@ -386,4 +386,4 @@ downloading [======================= ] 4.12MB/s
386
386
 
387
387
  ## Copyright
388
388
 
389
- Copyright (c) 2014-2016 Piotr Murach. See LICENSE for further details.
389
+ Copyright (c) 2014-2017 Piotr Murach. See LICENSE for further details.
@@ -1,27 +1,3 @@
1
1
  # coding: utf-8
2
2
 
3
- require 'io/console'
4
- require 'forwardable'
5
- require 'tty-screen'
6
-
7
- require 'tty/progressbar/configuration'
8
- require 'tty/progressbar/converter'
9
- require 'tty/progressbar/version'
10
- require 'tty/progressbar/pipeline'
11
- require 'tty/progressbar/formatter'
12
- require 'tty/progressbar/meter'
13
-
14
- require 'tty/progressbar/formatter/bar'
15
- require 'tty/progressbar/formatter/current'
16
- require 'tty/progressbar/formatter/current_byte'
17
- require 'tty/progressbar/formatter/elapsed'
18
- require 'tty/progressbar/formatter/estimated'
19
- require 'tty/progressbar/formatter/percent'
20
- require 'tty/progressbar/formatter/rate'
21
- require 'tty/progressbar/formatter/byte_rate'
22
- require 'tty/progressbar/formatter/mean_rate'
23
- require 'tty/progressbar/formatter/mean_byte'
24
- require 'tty/progressbar/formatter/total'
25
- require 'tty/progressbar/formatter/total_byte'
26
-
27
3
  require 'tty/progressbar'
@@ -1,5 +1,14 @@
1
1
  # coding: utf-8
2
2
 
3
+ require 'io/console'
4
+ require 'forwardable'
5
+ require 'tty-screen'
6
+
7
+ require_relative 'progressbar/configuration'
8
+ require_relative 'progressbar/formatter'
9
+ require_relative 'progressbar/meter'
10
+ require_relative 'progressbar/version'
11
+
3
12
  module TTY
4
13
  # Used for creating terminal progress bar
5
14
  #
@@ -57,7 +66,7 @@ module TTY
57
66
  # @api public
58
67
  def initialize(format, options = {})
59
68
  @format = format
60
- @configuration = Configuration.new(options)
69
+ @configuration = TTY::ProgressBar::Configuration.new(options)
61
70
  yield @configuration if block_given?
62
71
 
63
72
  @width = 0 if no_width
@@ -71,7 +80,7 @@ module TTY
71
80
  @started = false
72
81
  @tokens = {}
73
82
  @formatter = TTY::ProgressBar::Formatter.new
74
- @meter = Meter.new(options.fetch(:interval, 1))
83
+ @meter = TTY::ProgressBar::Meter.new(options.fetch(:interval, 1))
75
84
 
76
85
  @formatter.load
77
86
  end
@@ -5,7 +5,7 @@ module TTY
5
5
  # Responsible for converting values to different formats
6
6
  #
7
7
  # @api public
8
- class Converter
8
+ module Converter
9
9
  HOURSECONDS = 3600
10
10
 
11
11
  # Convert seconds to time notation
@@ -30,6 +30,7 @@ module TTY
30
30
  sprintf('%2ds', seconds)
31
31
  end
32
32
  end
33
+ module_function :to_time
33
34
 
34
35
  # Convert seconds to set precision
35
36
  #
@@ -44,30 +45,45 @@ module TTY
44
45
  precision ||= (seconds < 1 && !seconds.zero?) ? 5 : 2
45
46
  sprintf "%5.#{precision}f", seconds
46
47
  end
48
+ module_function :to_seconds
47
49
 
48
- KILOBYTE = 1024
49
- MEGABYTE = KILOBYTE * 1024
50
- GIGABYTE = MEGABYTE * 1024
50
+ BYTE_UNITS = %w(b kb mb gb tb pb eb).freeze
51
51
 
52
52
  # Convert value to bytes
53
53
  #
54
54
  # @param [Numeric] value
55
55
  # the value to convert to bytes
56
+ # @param [Hash[Symbol]] options
57
+ # @option [Integer] :decimals
58
+ # the number of decimals parts
59
+ # @option [String] :separator
60
+ # the separator to use for thousands in a number
61
+ # @option [String] :unit_separator
62
+ # the separtor to use between number and unit
56
63
  #
57
64
  # @return [String]
58
65
  #
59
66
  # @api public
60
- def to_bytes(value)
61
- if value >= GIGABYTE
62
- sprintf('%.2f', value / GIGABYTE.to_f) + 'GB'
63
- elsif value >= MEGABYTE
64
- sprintf('%.2f', value / MEGABYTE.to_f) + 'MB'
65
- elsif value >= KILOBYTE
66
- sprintf('%.2f', value / KILOBYTE.to_f) + 'KB'
67
+ def to_bytes(value, options = {})
68
+ decimals = options.fetch(:decimals) { 2 }
69
+ separator = options.fetch(:separator) { '.' }
70
+ unit_separator = options.fetch(:unit_separator) { '' }
71
+
72
+ base = 1024
73
+ pattern = "%.#{decimals}f"
74
+
75
+ unit = BYTE_UNITS.find.with_index { |_, i| value < base ** (i + 1) }
76
+
77
+ if value < base
78
+ formatted_value = value.to_i.to_s
67
79
  else
68
- value.to_s + 'B'
80
+ value_to_size = value / (base ** BYTE_UNITS.index(unit)).to_f
81
+ formatted_value = format(pattern, value_to_size)
69
82
  end
83
+
84
+ formatted_value.gsub(/\./, separator) + unit_separator + unit.to_s.upcase
70
85
  end
86
+ module_function :to_bytes
71
87
  end # Converter
72
88
  end # ProgressBar
73
89
  end # TTY
@@ -1,5 +1,20 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative 'pipeline'
4
+
5
+ require_relative 'formatter/bar'
6
+ require_relative 'formatter/current'
7
+ require_relative 'formatter/current_byte'
8
+ require_relative 'formatter/elapsed'
9
+ require_relative 'formatter/estimated'
10
+ require_relative 'formatter/percent'
11
+ require_relative 'formatter/rate'
12
+ require_relative 'formatter/byte_rate'
13
+ require_relative 'formatter/mean_rate'
14
+ require_relative 'formatter/mean_byte'
15
+ require_relative 'formatter/total'
16
+ require_relative 'formatter/total_byte'
17
+
3
18
  module TTY
4
19
  class ProgressBar
5
20
  class Formatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :byte_rate token
@@ -9,8 +11,7 @@ module TTY
9
11
  MATCHER = /:byte_rate/i
10
12
 
11
13
  def initialize(progress)
12
- @progress = progress
13
- @converter = Converter.new
14
+ @progress = progress
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -31,7 +32,7 @@ module TTY
31
32
  #
32
33
  # @api public
33
34
  def format(value)
34
- formatted = @converter.to_bytes(@progress.rate)
35
+ formatted = Converter.to_bytes(@progress.rate)
35
36
  value.gsub(MATCHER, formatted)
36
37
  end
37
38
  end # ByteRateFormatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :byte and :current_byte tokens
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -25,7 +26,7 @@ module TTY
25
26
  end
26
27
 
27
28
  def format(value)
28
- bytes = @converter.to_bytes(@progress.current)
29
+ bytes = Converter.to_bytes(@progress.current)
29
30
  value.gsub(MATCHER, bytes)
30
31
  end
31
32
  end # ByteFormatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :elapsed token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -26,7 +27,7 @@ module TTY
26
27
 
27
28
  def format(value)
28
29
  elapsed = (Time.now - @progress.start_at)
29
- value.gsub(MATCHER, @converter.to_time(elapsed))
30
+ value.gsub(MATCHER, Converter.to_time(elapsed))
30
31
  end
31
32
  end # ElapsedFormatter
32
33
  end # ProgressBar
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :eta token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -28,7 +29,7 @@ module TTY
28
29
  elapsed = Time.now - @progress.start_at
29
30
  estimated = (elapsed / @progress.ratio).to_f - elapsed
30
31
  estimated = (estimated.infinite? || estimated < 0) ? 0.0 : estimated
31
- value.gsub(MATCHER, @converter.to_time(estimated))
32
+ value.gsub(MATCHER, Converter.to_time(estimated))
32
33
  end
33
34
  end # ElapsedFormatter
34
35
  end # ProgressBar
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :mean_byte token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -31,7 +32,7 @@ module TTY
31
32
  #
32
33
  # @api public
33
34
  def format(value)
34
- formatted = @converter.to_bytes(@progress.mean_rate)
35
+ formatted = Converter.to_bytes(@progress.mean_rate)
35
36
  value.gsub(MATCHER, formatted)
36
37
  end
37
38
  end # MeanByteFormatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :mean_rate token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -31,7 +32,7 @@ module TTY
31
32
  #
32
33
  # @api public
33
34
  def format(value)
34
- formatted = @converter.to_seconds(@progress.mean_rate)
35
+ formatted = Converter.to_seconds(@progress.mean_rate)
35
36
  value.gsub(MATCHER, formatted)
36
37
  end
37
38
  end # MeanRateFormatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :rate token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -31,7 +32,7 @@ module TTY
31
32
  #
32
33
  # @api public
33
34
  def format(value)
34
- formatted = @converter.to_seconds(@progress.rate)
35
+ formatted = Converter.to_seconds(@progress.rate)
35
36
  value.gsub(MATCHER, formatted)
36
37
  end
37
38
  end # RateFormatter
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
+ require_relative '../converter'
4
+
3
5
  module TTY
4
6
  class ProgressBar
5
7
  # Used by {Pipeline} to format :total_byte token
@@ -10,7 +12,6 @@ module TTY
10
12
 
11
13
  def initialize(progress, *args, &block)
12
14
  @progress = progress
13
- @converter = Converter.new
14
15
  end
15
16
 
16
17
  # Determines whether this formatter is applied or not.
@@ -25,7 +26,7 @@ module TTY
25
26
  end
26
27
 
27
28
  def format(value)
28
- bytes = @converter.to_bytes(@progress.total)
29
+ bytes = Converter.to_bytes(@progress.total)
29
30
  value.gsub(MATCHER, bytes)
30
31
  end
31
32
  end # TotalByteFormatter
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class ProgressBar
5
- VERSION = '0.10.1'
5
+ VERSION = '0.11.0'
6
6
  end # ProgressBar
7
7
  end # TTY
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  RSpec.describe TTY::ProgressBar::Converter, '#to_bytes' do
4
- subject(:converter) { described_class.new }
4
+ subject(:converter) { described_class }
5
5
 
6
6
  it "converts 1000 to bytes" do
7
7
  expect(converter.to_bytes(1000)).to eq('1000B')
@@ -11,10 +11,34 @@ RSpec.describe TTY::ProgressBar::Converter, '#to_bytes' do
11
11
  expect(converter.to_bytes(1024)).to eq('1.00KB')
12
12
  end
13
13
 
14
+ it "converts 1234 to bytes" do
15
+ expect(converter.to_bytes(1234)).to eq('1.21KB')
16
+ end
17
+
18
+ it "converts 12345 to bytes" do
19
+ expect(converter.to_bytes(12345)).to eq('12.06KB')
20
+ end
21
+
14
22
  it "converts 2000 to bytes" do
15
23
  expect(converter.to_bytes(2000)).to eq('1.95KB')
16
24
  end
17
25
 
26
+ it "converts 1234567 to bytes" do
27
+ expect(converter.to_bytes(1234567)).to eq('1.18MB')
28
+ end
29
+
30
+ it "converts 1234567 to bytes with :separator" do
31
+ expect(converter.to_bytes(1234567, separator: ',')).to eq('1,18MB')
32
+ end
33
+
34
+ it "converts 1234567 to bytes with :unit_separator" do
35
+ expect(converter.to_bytes(1234567, unit_separator: ' ')).to eq('1.18 MB')
36
+ end
37
+
38
+ it "converts 1234567 to bytes with comma as a separator" do
39
+ expect(converter.to_bytes(1234567, decimals: 1)).to eq('1.2MB')
40
+ end
41
+
18
42
  it "converts 10_000_000 to bytes" do
19
43
  expect(converter.to_bytes(10_000_000)).to eq('9.54MB')
20
44
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  RSpec.describe TTY::ProgressBar::Converter, '#to_seconds' do
4
- subject(:converter) { described_class.new }
4
+ subject(:converter) { described_class }
5
5
 
6
6
  it "ensure 5 digit precision for < 1" do
7
7
  expect(converter.to_seconds(0.000005)).to eq("0.00001")
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  RSpec.describe TTY::ProgressBar::Converter, '#to_time' do
4
- subject(:converter) { described_class.new }
4
+ subject(:converter) { described_class }
5
5
 
6
6
  it "converts seconds to seconds" do
7
7
  expect(converter.to_time(15)).to eq("15s")
@@ -24,8 +24,8 @@ RSpec.describe TTY::ProgressBar, ':byte_rate token' do
24
24
  output.rewind
25
25
  expect(output.read).to eq([
26
26
  "\e[1G0B",
27
- "\e[1G500.0B",
28
- "\e[1G1000.0B",
27
+ "\e[1G500B",
28
+ "\e[1G1000B",
29
29
  "\e[1G1.46KB",
30
30
  "\e[1G1.95KB\n"
31
31
  ].join)
@@ -24,9 +24,9 @@ RSpec.describe TTY::ProgressBar, ':mean_byte token' do
24
24
  output.rewind
25
25
  expect(output.read).to eq([
26
26
  "\e[1G0B",
27
- "\e[1G500.0B",
28
- "\e[1G750.0B",
29
- "\e[1G1000.0B",
27
+ "\e[1G500B",
28
+ "\e[1G750B",
29
+ "\e[1G1000B",
30
30
  "\e[1G1.22KB\n"
31
31
  ].join)
32
32
  Timecop.return
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-progressbar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-26 00:00:00.000000000 Z
11
+ date: 2017-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-screen