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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby-progress/ripple.rb', line 87

def initialize(string, options = {})
  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

Returns the value of attribute caps.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def caps
  @caps
end

#formatObject

Returns the value of attribute format.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def format
  @format
end

#indexObject

Returns the value of attribute index.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def index
  @index
end

#inverseObject

Returns the value of attribute inverse.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def inverse
  @inverse
end

#rainbowObject

Returns the value of attribute rainbow.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def rainbow
  @rainbow
end

#speedObject

Returns the value of attribute speed.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def speed
  @speed
end

#spinnerObject

Returns the value of attribute spinner.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def spinner
  @spinner
end

#spinner_positionObject

Returns the value of attribute spinner_position.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def spinner_position
  @spinner_position
end

#stringObject

Returns the value of attribute string.



85
86
87
# File 'lib/ruby-progress/ripple.rb', line 85

def string
  @string
end

Class Method Details

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



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby-progress/ripple.rb', line 157

def self.complete(string, message, checkmark, success, icons: {})
  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)



149
150
151
# File 'lib/ruby-progress/ripple.rb', line 149

def self.hide_cursor
  RubyProgress::Utils.hide_cursor
end

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ruby-progress/ripple.rb', line 199

def self.progress(string, options = {})
  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



153
154
155
# File 'lib/ruby-progress/ripple.rb', line 153

def self.show_cursor
  RubyProgress::Utils.show_cursor
end

Instance Method Details

#advanceObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ruby-progress/ripple.rb', line 170

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



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
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby-progress/ripple.rb', line 111

def printout
  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