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
This commit is contained in:
psychon
2008-01-18 22:27:59 +00:00
parent 5370a54012
commit 74fb58cce5
3 changed files with 55 additions and 20 deletions

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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;