Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing error color. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion include/ns_getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <cctype>
#include <cstring>

#ifdef WIN32
#include <windows.h>
#endif // WIN32

namespace opt {

/* List of argument types. */
Expand Down Expand Up @@ -97,6 +101,7 @@ enum flag : unsigned int {
, arguments_are_optional = 4
, arg0_is_normal_argument = 8
, dont_print_help = 16
, no_color = 32
};

inline flag operator|(flag lhs, flag rhs);
Expand Down Expand Up @@ -675,7 +680,33 @@ inline bool compare_no_case(const std::string& lhs , const std::string& rhs
inline void maybe_print_msg(const options& option, const std::string& msg) {
if (has_flag(option.flags, flag::no_user_error_messages))
return;
std::cout << msg << std::endl << std::endl;

if (has_flag(option.flags, flag::no_color)) {
std::cout << "Parsing error : " << msg << std::endl << std::endl;
return;
}

/* Print colors. */
#ifdef WIN32
/// @todo Find how to set bold text.
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;

/* Save current attributes. */
GetConsoleScreenBufferInfo(console_handle, &consoleInfo);

WORD saved_attributes;
saved_attributes = consoleInfo.wAttributes;

SetConsoleTextAttribute(console_handle, FOREGROUND_RED);
std::cout << "Parsing error : " << msg << std::endl << std::endl;

/* Restore original attributes. */
SetConsoleTextAttribute(console_handle, saved_attributes);
#else // Anything other than Windows.
std::cout << "\033[1;31mParsing error : " << msg << "\033[0m" <<
std::endl << std::endl;
#endif // WIN32
}

inline bool has_flag(const flag flags, flag flag_to_check) {
Expand Down