Skip to content

Commit

Permalink
Change to rename formatter format method to call
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jan 6, 2021
1 parent 499e353 commit c3feb66
Show file tree
Hide file tree
Showing 16 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ class TimeFormatter
end
```

Next, add `format` method that will substitute the matched token with an actual value. For example, to see the time elapsed since the start do:
Next, add `call` method that will substitute the matched token with an actual value. For example, to see the time elapsed since the start do:

```ruby
class TimeFormatter
include TTY::ProgressBar::Formatter[/:time/i]

def format(value) # specify how display string is formatted
def call(value) # specify how display string is formatted
# access current progress bar instance to read start time
transformed = (Time.now - @progress.start_at).to_s
value.gsub(/:time/, transformed) # replace :time token with a value
transformed = (Time.now - progress.start_at).to_s
value.gsub(matcher, transformed) # replace :time token with a value
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BarFormatter
# the value being formatted
#
# @api public
def format(value)
def call(value)
without_bar = value.gsub(/:bar/, "")
available_space = [0, ProgressBar.max_columns -
ProgressBar.display_columns(without_bar) -
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/byte_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ByteRateFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
formatted = Converter.to_bytes(@progress.rate)
value.gsub(matcher, formatted)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/current.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CurrentFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
value.gsub(matcher, @progress.current.to_s)
end
end # CurrentFormatter
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/current_byte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ByteFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
bytes = Converter.to_bytes(@progress.current)
value.gsub(matcher, bytes)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/elapsed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ElapsedFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
elapsed = (Time.now - @progress.start_at)
value.gsub(matcher, Converter.to_time(elapsed))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/estimated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EstimatedFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
if @progress.indeterminate?
return value.gsub(matcher, "--s")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/estimated_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EstimatedTimeFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
if @progress.indeterminate?
return value.gsub(matcher, "--:--:--")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/mean_byte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MeanByteFormatter
# the value being formatted
#
# @api public
def format(value)
def call(value)
formatted = Converter.to_bytes(@progress.mean_rate)
value.gsub(matcher, formatted)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/mean_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MeanRateFormatter
# the value being formatted
#
# @api public
def format(value)
def call(value)
formatted = Converter.to_seconds(@progress.mean_rate)
value.gsub(matcher, formatted)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/percent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PercentFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
percent = @progress.width == 0 ? 100 : (@progress.ratio * 100).to_i
display = @progress.indeterminate? ? "-" : percent.to_s
value.gsub(matcher, "#{display}%")
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RateFormatter
# the value being formatted
#
# @api public
def format(value)
def call(value)
formatted = Converter.to_seconds(@progress.rate)
value.gsub(matcher, formatted)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/total.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TotalFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
display = @progress.indeterminate? ? "-" : @progress.total.to_s
value.gsub(matcher, display)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/formatter/total_byte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TotalByteFormatter
# the value to format
#
# @api public
def format(value)
def call(value)
bytes = if @progress.indeterminate?
"-B"
else
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/progressbar/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def decorate(tokenized)
base = tokenized.dup
formatters.inject(base) do |formatted, formatter|
if formatter.respond_to?(:matches?) && formatter.matches?(formatted)
formatter.format(formatted)
formatter.(formatted)
else
formatted
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/custom_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def matches?(value)
value.to_s =~ /:hi/
end

def format(value)
def call(value)
value.gsub(/:hi/, "Hello")
end
end)
Expand Down

0 comments on commit c3feb66

Please sign in to comment.