ctrlc-tiny
A tiny crate for checking if Ctrl-C was pressed.
No handlers to set. No threads. No AtomicBool.
Just call init_ctrlc() once, then check is_ctrlc_received() in your loop.
⨠Features
- Signal-safe
SIGINThandler - No threads, no allocations
- No runtime Rust dependencies
- Ideal for polling-based CLI tools
š Usage
Add to your Cargo.toml:
= "0.2"
Example:
Need to detect Ctrl-C more than once? See examples/multi_ctrlc.rs.
š Why not use ctrlc?
ctrlc is great when you want to run custom logic when Ctrl-C is pressed.
But if you just want to check whether Ctrl-C was pressed, it can feel more involved than necessary.
ctrlc-tiny keeps things simple: a single flag you can poll.
š Signal Safety
- Internally uses a
volatile sig_atomic_tflag ā safe in POSIX signal handlers. - No heap, no threads ā fully signal-safe by design.
- The flag can be reset via
reset_ctrlc_received(), but may race with the signal handler if SIGINT is received at the same time.
š ļø Platform Support
- ā Linux
- ā macOS
- ā Windows (no plans to add support)
š Note
Honestly, Arc, AtomicBool, and even the internals of the ctrlc crate don't pose any real-world performance issues.
This crate was created to scratch a personal itch ā to get rid of a subjective sense of overkill and a bit of boilerplate.
In that sense, Iām quite happy with how this crate turned out.
š¦ License
Licensed under either of:
- MIT
- Apache 2.0
See LICENSE-MIT or LICENSE-APACHE.