1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 137 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 234 235 236 237 238 239 240 241 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
# frozen_string_literal: true
require 'monitor'
require 'forwardable'
require_relative '../spinner'
module TTY
class Spinner
# Used for managing multiple terminal spinners
#
# @api public
class Multi
include Enumerable
include MonitorMixin
extend Forwardable
def_delegators :@spinners, :each, :empty?, :length
DEFAULT_INSET = {
top: Gem.win_platform? ? '+ ' : "\u250c ",
middle: Gem.win_platform? ? '|-- ' : "\u251c\u2500\u2500 ",
bottom: Gem.win_platform? ? '|__ ' : "\u2514\u2500\u2500 "
}
# The current count of all rendered rows
#
# @api public
attr_reader :rows
# Initialize a multispinner
#
# @example
# spinner = TTY::Spinner::Multi.new
#
# @param [String] message
# the optional message to print in front of the top level spinner
#
# @param [Hash] options
# @option options [Hash] :style
# keys :top :middle and :bottom can contain Strings that are used to
# indent the spinners. Ignored if message is blank
# @option options [Object] :output
# the object that responds to print call defaulting to stderr
# @option options [Boolean] :hide_cursor
# display or hide cursor
# @option options [Boolean] :clear
# clear ouptut when finished
# @option options [Float] :interval
# the interval for auto spinning
#
# @api public
def initialize(*args)
super()
@options = args.last.is_a?(::Hash) ? args.pop : {}
message = args.empty? ? nil : args.pop
@inset_opts = @options.delete(:style) { DEFAULT_INSET }
@rows = 0
@spinners = []
@spinners_count = 0
@top_spinner = nil
@last_spin_at = nil
unless message.nil?
@top_spinner = register(message, observable: false, row: next_row)
end
@callbacks = {
success: [],
error: [],
done: [],
spin: []
}
end
# Register a new spinner
#
# @param [String, TTY::Spinner] pattern_or_spinner
# the pattern used for creating spinner, or a spinner instance
#
# @api public
def register(pattern_or_spinner, **options, &job)
observable = options.delete(:observable) { true }
spinner = nil
synchronize do
spinner = create_spinner(pattern_or_spinner, options)
spinner.attach_to(self)
spinner.job(&job) if block_given?
observe(spinner) if observable
@spinners << spinner
@spinners_count += 1
if @top_spinner
@spinners.each { |sp| sp.redraw_indent if sp.spinning? || sp.done? }
end
end
spinner
end
# Create a spinner instance
#
# @api private
def create_spinner(pattern_or_spinner, options)
case pattern_or_spinner
when ::String
TTY::Spinner.new(
pattern_or_spinner,
@options.merge(options)
)
when ::TTY::Spinner
pattern_or_spinner
else
raise ArgumentError, "Expected a pattern or spinner, " \
"got: #{pattern_or_spinner.class}"
end
end
# Increase a row count
#
# @api public
def next_row
synchronize do
@rows += 1
end
end
# Get the top level spinner if it exists
#
# @return [TTY::Spinner] the top level spinner
#
# @api public
def top_spinner
raise "No top level spinner" if @top_spinner.nil?
@top_spinner
end
# Auto spin the top level spinner & all child spinners
# that have scheduled jobs
#
# @api public
def auto_spin
raise "No top level spinner" if @top_spinner.nil?
jobs = []
@spinners.each do |spinner|
if spinner.job?
spinner.auto_spin
jobs << Thread.new { spinner.execute_job }
end
end
jobs.each(&:join)
end
# Perform a single spin animation
#
# @api public
def spin
raise "No top level spinner" if @top_spinner.nil?
synchronize do
throttle { @top_spinner.spin }
end
end
# Pause all spinners
#
# @api public
def pause
@spinners.dup.each(&:pause)
end
# Resume all spinners
#
# @api public
def resume
@spinners.dup.each(&:resume)
end
# Find the number of characters to move into the line
# before printing the spinner
#
# @param [Integer] line_no
# the current spinner line number for which line inset is calculated
#
# @return [String]
# the inset
#
# @api public
def line_inset(line_no)
return '' if @top_spinner.nil?
if line_no == 1
@inset_opts[:top]
elsif line_no == @spinners_count
@inset_opts[:bottom]
else
@inset_opts[:middle]
end
end
# Check if all spinners are done
#
# @return [Boolean]
#
# @api public
def done?
synchronize do
(@spinners - [@top_spinner]).all?(&:done?)
end
end
# Check if all spinners succeeded
#
# @return [Boolean]
#
# @api public
def success?
synchronize do
(@spinners - [@top_spinner]).all?(&:success?)
end
end
# Check if any spinner errored
#
# @return [Boolean]
#
# @api public
def error?
synchronize do
(@spinners - [@top_spinner]).any?(&:error?)
end
end
# Stop all spinners
#
# @api public
def stop
@spinners.dup.each(&:stop)
end
# Stop all spinners with success status
#
# @api public
def success
@spinners.dup.each(&:success)
end
# Stop all spinners with error status
#
# @api public
def error
@spinners.dup.each(&:error)
end
# Listen on event
#
# @api public
def on(key, &callback)
unless @callbacks.key?(key)
raise ArgumentError, "The event #{key} does not exist. "\
' Use :spin, :success, :error, or :done instead'
end
@callbacks[key] << callback
self
end
private
# Check if this spinner should revolve to keep constant speed
# matching top spinner interval
#
# @api private
def throttle
sleep_time = 1.0 / @top_spinner.interval
if @last_spin_at && Time.now - @last_spin_at < sleep_time
return
end
yield if block_given?
@last_spin_at = Time.now
end
# Fire an event
#
# @api private
def emit(key, *args)
@callbacks[key].each do |block|
block.call(*args)
end
end
# Observe spinner for events to notify top spinner of current state
#
# @param [TTY::Spinner] spinner
# the spinner to listen to for events
#
# @api private
def observe(spinner)
spinner.on(:spin, &spin_handler)
.on(:success, &success_handler)
.on(:error, &error_handler)
.on(:done, &done_handler)
end
# Handle spin event
#
# @api private
def spin_handler
proc do
spin if @top_spinner
emit(:spin)
end
end
# Handle the success state
#
# @api private
def success_handler
proc do
if success?
@top_spinner.success if @top_spinner
emit(:success)
end
end
end
# Handle the error state
#
# @api private
def error_handler
proc do
if error?
@top_spinner.error if @top_spinner
@fired ||= emit(:error) # fire once
end
end
end
# Handle the done state
#
# @api private
def done_handler
proc do
if done?
@top_spinner.stop if @top_spinner && !error? && !success?
emit(:done)
end
end
end
end # MultiSpinner
end # Spinner
end # TTY
|