Class: RubyProgress::Ripple

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progress/ripple.rb

Overview

Text ripple animation class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options = {}) ⇒ Ripple

Returns a new instance of Ripple.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby-progress/ripple.rb', line 105

def initialize(string, options = {})
  # Create a new Ripple animation for the given string.
  #
  # @param string [String] the text to animate
  # @param options [Hash] configuration options (speed, format, rainbow, etc.)
  # @option options [Symbol] :speed (:medium) animation speed :fast/:medium/:slow
  # @option options [Symbol] :format (:bidirectional) :bidirectional/:forward_only
  # @option options [Boolean] :rainbow (false) colorize characters individually
  # @option options [Boolean] :spinner (false) use a spinner glyph instead of ripple
  # @return [void]
  defaults = {
    speed: :medium,
    format: :bidirectional,
    rainbow: false,
    spinner: false,
    spinner_position: false,
    caps: false,
    inverse: false,
    output: :error,
    ends: nil
  }
  @options = defaults.merge(options)
  @string = string
  @index = 0
  @direction = :forward
  @rainbow = @options[:rainbow]
  @spinner = @options[:spinner]
  @spinner_position = @options[:spinner_position]
  @caps = @options[:caps]
  @inverse = @options[:inverse]
  @start_chars, @end_chars = RubyProgress::Utils.parse_ends(@options[:ends])
end

Instance Attribute Details

#capsObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def caps
  @caps
end

#formatObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def format
  @format
end

#indexObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def index
  @index
end

#inverseObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def inverse
  @inverse
end

#rainbowObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def rainbow
  @rainbow
end

#speedObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def speed
  @speed
end

#spinnerObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def spinner
  @spinner
end

#spinner_positionObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def spinner_position
  @spinner_position
end

#stringObject

Public API:

  • instance: #advance, #printout

  • class: .progress (block-based helper), .complete



103
104
105
# File 'lib/ruby-progress/ripple.rb', line 103

def string
  @string
end

Class Method Details

.complete(string, message, checkmark, success, icons: {}) ⇒ void

This method returns an undefined value.

Show the cursor in the terminal. Delegates to Utils.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ruby-progress/ripple.rb', line 192

def self.complete(string, message, checkmark, success, icons: {})
  # Display a final completion message for the ripple indicator.
  #
  # @param string [String] fallback message
  # @param message [String,nil] explicit message to show
  # @param checkmark [Boolean] whether to show a checkmark
  # @param success [Boolean] whether the result is success
  # @param icons [Hash] optional icons mapping
  # @return [void]
  display_message = message || (checkmark ? string : nil)
  return unless display_message

  RubyProgress::Utils.display_completion(
    display_message,
    success: success,
    show_checkmark: checkmark,
    output_stream: :warn,
    icons: icons
  )
end

.hide_cursorObject

Hide or show the cursor (delegated to Utils)



180
181
182
# File 'lib/ruby-progress/ripple.rb', line 180

def self.hide_cursor
  RubyProgress::Utils.hide_cursor
end

.progress(string, options = {}) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ruby-progress/ripple.rb', line 242

def self.progress(string, options = {})
  # Block-style helper which runs the ripple animation while the
  # provided block executes. The cursor is hidden for the duration and
  # restored afterwards. This method attempts to return a sensible
  # value depending on the :output option (see code).
  #
  # @param string [String] the label text to animate
  # @param options [Hash] configuration options forwarded to Ripple.new
  # @yield the work to perform while the animation runs
  # @return [Object,nil] returns block result or boolean depending on :output
  Signal.trap('INT') do
    Thread.current.kill
    nil
  end
  defaults = { speed: :medium,
               format: :bidirectional,
               rainbow: false,
               inverse: false,
               output: :error }
  options = defaults.merge(options)

  rippler = new(string, options)
  Ripple.hide_cursor
  begin
    thread = Thread.new do
      rippler.advance while true
    end
    result = yield if block_given?
    thread.kill

    if @options[:output] == :error
      $?.exitstatus.zero?
    elsif @options[:output] == :stdout
      result
    else
      nil
    end
  rescue StandardError
    thread&.kill
    nil
  ensure
    Ripple.show_cursor
  end
end

.show_cursorObject



184
185
186
# File 'lib/ruby-progress/ripple.rb', line 184

def self.show_cursor
  RubyProgress::Utils.show_cursor
end

Instance Method Details

#advanceObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ruby-progress/ripple.rb', line 213

def advance
  max = @spinner ? (INDICATORS[@spinner].count - 1) : (@string.length - 1)
  advance = true

  if @index == max && @options[:format] != :forward_only
    @direction = :backward
  elsif @index == max && @options[:format] == :forward_only
    @index = 0
    advance = false
  elsif @index == 0
    @direction = :forward
  end

  if advance
    @index = @direction == :backward ? @index - 1 : @index + 1
  end

  printout

  case @options[:speed]
  when :fast
    sleep 0.05
  when :medium
    sleep 0.1
  else
    sleep 0.2
  end
end

#printoutObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ruby-progress/ripple.rb', line 138

def printout
  # Render a single frame of the ripple to stderr, preserving any
  # reserved output area managed by OutputCapture.
  #
  # @return [void]
  letters = @string.dup.chars
  i = @index
  if @spinner
    case @spinner_position
    when :before
      pre = "#{INDICATORS[@spinner][i]} "
      post = @string
    else
      pre = "#{@string} "
      post = INDICATORS[@spinner][i]
    end
  elsif @caps
    pre = letters.slice!(0, i).join
    char = letters.slice!(0, 2).join
    post = letters.slice!(0, letters.length).join
    pre = @inverse ? pre.upcase : pre.downcase
    char = @inverse ? char.downcase : char.upcase
    post = @inverse ? post.upcase : post.downcase
  elsif @inverse
    pre = letters.slice!(0, i).join
    pre = @rainbow ? pre.rainbow : pre.extend(StringExtensions).light_white
    char = letters.slice!(0, 2).join
    char = char.extend(StringExtensions).dark_white
    post = letters.slice!(0, letters.length).join
    post = @rainbow ? post.rainbow : post.extend(StringExtensions).light_white
  else
    pre = letters.slice!(0, i).join.extend(StringExtensions).dark_white
    char = letters.slice!(0, 2).join
    char = @rainbow ? char.rainbow(i) : char.extend(StringExtensions).light_white
    post = letters.slice!(0, letters.length).join.extend(StringExtensions).dark_white
  end
  @output_capture&.redraw($stderr)
  $stderr.print "\r\e[2K#{@start_chars}#{pre}#{char}#{post}#{@end_chars}"
  $stderr.flush
end