Commit 1f6854ef4a627df66a0a8557492f52ba7f212007
Committed by
GitHub
1 parent
c5eadcf9
Adding note on sighandler
Showing
1 changed file
with
22 additions
and
0 deletions
README.md
| ... | ... | @@ -280,6 +280,28 @@ try { |
| 280 | 280 | |
| 281 | 281 | This will print help in blue, errors in red, and will reset before returning the terminal to the user. |
| 282 | 282 | |
| 283 | +If you are on a Unix-like system, and you'd like to handle control-c and color, you can add: | |
| 284 | + | |
| 285 | +```cpp | |
| 286 | + #include <csignal> | |
| 287 | + void signal_handler(int s) { | |
| 288 | + std::cout << std::endl << rang::style::reset << rang::fg::red << rang::fg::bold; | |
| 289 | + std::cout << "Control-C detected, exiting..." << rang::style::reset << std::endl; | |
| 290 | + std::exit(1); // will call the correct exit func, no unwinding of the stack though | |
| 291 | + } | |
| 292 | +``` | |
| 293 | + | |
| 294 | +And, in your main function: | |
| 295 | + | |
| 296 | +``` | |
| 297 | + // Nice Control-C | |
| 298 | + struct sigaction sigIntHandler; | |
| 299 | + sigIntHandler.sa_handler = signal_handler; | |
| 300 | + sigemptyset(&sigIntHandler.sa_mask); | |
| 301 | + sigIntHandler.sa_flags = 0; | |
| 302 | + sigaction(SIGINT, &sigIntHandler, nullptr); | |
| 303 | +``` | |
| 304 | + | |
| 283 | 305 | ## Contributing |
| 284 | 306 | |
| 285 | 307 | To contribute, open an [issue][Github Issues] or [pull request][Github Pull Requests] on GitHub, or ask a question on [gitter]. | ... | ... |