Skip to content

Commit

Permalink
Add option for top level multi spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
austb committed Aug 1, 2017
1 parent fe60225 commit 8446001
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
26 changes: 26 additions & 0 deletions examples/multi_with_inset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# encoding: utf-8

require 'tty-spinner'

spinners = TTY::Spinner::Multi.new(message: "[:spinner] Top level spinner")

sp1 = spinners.register "[:spinner] one"
sp2 = spinners.register "[:spinner] two"
sp3 = spinners.register "[:spinner] three"

spinners.auto_spin
sp1.auto_spin
sp2.auto_spin
sp3.auto_spin

sleep(2)

sp1.success
sleep 1
sp2.success

sleep 1

sp3.error

spinners.error
8 changes: 6 additions & 2 deletions lib/tty/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def stop(stop_message = '')
end

write(data, true)
write("\n", false) unless @clear
write("\n", false) unless @clear || @multispinner
ensure
@state = :stopped
@done = true
Expand Down Expand Up @@ -372,7 +372,11 @@ def execute_on_line
def write(data, clear_first = false)
execute_on_line do
output.print(ECMA_CSI + '1' + ECMA_CHA) if clear_first
output.print(data)

# If there's a top level spinner, print with inset
characters_in = @multispinner.nil? ? "" : @multispinner.line_inset(self)

output.print(characters_in + data)
output.flush
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/tty/spinner/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ class Multi
def_delegators :@spinners, :each, :empty?, :length

def initialize(options = {})
message = options.delete(:message)
@options = options

@create_spinner_lock = Mutex.new

@spinners = []

unless message.nil?
@master_spinner = register(message)
end

@callbacks = {
success: [],
error: [],
Expand All @@ -47,6 +53,12 @@ def register(pattern, options = {})
spinner
end

def auto_spin
raise "No top level message given" if @master_spinner.nil?

@master_spinner.auto_spin
end

# Find relative offset position to which to move the current cursor
#
# The position is found among the registered spinners given the current
Expand All @@ -69,6 +81,16 @@ def count_line_offset(index)
end
end

# Find the number of characters to move into the line before printing the
# spinner
#
# @api public
def line_inset(spinner)
return " " if @master_spinner && spinner != @master_spinner

""
end

# Check if all spinners are done
#
# @return [Boolean]
Expand Down Expand Up @@ -108,6 +130,7 @@ def stop
#
# @api public
def success
@master_spinner.success if @master_spinner
@spinners.dup.each(&:success)
emit :success
end
Expand All @@ -116,6 +139,7 @@ def success
#
# @api public
def error
@master_spinner.error if @master_spinner
@spinners.dup.each(&:error)
emit :error
end
Expand Down
1 change: 1 addition & 0 deletions spec/unit/spin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
it "spins with newline when it has a MultiSpinner" do
multi_spinner = double("MultiSpinner")
allow(multi_spinner).to receive(:count_line_offset).and_return(1, 1, 2, 1)
allow(multi_spinner).to receive(:line_inset).and_return("")

spinner = TTY::Spinner.new(output: output, interval: 100)
spinner.add_multispinner(multi_spinner, 0)
Expand Down

0 comments on commit 8446001

Please sign in to comment.