X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fconsolecolors.h;h=8d154f7ee5eead60d71fcda19038b9c1a01b4fb4;hb=9982ec4e5b027ed24b1fda5e6fd3ab35b98de1a7;hp=f7ca1335ed244f16514d457d4fa035971fab2996;hpb=12c0e5d76395ae6348f5d411fef50aa777433f02;p=user%2Fhenk%2Fcode%2Finspircd.git
diff --git a/include/consolecolors.h b/include/consolecolors.h
index f7ca1335e..8d154f7ee 100644
--- a/include/consolecolors.h
+++ b/include/consolecolors.h
@@ -14,10 +14,31 @@
* along with this program. If not, see .
*/
-#ifndef CONSOLECOLORS_H
-#define CONSOLECOLORS_H
+
+#pragma once
#include
+#include
+
+#ifdef _WIN32
+# include
+# define isatty(x) _isatty((x))
+# define fileno(x) _fileno((x))
+#else
+# include
+#endif
+
+namespace
+{
+ inline bool CanUseColors()
+ {
+#ifdef INSPIRCD_DISABLE_COLORS
+ return false;
+#else
+ return isatty(fileno(stdout));
+#endif
+ }
+}
#ifdef _WIN32
@@ -29,72 +50,88 @@ extern HANDLE g_hStdout;
inline std::ostream& con_green(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ return s;
}
inline std::ostream& con_red(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ return s;
}
inline std::ostream& con_white(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|g_wBackgroundColor);
+ return s;
}
inline std::ostream& con_white_bright(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY|g_wBackgroundColor);
+ return s;
}
inline std::ostream& con_bright(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, FOREGROUND_INTENSITY|g_wBackgroundColor);
+ return s;
}
inline std::ostream& con_reset(std::ostream &s)
{
- SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
- return s;
+ if (CanUseColors())
+ SetConsoleTextAttribute(g_hStdout, g_wOriginalColors);
+ return s;
}
#else
inline std::ostream& con_green(std::ostream &s)
{
- return s << "\033[1;32m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[1;32m";
}
inline std::ostream& con_red(std::ostream &s)
{
- return s << "\033[1;31m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[1;31m";
}
inline std::ostream& con_white(std::ostream &s)
{
- return s << "\033[0m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[0m";
}
inline std::ostream& con_white_bright(std::ostream &s)
{
- return s << "\033[1m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[1m";
}
inline std::ostream& con_bright(std::ostream &s)
{
- return s << "\033[1m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[1m";
}
inline std::ostream& con_reset(std::ostream &s)
{
- return s << "\033[0m";
+ if (!CanUseColors())
+ return s;
+ return s << "\033[0m";
}
#endif
-
-#endif