Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi logging WIP #52

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/tty/progressbar/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ def done?
end
end

# sort of working, but just keeps overwriting the top bar
def log(message)
sanitized_message = message.gsub(/\r|\n/, " ")
sanitized_message = "#{sanitized_message}#{NEWLINE}" if done?
top_bar.write(sanitized_message)
end

# Outputs as expecte, but the bars keep writing to the same line
def log(message)
@output.print("#{ECMA_CSI}0m#{TTY::Cursor.clear_line}")
@output.print(message)
@output.print(TTY::ProgressBar::NEWLINE)
@output.print TTY::Cursor.next_line

# Reposition the bars - but then the logs get overwritten?
#@bars.reverse.each_with_index { |b, i| b.instance_variable_set(:@row, @rows - i + 1) }
next_row
end

# Check if all bars are paused
#
# @return [Boolean]
Expand Down
34 changes: 34 additions & 0 deletions multi_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'lib/tty-progressbar'

puts "here's some stuff"
puts "go go go"

bars = TTY::ProgressBar::Multi.new("main [:bar] :percent")

BAR1_TIMES = 15
BAR2_TIMES = 5

bar1 = bars.register("one [:bar] :percent", total: BAR1_TIMES)
bar2 = bars.register("two [:bar] :percent", total: BAR2_TIMES)

bars.start # starts all registered bars timers

i1 = 0
i2 = 0

th1 = Thread.new {
BAR1_TIMES.times {
bars.log("Bar 1 (#{i1 += 1}): #{rand}")
sleep(0.1)
bar1.advance
}
}
th2 = Thread.new {
BAR2_TIMES.times {
bars.log("Bar 2 (#{i2 += 1}): #{rand}")
sleep(0.5)
bar2.advance
}
}

[th1, th2].each(&:join)