Skip to content

Commit

Permalink
Fix cursor hiding in threaded run
Browse files Browse the repository at this point in the history
auto_spin ensure was called right after starting to spin, so the cursor
was never actually hidden.

Fix by calling the initial spin in the thread, and wrap the thread with
ensure that shows the cursor at the end.
  • Loading branch information
orgads committed Sep 18, 2023
1 parent 596bf41 commit a5fd890
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
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
# 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
ensure
write(TTY::Cursor.show, false) if @hide_cursor
end
end
end
ensure
if @hide_cursor
write(TTY::Cursor.show, false)
end
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

0 comments on commit a5fd890

Please sign in to comment.