ref: refs/heads/master
commit 50e361e91fa3d7df2a322bce77be49c72794cb8e
Author: Ján Kupec
Date: Sun Feb 22 17:21:06 2009 +0100
Slight introduction of colors.
---
src/CMakeLists.txt | 2 +
src/Zypper.cc | 3 --
src/output/OutNormal.cc | 58 ++++++++++++++++++++++++++++++++-------
src/output/OutNormal.h | 19 +++++++++---
src/utils/colors.cc | 29 ++++++++++++++++++++
src/utils/colors.h | 27 ++++++++++++++++++
src/utils/prompt.cc | 68 ++++++++++++++++++++++++++++++++++++++++------
src/utils/prompt.h | 8 +++++
8 files changed, 186 insertions(+), 28 deletions(-)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b71cd06..9cf4d17 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -58,6 +58,7 @@ SET( zypper_out_SRCS
)
SET( zypper_utils_HEADERS
+ utils/colors.h
utils/getopt.h
utils/messages.h
utils/misc.h
@@ -67,6 +68,7 @@ SET( zypper_utils_HEADERS
)
SET( zypper_utils_SRCS
+ utils/colors.cc
utils/getopt.cc
utils/messages.cc
utils/misc.cc
diff --git a/src/Zypper.cc b/src/Zypper.cc
index 181340a..67703c4 100644
--- a/src/Zypper.cc
+++ b/src/Zypper.cc
@@ -8,9 +8,6 @@
// zypper - command line interface for libzypp, the package management library
// http://en.opensuse.org/Zypper
-#ifdef HAVE_CONFIG_H
-#include
-#endif
#include <iostream>
#include <fstream>
#include <sstream>
diff --git a/src/output/OutNormal.cc b/src/output/OutNormal.cc
index dc8a62b..cf1c170 100644
--- a/src/output/OutNormal.cc
+++ b/src/output/OutNormal.cc
@@ -1,3 +1,10 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
#include <iostream>
#include <sstream>
@@ -7,7 +14,8 @@
#include "zypp/ByteCount.h" // for download progress reporting
#include "zypp/base/String.h" // for toUpper()
-#include "../main.h"
+#include "main.h"
+#include "utils/colors.h"
#include "AliveCursor.h"
#include "OutNormal.h"
@@ -18,6 +26,11 @@ using std::endl;
using std::string;
using std::ostringstream;
+OutNormal::OutNormal(Verbosity verbosity)
+ : Out(TYPE_NORMAL, verbosity),
+ _has_colors(has_colors()), _isatty(isatty(STDOUT_FILENO))
+{}
+
OutNormal::~OutNormal()
{
@@ -44,19 +57,30 @@ void OutNormal::info(const std::string & msg, Verbosity verbosity, Type mask)
{
if (infoWarningFilter(verbosity, mask))
return;
- cout << msg << endl;
+
+ if (_has_colors && verbosity > Out::QUIET)
+ cout << COLOR_WHITE << msg << COLOR_RESET << endl;
+ else
+ cout << msg << endl;
}
void OutNormal::warning(const std::string & msg, Verbosity verbosity, Type mask)
{
if (infoWarningFilter(verbosity, mask))
return;
- info(_("Warning: ") + msg, verbosity, mask);
+
+ if (_has_colors)
+ cout << COLOR_YELLOW_BOLD << _("Warning: ") << COLOR_RESET << msg << endl;
+ else
+ cout << msg << endl;
}
void OutNormal::error(const std::string & problem_desc, const std::string & hint)
{
- cerr << problem_desc;
+ if (_has_colors)
+ cerr << COLOR_RED_BOLD << problem_desc << COLOR_RESET;
+ else
+ cerr << problem_desc;
if (!hint.empty() && this->verbosity() > Out::QUIET)
cerr << endl << hint;
cerr << endl;
@@ -68,10 +92,17 @@ void OutNormal::error(const zypp::Exception & e,
const string & problem_desc,
const string & hint)
{
+ if (_has_colors)
+ cerr << COLOR_RED_BOLD;
+
// problem
cerr << problem_desc << endl;
// cause
cerr << zyppExceptionReport(e) << endl;
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
+
// hint
if (!hint.empty())
cerr << hint << endl;
@@ -122,7 +153,7 @@ void OutNormal::progressStart(const std::string & id,
if (progressFilter())
return;
- if (!isatty(STDOUT_FILENO))
+ if (!_isatty)
cout << label << " [";
if (is_tick)
@@ -147,10 +178,15 @@ void OutNormal::progressEnd(const std::string & id, const string & label, bool e
if (progressFilter())
return;
- if (isatty(STDOUT_FILENO))
- cout << CLEARLN << label << " [" << (error ? _("error") : _("done")) << "]";
- else
- cout << "]";
+ if (_isatty)
+ {
+ cout << CLEARLN << label << " [";
+ if (error)
+ print_color(_("error"), COLOR_RED);
+ else
+ cout << _("done");
+ }
+ cout << "]";
cout << endl << std::flush;
}
@@ -237,7 +273,7 @@ void OutNormal::prompt(PromptId id,
cout << startdesc << endl;
cout << prompt;
if (!poptions.empty())
- cout << " [" << poptions.optionString() << "]";
+ cout << " [" << (_has_colors ? poptions.optionStringColored() : poptions.optionString()) << "]";
cout << ": " << std::flush;
}
@@ -264,5 +300,5 @@ void OutNormal::promptHelp(const PromptOptions & poptions)
}
}
- cout << endl << "[" << poptions.optionString() << "]: " << std::flush;
+ cout << endl << "[" << (_has_colors ? poptions.optionStringColored() : poptions.optionString()) << "]: " << std::flush;
}
diff --git a/src/output/OutNormal.h b/src/output/OutNormal.h
index 1285295..efb3e95 100644
--- a/src/output/OutNormal.h
+++ b/src/output/OutNormal.h
@@ -1,3 +1,10 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
#ifndef OUTNORMAL_H_
#define OUTNORMAL_H_
@@ -6,7 +13,7 @@
class OutNormal : public Out
{
public:
- OutNormal(Verbosity verbosity = NORMAL) : Out(TYPE_NORMAL, verbosity) {}
+ OutNormal(Verbosity verbosity = NORMAL);
virtual ~OutNormal();
public:
@@ -20,13 +27,13 @@ public:
/**
* Prints \a msg prepended with <tt>"Warning: "</tt> to the standard output
* and appends a newline.
- *
+ *
* \see Out::warning
*/
virtual void warning(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL);
-
+
/**
- *
+ *
*/
virtual void error(const std::string & problem_desc, const std::string & hint = "");
virtual void error(const zypp::Exception & e,
@@ -52,7 +59,7 @@ public:
virtual void dwnldProgressEnd(const zypp::Url & uri,
long rate = -1,
bool error = false);
-
+
virtual void prompt(PromptId id,
const std::string & prompt,
const PromptOptions & poptions,
@@ -65,6 +72,8 @@ protected:
private:
bool infoWarningFilter(Verbosity verbosity, Type mask);
+ bool _has_colors;
+ bool _isatty;
};
#endif /*OUTNORMAL_H_*/
diff --git a/src/utils/colors.cc b/src/utils/colors.cc
new file mode 100644
index 0000000..ad3a523
--- /dev/null
+++ b/src/utils/colors.cc
@@ -0,0 +1,29 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
+#include <iostream>
+#include
+#include
+#include
+
+#include "colors.h"
+
+bool has_colors()
+{
+ if (isatty(STDOUT_FILENO))
+ {
+ char *term = ::getenv("TERM");
+ if (term && ::strcmp(term, "dumb"))
+ return true;
+ }
+ return false;
+}
+
+void print_color(const std::string & s, const char * ansi_color_seq)
+{
+ std::cout << ansi_color_seq << s << COLOR_RESET;
+}
diff --git a/src/utils/colors.h b/src/utils/colors.h
new file mode 100644
index 0000000..0f9423d
--- /dev/null
+++ b/src/utils/colors.h
@@ -0,0 +1,27 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
+#ifndef UTILS_COLORS_H_
+#define UTILS_COLORS_H_
+
+#include <iosfwd>
+
+#define COLOR_GREEN "\033[32m"
+#define COLOR_GREEN_BOLD "\033[1;32m"
+#define COLOR_RED "\033[31m"
+#define COLOR_RED_BOLD "\033[1;31m"
+#define COLOR_WHITE "\033[37m" // grey
+#define COLOR_WHITE_BOLD "\033[1;37m"
+#define COLOR_YELLOW "\033[33m" // brown
+#define COLOR_YELLOW_BOLD "\033[1;33m"
+
+#define COLOR_RESET "\033[m"
+
+bool has_colors();
+void print_color(const std::string & s, const char * ansi_color_seq);
+
+#endif /* UTILS_COLORS_H_ */
diff --git a/src/utils/prompt.cc b/src/utils/prompt.cc
index 87f7889..95b8fd9 100644
--- a/src/utils/prompt.cc
+++ b/src/utils/prompt.cc
@@ -1,3 +1,10 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
#include
#include <iostream>
#include <sstream>
@@ -14,6 +21,8 @@
#include "zypp/base/String.h"
#include "Zypper.h"
+#include "utils/colors.h"
+
#include "prompt.h"
using namespace std;
@@ -45,25 +54,67 @@ void PromptOptions::setOptions(const std::string & option_str, unsigned int defa
_default = default_opt;
}
+
+
const string PromptOptions::optionString() const
{
- string option_str;
+ ostringstream option_str;
StrVector::const_iterator it;
if ((it = options().begin()) != options().end())
{
- option_str += (defaultOpt() == 0 ? zypp::str::toUpper(*it) : *it);
+ if (defaultOpt() == 0)
+ option_str << "_" << *it << "_";
+ else
+ option_str << *it;
++it;
}
for (unsigned int i = 1; it != options().end() && i < _shown_count; ++it, ++i)
if (isEnabled(i))
- option_str += "/" + (defaultOpt() == i ? zypp::str::toUpper(*it) : *it);
+ {
+ option_str << "/";
+ if (defaultOpt() == i)
+ option_str << "_" << *it << "_";
+ else
+ option_str << *it;
+ }
if (!_opt_help.empty())
- option_str += "/?";
+ option_str << "/?";
- return option_str;
+ return option_str.str();
}
+const string PromptOptions::optionStringColored() const
+{
+ ostringstream option_str;
+ StrVector::const_iterator it;
+ if ((it = options().begin()) != options().end())
+ {
+ if (defaultOpt() == 0)
+ option_str << COLOR_YELLOW << *it;
+ else
+ option_str << COLOR_WHITE << *it;
+ ++it;
+ }
+ for (unsigned int i = 1; it != options().end() && i < _shown_count; ++it, ++i)
+ if (isEnabled(i))
+ {
+ option_str << COLOR_WHITE << "/";
+ if (defaultOpt() == i)
+ option_str << COLOR_YELLOW << *it;
+ else
+ option_str << *it;
+ }
+
+ if (!_opt_help.empty())
+ option_str << COLOR_WHITE << "/?";
+
+ option_str << COLOR_RESET;
+
+ return option_str.str();
+}
+
+
void PromptOptions::setOptionHelp(unsigned int opt, const std::string & help_str)
{
if (help_str.empty())
@@ -302,10 +353,9 @@ unsigned int get_prompt_reply(Zypper & zypper,
if (poptions.isYesNoPrompt())
{
s << " " << format(
- // TranslatorExplanation don't translate the 'y' and 'n', they can always be used as answers.
- // The second and the third %s is the translated 'yes' and 'no' string (lowercase).
- _("Enter 'y' for '%s' or 'n' for '%s' if nothing else works for you."))
- % _("yes") % _("no");
+ // translators: the %s are: 'y', 'n', 'yes' (translated), and 'no' (translated).
+ _("Enter '%s' for '%s' or '%s' for '%s' if nothing else works for you."))
+ % "y" % "n" % _("yes") % _("no");
}
zypper.out().prompt(pid, s.str(), poptions);
diff --git a/src/utils/prompt.h b/src/utils/prompt.h
index 86828b2..6ef5bdc 100644
--- a/src/utils/prompt.h
+++ b/src/utils/prompt.h
@@ -1,3 +1,10 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
#ifndef ZYPPERPROMPT_H_
#define ZYPPERPROMPT_H_
@@ -46,6 +53,7 @@ public:
void setOptions(const std::string & option_str, unsigned int default_opt);
unsigned int defaultOpt() const { return _default; }
const std::string optionString() const;
+ const std::string optionStringColored() const;
bool empty() const { return _options.empty(); }
bool isYesNoPrompt() const;
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org