Skip to content

Commit

Permalink
[bugfix] Updating examples
Browse files Browse the repository at this point in the history
# What
A few of the examples could do with a few improvements:
- `clear.rb` wasn't setting `clear: true`, so not exhibiting the feature
- updated the glyphs for `custom_style.rb`
- `multi/threaded.rb` was broken, due to [Ruby 3.0 changes](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/).  I've fixed it and added random success/errors to better demonstrate the example.
  • Loading branch information
GetOutOfMyBakery committed Aug 19, 2024
1 parent ea9be11 commit 7290bfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/clear.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../lib/tty-spinner"

spinner = TTY::Spinner.new("[:spinner] Task name", format: :bouncing_ball)
spinner = TTY::Spinner.new("[:spinner] Task name", clear: true, format: :bouncing_ball)
20.times do
spinner.spin
sleep(0.1)
Expand Down
6 changes: 3 additions & 3 deletions examples/multi/custom_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

opts = {
style: {
top: ". ",
middle: "|-> ",
bottom: "|__ "
top: "╭─➤ ",
middle: "├──➤ ",
bottom: "╰──➤ "
},
format: :bouncing_ball
}
Expand Down
27 changes: 19 additions & 8 deletions examples/multi/threaded.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
# frozen_string_literal: true

require_relative "../../lib/tty-spinner"
require "pastel"

@pastel = Pastel.new

def spinner_text
":spinner #{@pastel.bold("No")} :number \t #{@pastel.bold('Row')} :line"
end

def spinner_options
[
":spinner \e[1mNo\e[0m :number Row :line",
{
format: :dots,
error_mark: "✖",
success_mark: "\e[1m\e[32m✓\e[0m\e[0m"
]
hide_cursor: true,
error_mark: @pastel.red.bold("✖"),
success_mark: @pastel.green.bold("✓")
}
end

spinners = TTY::Spinner::Multi.new(*spinner_options)
spinners = TTY::Spinner::Multi.new(spinner_text, **spinner_options)
threads = []

20.times do |i|
threads << Thread.new do
spinner = spinners.register(*spinner_options)
spinner = spinners.register(spinner_text, **spinner_options)
sleep Random.rand(0.1..0.3)

10.times do
sleep Random.rand(0.1..0.3)
spinner.update(number: "(#{i})", line: spinner.row)
spinner.update(number: "#{i}", line: spinner.row)
spinner.spin
end

# Randomize conclusion
conclusion = [ :success, :error ].sample
spinner.send(conclusion)
end
end

Expand Down

0 comments on commit 7290bfd

Please sign in to comment.