Module: RubyProgress::StringExtensions

Included in:
String
Defined in:
lib/ruby-progress/ripple.rb

Overview

String extensions for color support

Instance Method Summary collapse

Instance Method Details

#normalize_typeObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby-progress/ripple.rb', line 84

def normalize_type
  # Attempt to guess a spinner indicator type from this string's
  # characters. Returns a symbol matching one of INDICATORS keys or
  # :classic as fallback.
  #
  # @return [Symbol]
  spinner_type = :classic
  INDICATORS.each do |spinner, _v|
    spinner_type = spinner if spinner =~ /^#{chars.join('.*?')}/i
  end
  spinner_type
end

#rainbow(index = 0) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby-progress/ripple.rb', line 70

def rainbow(index = 0)
  # Apply a per-character rainbow by cycling through the COLORS map.
  #
  # @param index [Integer] an optional offset to shift the colors
  # @return [String] colorized string where each character is wrapped
  #   in a terminal color escape sequence
  chars = self.chars
  colored_chars = chars.map.with_index do |char, idx|
    color = COLORS.values[(idx + index) % COLORS.size]
    "#{color}#{char}#{COLORS['reset']}"
  end
  colored_chars.join
end