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

Fix cursor hiding in threaded run #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Change gemspec to include rake and rspec as dev dependencies
* Change all built-in formats to use consistent frames formatting

### Fixed
* Fix cursor hiding in threaded run

## [v0.9.3] - 2020-01-28

### Changed
Expand Down
24 changes: 13 additions & 11 deletions lib/tty/spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,25 @@ def auto_spin
start
sleep_time = 1.0 / @interval

spin
@thread = Thread.new do
sleep(sleep_time)
while @started_at
if Thread.current["pause"]
Thread.stop
Thread.current["pause"] = false
end
# rubocop:disable Style/RedundantBegin
begin
orgads marked this conversation as resolved.
Show resolved Hide resolved
# rubocop:enable Style/RedundantBegin
spin
sleep(sleep_time)
while @started_at
if Thread.current["pause"]
Thread.stop
Thread.current["pause"] = false
end
spin
sleep(sleep_time)
end
rescue StandardError
write(TTY::Cursor.show, false) if @hide_cursor
end
end
end
ensure
if @hide_cursor
write(TTY::Cursor.show, false)
end
orgads marked this conversation as resolved.
Show resolved Hide resolved
end

# Checked if current spinner is paused
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/auto_spin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
}

output.rewind
expect(output.read).to start_with("\e[?25l").and end_with("\e[?25h")
expect(output.read.empty?).to eq(true)
end
end
6 changes: 1 addition & 5 deletions spec/unit/pause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
spinner.auto_spin

output.rewind
expect(output.read).to eq([
"\e[1G[|]",
"\e[1G[?]",
"\e[1G[|]"
].join)
expect(output.read).to eq("\e[1G[?]")
end
end
16 changes: 16 additions & 0 deletions spec/unit/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@
spinner.run("done", &job)
}.to yield_with_args(spinner)
end

it "restores cursor when error is raised" do
spinner = TTY::Spinner.new(output: output, hide_cursor: true)

Thread.report_on_exception = false
begin
spinner.run do
raise "boom"
end
rescue RuntimeError
end
Thread.report_on_exception = true

output.rewind
expect(output.read).to start_with("\e[?25l").and end_with("\e[?25h")
end
end