Class: ProgressBar::Format::Base
- Inherits:
-
Object
- Object
- ProgressBar::Format::Base
- Defined in:
- lib/ruby-progressbar/format/base.rb
Instance Attribute Summary collapse
-
#molecules ⇒ Object
readonly
Returns the value of attribute molecules.
Instance Method Summary collapse
-
#initialize(format_string) ⇒ Base
constructor
A new instance of Base.
- #process(environment) ⇒ Object
Constructor Details
#initialize(format_string) ⇒ Base
Returns a new instance of Base.
6 7 8 9 |
# File 'lib/ruby-progressbar/format/base.rb', line 6 def initialize(format_string) @format_string = format_string @molecules = parse(format_string) end |
Instance Attribute Details
#molecules ⇒ Object (readonly)
Returns the value of attribute molecules.
4 5 6 |
# File 'lib/ruby-progressbar/format/base.rb', line 4 def molecules @molecules end |
Instance Method Details
#process(environment) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby-progressbar/format/base.rb', line 11 def process(environment) processed_string = @format_string.dup ansi_sgr_codes = %r{\e\[[\d;]+m} .each do |molecule| processed_string.gsub!("%#{molecule.key}", environment.send(molecule.method_name).to_s) end remaining_molecules = .size placeholder_length = remaining_molecules * 2 processed_string.gsub! '%%', '%' processed_string_length = processed_string.gsub(ansi_sgr_codes, '').length = environment.send(:length) - processed_string_length + placeholder_length = < 0 ? 0 : .each do |molecule| processed_string.gsub!("%#{molecule.key}", environment.send(molecule.method_name, ).to_s) end processed_string end |