Skip to content

Commit

Permalink
Add docs about hiding cursor and ensuring it is restored on interrupt…
Browse files Browse the repository at this point in the history
…s and other unpredictable errors (ref #40)
  • Loading branch information
piotrmurach committed Jul 12, 2020
1 parent ee01e48 commit bfc1667
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Expand Up @@ -338,7 +338,7 @@ There are number of configuration options that can be provided:
* [:output](#32-output) the output stream defaulting to `stderr`
* [:frequency](#33-frequency) used to throttle the output, by default `0`
* [:interval](#34-interval) used to measure the speed, by default `1 sec`
* `:hide_cursor` to hide display cursor defaulting to `false`
* [:hide_cursor](#35-hide_cursor) to hide display cursor defaulting to `false`
* `:clear` to clear the finished bar defaulting to `false`
* `:clear_head` to clear the head character when the progress is done, defaults to `false`

Expand Down Expand Up @@ -396,6 +396,25 @@ TTY::ProgressBar.new(":rate/minute", total: 100, interval: 60) # 1 minute
TTY::ProgressBar.new(":rate/hour", total: 100, interval: 3600) # 1 hour
```

### 3.5 :hide_cursor

By default the cursor is visible during progress bar rendering. If you wish to hide it, you can do so with the `:hide_cursor` option.

Please note that hiding cursor changes user's terminal and you need to ensure that the cursor is made visible after your code finishes. This means also handling premature interrupt signals and other unpredictable events.

One solution is to wrap your progress rendering inside the `begin` and `ensure` like so:

```ruby
progress = TTY::ProgressBar.new("[:bar]", hide_cursor: true)

begin
# logic to advance progress bar
ensure
progress.stop # or progress.finish
# both methods will ensure that cursor is made visible again
end
```

## 4. Formatting

Every **TTY::ProgressBar** instance requires a format string, which apart from regular characters accepts special tokens to display dynamic information. For instance, a format to measure download progress could be:
Expand Down

0 comments on commit bfc1667

Please sign in to comment.