Skip to content

Commit

Permalink
feat: publish show_cursor (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-napoleone committed Mar 24, 2022
1 parent caf329d commit c27dc8a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ s.stop_with("🥬", "spinach'd", Color::Ignore);
s.stop_with(None, None, Color::Blue);
```

## FAQ

### How to avoid leaving terminal without prompt on interupt (ctrl^c)?

You can use a library like [`ctrlc`](https://crates.io/crates/ctrlc) to handle interupts.

The most basic way to handle it would be in conjuction with this lib QoL `show_cursor` function like this:

```rust
use spinach::{term, Spinach};

fn main() {
ctrlc::set_handler(|| {
term::show_cursor();
std::process::exit(0);
})
.expect("Error setting Ctrl-C handler");

let s = Spinach::new("spinnin'");
// ...
```

## Related

Inspired by:
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use crate::term::Color;

mod context;
mod spinner;
mod term;

pub mod term;

/// Spinach spinner.
///
Expand Down Expand Up @@ -327,6 +328,7 @@ impl Spinach {
Self { sender, handle }
}
}

enum SpinnerCommand {
Update {
text: Option<&'static str>,
Expand Down
15 changes: 14 additions & 1 deletion src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ pub(crate) fn hide_cursor() {
print!("\x1b[?25l")
}

pub(crate) fn show_cursor() {
/// Print show cursor ANSI escape code
///
/// Can be used when managing ctrl^c/SIGINT to show the cursor back
///
/// # Examples
///
/// ```
/// use spinach;
///
/// let s = spinach::Spinach::new("Cutting spinaches");
/// // somehow `s` is droped
/// spinach::term::show_cursor();
/// ```
pub fn show_cursor() {
print!("\x1b[?25h")
}

Expand Down

0 comments on commit c27dc8a

Please sign in to comment.