From 74fb58cce5c8a85353737e30f342103dc0a7dfc1 Mon Sep 17 00:00:00 2001 From: psychon Date: Fri, 18 Jan 2008 22:27:59 +0000 Subject: [PATCH] Add --no-color switch and only use escape sequences if isatty() says so git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@926 726aef4b-f618-498e-8847-2d620e286838 --- Utils.cpp | 62 +++++++++++++++++++++++++++++++++++++++---------------- Utils.h | 3 +++ main.cpp | 10 +++++++-- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index 79f3253f..b96c8135 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -370,45 +370,71 @@ bool CUtils::GetInput(const CString& sPrompt, CString& sRet, const CString& sDef } void CUtils::PrintError(const CString& sMessage) { - fprintf(stdout, "\033[1m\033[34m[\033[31m ** \033[34m]\033[39m\033[22m %s\n", sMessage.c_str()); + if (stdoutIsTTY) + fprintf(stdout, "\033[1m\033[34m[\033[31m ** \033[34m]\033[39m\033[22m %s\n", sMessage.c_str()); + else + fprintf(stdout, "[ ** ] %s\n", sMessage.c_str()); fflush(stdout); } void CUtils::PrintPrompt(const CString& sMessage) { - fprintf(stdout, "\033[1m\033[34m[\033[33m ?? \033[34m]\033[39m\033[22m %s: ", sMessage.c_str()); + if (stdoutIsTTY) + fprintf(stdout, "\033[1m\033[34m[\033[33m ?? \033[34m]\033[39m\033[22m %s: ", sMessage.c_str()); + else + fprintf(stdout, "[ ?? ] %s: ", sMessage.c_str()); fflush(stdout); } void CUtils::PrintMessage(const CString& sMessage, bool bStrong) { - fprintf(stdout, "\033[1m\033[34m[\033[33m ** \033[34m]\033[39m\033[22m %s%s%s\n", - ((bStrong) ? "\033[1m" : ""), - sMessage.c_str(), - ((bStrong) ? "\033[22m" : "") - ); + if (stdoutIsTTY) { + if (bStrong) + fprintf(stdout, "\033[1m\033[34m[\033[33m ** \033[34m]\033[39m\033[22m \033[1m%s\033[22m\n", + sMessage.c_str()); + else + fprintf(stdout, "\033[1m\033[34m[\033[33m ** \033[34m]\033[39m\033[22m %s\n", + sMessage.c_str()); + } else + fprintf(stdout, "%s\n", sMessage.c_str()); fflush(stdout); } void CUtils::PrintAction(const CString& sMessage) { - fprintf(stdout, "\033[1m\033[34m[\033[32m \033[34m]\033[39m\033[22m %s... ", sMessage.c_str()); + if (stdoutIsTTY) + fprintf(stdout, "\033[1m\033[34m[\033[32m \033[34m]\033[39m\033[22m %s... ", sMessage.c_str()); + else + fprintf(stdout, "%s... ", sMessage.c_str()); fflush(stdout); } void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) { - if (!sMessage.empty()) { - if (bSuccess) { - fprintf(stdout, "%s", sMessage.c_str()); - } else { - fprintf(stdout, "\033[1m\033[34m[\033[31m %s \033[34m]\033[39m\033[22m", sMessage.c_str()); + if (stdoutIsTTY) { + if (!sMessage.empty()) { + if (bSuccess) { + fprintf(stdout, "%s", sMessage.c_str()); + } else { + fprintf(stdout, "\033[1m\033[34m[\033[31m %s \033[34m]" + "\033[39m\033[22m", sMessage.c_str()); + } } - } - fprintf(stdout, "\r"); + fprintf(stdout, "\r"); - if (bSuccess) { - fprintf(stdout, "\033[1m\033[34m[\033[32m ok \033[34m]\033[39m\033[22m\n"); + if (bSuccess) { + fprintf(stdout, "\033[1m\033[34m[\033[32m ok \033[34m]\033[39m\033[22m\n"); + } else { + fprintf(stdout, "\033[1m\033[34m[\033[31m !! \033[34m]\033[39m\033[22m\n"); + } } else { - fprintf(stdout, "\033[1m\033[34m[\033[31m !! \033[34m]\033[39m\033[22m\n"); + if (bSuccess) { + fprintf(stdout, "%s\n", sMessage.c_str()); + } else { + if (!sMessage.empty()) { + fprintf(stdout, "[ %s ]", sMessage.c_str()); + } + + fprintf(stdout, "\n[ !! ]\n"); + } } fflush(stdout); diff --git a/Utils.h b/Utils.h index a94513a5..4bdda588 100644 --- a/Utils.h +++ b/Utils.h @@ -37,6 +37,8 @@ public: static unsigned long GetLongIP(const CString& sIP); static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir); static int MakeDir(const CString& sPath, mode_t iMode = 0700); + static void SetStdoutIsTTY(bool b) { stdoutIsTTY = b; } + static void PrintError(const CString& sMessage); static void PrintMessage(const CString& sMessage, bool bStrong = false); static void PrintPrompt(const CString& sMessage); @@ -63,6 +65,7 @@ public: private: protected: + static bool stdoutIsTTY; }; class CLockFile { diff --git a/main.cpp b/main.cpp index c5d532d6..2add29a5 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ static struct option g_LongOpts[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, + { "no-color", no_argument, 0, 'n' }, { "makeconf", no_argument, 0, 'c' }, { "makepass", no_argument, 0, 's' }, #ifdef HAVE_LIBSSL @@ -27,6 +28,7 @@ void GenerateHelp(const char *appname) { CUtils::PrintMessage("Options are:"); CUtils::PrintMessage("\t-h, --help List available command line options (this page)"); CUtils::PrintMessage("\t-v, --version Output version information and exit"); + CUtils::PrintMessage("\t-n, --no-color Don't use escape sequences in the output"); CUtils::PrintMessage("\t-c, --makeconf Interactively create a new config"); CUtils::PrintMessage("\t-s, --makepass Generates a password for use in config"); #ifdef HAVE_LIBSSL @@ -57,6 +59,7 @@ int main(int argc, char** argv) { CString sDataDir = ""; srand(time(NULL)); + CUtils::SetStdoutIsTTY(isatty(1)); #ifdef HAVE_LIBSSL InitSSL(); @@ -69,10 +72,10 @@ int main(int argc, char** argv) { bool bMakePem = false; bool bEncPem = false; - while ((iArg = getopt_long(argc, argv, "hvcsped:", g_LongOpts, &iOptIndex)) != -1) { + while ((iArg = getopt_long(argc, argv, "hvncsped:", g_LongOpts, &iOptIndex)) != -1) { #else - while ((iArg = getopt_long(argc, argv, "hvcsd:", g_LongOpts, &iOptIndex)) != -1) { + while ((iArg = getopt_long(argc, argv, "hvncsd:", g_LongOpts, &iOptIndex)) != -1) { #endif /* HAVE_LIBSSL */ switch (iArg) { case 'h': @@ -81,6 +84,9 @@ int main(int argc, char** argv) { case 'v': cout << CZNC::GetTag() << endl; return 0; + case 'n': + CUtils::SetStdoutIsTTY(false); + break; case 'c': bMakeConf = true; break;