ref: refs/heads/master
commit 1fb6ab830a1a3de59e24828bded6e544df236527
Author: Ján Kupec
Date: Fri Mar 6 15:54:09 2009 +0100
Use darker color for progress output.
---
src/output/OutNormal.cc | 54 +++++++++++++++++++++++++++++++++++++++++++---
src/utils/colors.cc | 17 ++++++++++++--
src/utils/colors.h | 23 +++++++++++++++++++-
3 files changed, 86 insertions(+), 8 deletions(-)
diff --git a/src/output/OutNormal.cc b/src/output/OutNormal.cc
index cf1c170..cae8bfa 100644
--- a/src/output/OutNormal.cc
+++ b/src/output/OutNormal.cc
@@ -104,7 +104,7 @@ void OutNormal::error(const zypp::Exception & e,
cerr << COLOR_RESET;
// hint
- if (!hint.empty())
+ if (!hint.empty() && this->verbosity() > Out::QUIET)
cerr << hint << endl;
}
@@ -153,6 +153,9 @@ void OutNormal::progressStart(const std::string & id,
if (progressFilter())
return;
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
if (!_isatty)
cout << label << " [";
@@ -160,6 +163,9 @@ void OutNormal::progressStart(const std::string & id,
display_tick(id, label);
else
display_progress(id, label, 0);
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
}
void OutNormal::progress(const std::string & id, const string & label, int value)
@@ -167,10 +173,16 @@ void OutNormal::progress(const std::string & id, const string & label, int value
if (progressFilter())
return;
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
if (value)
display_progress(id, label, value);
else
display_tick(id, label);
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
}
void OutNormal::progressEnd(const std::string & id, const string & label, bool error)
@@ -178,15 +190,22 @@ void OutNormal::progressEnd(const std::string & id, const string & label, bool e
if (progressFilter())
return;
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
if (_isatty)
{
cout << CLEARLN << label << " [";
if (error)
- print_color(_("error"), COLOR_RED);
+ print_color(_("error"), COLOR_RED, COLOR_WHITE);
else
cout << _("done");
}
cout << "]";
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
+
cout << endl << std::flush;
}
@@ -196,6 +215,9 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
if (verbosity() < NORMAL)
return;
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
if (isatty(STDOUT_FILENO))
cout << CLEARLN;
cout << _("Retrieving:") << " ";
@@ -207,6 +229,10 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
cout << " [" << _("starting") << "]"; //! \todo align to the right
else
cout << " [" ;
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
+
cout << std::flush;
}
@@ -216,12 +242,16 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
{
if (verbosity() < NORMAL)
return;
+
if (!isatty(STDOUT_FILENO))
{
cout << '.' << std::flush;
return;
}
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
cout << CLEARLN << _("Retrieving:") << " ";
if (verbosity() == DEBUG)
cout << uri; //! \todo shorten to fit the width of the terminal
@@ -238,6 +268,9 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
cout << "]";
}
+ if (_has_colors)
+ cerr << COLOR_RESET;
+
cout << std::flush;
}
@@ -246,7 +279,10 @@ void OutNormal::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error)
if (verbosity() < NORMAL)
return;
- if (isatty(STDOUT_FILENO))
+ if (_has_colors)
+ cerr << COLOR_WHITE;
+
+ if (_isatty)
{
cout << CLEARLN << _("Retrieving:") << " ";
if (verbosity() == DEBUG)
@@ -254,11 +290,21 @@ void OutNormal::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error)
else
cout << zypp::Pathname(uri.getPathName()).basename();
cout << " [";
+ if (error)
+ print_color(_("error"), COLOR_RED, COLOR_WHITE);
+ else
+ cout << _("done");
}
- cout << (error ? _("error") : _("done"));
+ else
+ cout << (error ? _("error") : _("done"));
+
if (rate >= 0)
cout << " (" << zypp::ByteCount(rate) << "/s)";
cout << "]";
+
+ if (_has_colors)
+ cerr << COLOR_RESET;
+
cout << endl << std::flush;
}
diff --git a/src/utils/colors.cc b/src/utils/colors.cc
index ad3a523..8f34601 100644
--- a/src/utils/colors.cc
+++ b/src/utils/colors.cc
@@ -12,9 +12,11 @@
#include "colors.h"
+using namespace std;
+
bool has_colors()
{
- if (isatty(STDOUT_FILENO))
+ if (::isatty(STDOUT_FILENO))
{
char *term = ::getenv("TERM");
if (term && ::strcmp(term, "dumb"))
@@ -23,7 +25,16 @@ bool has_colors()
return false;
}
-void print_color(const std::string & s, const char * ansi_color_seq)
+void print_color(const std::string & s,
+ const char * ansi_color_seq, const char * prev_color)
{
- std::cout << ansi_color_seq << s << COLOR_RESET;
+ if (prev_color)
+ cout << COLOR_RESET;
+
+ cout << ansi_color_seq << s;
+
+ if (prev_color)
+ cout << prev_color;
+ else
+ cout << COLOR_RESET;
}
diff --git a/src/utils/colors.h b/src/utils/colors.h
index 0f9423d..0e50d0e 100644
--- a/src/utils/colors.h
+++ b/src/utils/colors.h
@@ -21,7 +21,28 @@
#define COLOR_RESET "\033[m"
+/*
+enum zypper_colors
+{
+ ZYPPER_COLOR_MSG_NORMAL = 1,
+ ZYPPER_COLOR_MSG_HIGHLIGHTED = 2,
+ ZYPPER_COLOR_MSG_ERROR = 3,
+ ZYPPER_COLOR_MSG_WARNING = 4
+};
+*/
+
+/** Simple check whether stdout can handle colors. */
bool has_colors();
-void print_color(const std::string & s, const char * ansi_color_seq);
+
+/**
+ * Print string \a s in given color.
+ *
+ * \param s string to print
+ * \param ansi_color_seq color to print with
+ * \param prev_color color to restore after printing. If NULL,
+ * COLOR_RESET will be used
+ */
+void print_color(const std::string & s,
+ const char * ansi_color_seq, const char * prev_color);
#endif /* UTILS_COLORS_H_ */
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org