Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Detegr committed Jun 10, 2021
1 parent 7de9bd4 commit 2c669a4
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,41 @@ A simple easy to use wrapper around Ctrl-C signal.

[Documentation](http://detegr.github.io/doc/ctrlc/)

## Example usage
# Example usage
## Counter example
```rust
use std::sync::mpsc::channel;
use ctrlc;
extern crate ctrlc;
use ctrlc::{Counter, SignalType};
use std::thread;
use std::time;

fn main() {
let (tx, rx) = channel();

ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
.expect("Error setting Ctrl-C handler");

let counter = Counter::new(SignalType::Ctrlc).unwrap();
println!("Waiting for Ctrl-C...");
rx.recv().expect("Could not receive from channel.");
println!("Got it! Exiting...");
while counter.get() == 0 {
thread::sleep(time::Duration::from_millis(10));
}
println!("Got it! Exiting...");
}
```
## Channel example

#### Try the example yourself
`cargo build --examples && target/debug/examples/readme_example`
```rust
extern crate ctrlc;
use ctrlc::{Channel, SignalType};

## Handling SIGTERM
Add CtrlC to Cargo.toml using `termination` feature and CtrlC will handle both SIGINT and SIGTERM.
```
[dependencies]
ctrlc = { version = "3.0", features = ["termination"] }
fn main() {
let channel = Channel::new(SignalType::Ctrlc).unwrap();
println!("Waiting for Ctrl-C...");
channel.recv().unwrap();
println!("Got it! Exiting...");
}
```

#### Try the examples yourself
`cargo run --example counter_example`
`cargo run --example channel_example`

## License

Licensed under either of
Expand Down

0 comments on commit 2c669a4

Please sign in to comment.