From 1f6854ef4a627df66a0a8557492f52ba7f212007 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 3 Jul 2017 17:22:29 -0400 Subject: [PATCH] Adding note on sighandler --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index e0f8bba0..0e0e8bc9 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,28 @@ try { This will print help in blue, errors in red, and will reset before returning the terminal to the user. +If you are on a Unix-like system, and you'd like to handle control-c and color, you can add: + +```cpp + #include + void signal_handler(int s) { + std::cout << std::endl << rang::style::reset << rang::fg::red << rang::fg::bold; + std::cout << "Control-C detected, exiting..." << rang::style::reset << std::endl; + std::exit(1); // will call the correct exit func, no unwinding of the stack though + } +``` + +And, in your main function: + +``` + // Nice Control-C + struct sigaction sigIntHandler; + sigIntHandler.sa_handler = signal_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + sigaction(SIGINT, &sigIntHandler, nullptr); +``` + ## Contributing To contribute, open an [issue][Github Issues] or [pull request][Github Pull Requests] on GitHub, or ask a question on [gitter].