ref: refs/heads/master
commit 8fd8c2eae9f32be5953bd193e2f8997f8f0ff55d
Author: Ján Kupec
Date: Thu Mar 12 12:58:27 2009 +0100
Config struct added.
- will replace GlobalOptions
- will read zypper.conf
- turned off colors by default & until zypper.conf is available
(bnc #483349)
---
src/CMakeLists.txt | 2 ++
src/Config.cc | 27 +++++++++++++++++++++++++++
src/Config.h | 36 ++++++++++++++++++++++++++++++++++++
src/Zypper.cc | 16 ++++++++++++++--
src/Zypper.h | 6 ++++++
src/output/OutNormal.cc | 40 ++++++++++++++++++++--------------------
src/output/OutNormal.h | 5 ++++-
zypper.conf | 42 ++++++++++++++++++++++++++++++++++++++++++
8 files changed, 151 insertions(+), 23 deletions(-)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9cf4d17..599273f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -11,6 +11,7 @@ SET (zypper_HEADERS
Zypper.h
main.h
Command.h
+ Config.h
repos.h
misc.h
search.h
@@ -30,6 +31,7 @@ SET( zypper_SRCS
Zypper.cc
main.cc
Command.cc
+ Config.cc
repos.cc
misc.cc
search.cc
diff --git a/src/Config.cc b/src/Config.cc
new file mode 100644
index 0000000..eb725b5
--- /dev/null
+++ b/src/Config.cc
@@ -0,0 +1,27 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
+#include "utils/colors.h"
+
+#include "Config.h"
+
+#include <iostream>
+
+Config::Config()
+ : do_colors(false)
+ , color_useColors("never")
+ , color_background(false) // dark background
+{}
+
+void Config::read()
+{
+ // color_useColors = auges.getOption("colors/useColors");
+ // ...
+ do_colors =
+ color_useColors == "autodetect" && has_colors()
+ || color_useColors == "always";
+}
diff --git a/src/Config.h b/src/Config.h
new file mode 100644
index 0000000..d5b197e
--- /dev/null
+++ b/src/Config.h
@@ -0,0 +1,36 @@
+/*---------------------------------------------------------------------------*\
+ ____ _ _ __ _ __ ___ _ _
+ |_ / || | '_ \ '_ \/ -_) '_|
+ /__|\_, | .__/ .__/\___|_|
+ |__/|_| |_|
+\*---------------------------------------------------------------------------*/
+
+#ifndef ZYPPER_CONFIG_H_
+#define ZYPPER_CONFIG_H_
+
+#include <string>
+
+struct Config
+{
+ /** Initializes the config options to defaults. */
+ Config();
+
+ /** Reads zypper.conf and stores the result */
+ void read();
+
+
+
+ /**
+ * Whether to print colors to stdout. This is evaluated according to
+ * color_useColors and has_colors()
+ */
+ bool do_colors;
+
+ /** zypper.conf: color.useColors option */
+ std::string color_useColors;
+
+ /** dark (false) or light (true) */
+ bool color_background;
+};
+
+#endif /* ZYPPER_CONFIG_H_ */
diff --git a/src/Zypper.cc b/src/Zypper.cc
index 91b9c07..730bfcb 100644
--- a/src/Zypper.cc
+++ b/src/Zypper.cc
@@ -102,7 +102,10 @@ int Zypper::main(int argc, char ** argv)
_argv = argv;
// parse global options and the command
- try { processGlobalOptions(); }
+ try {
+ readConfig();
+ processGlobalOptions();
+ }
catch (const ExitRequestException & e)
{
MIL << "Caught exit request:" << endl << e.msg() << endl;
@@ -145,6 +148,11 @@ Out & Zypper::out()
ZYPP_THROW(ExitRequestException("no output writer"));
}
+void Zypper::readConfig()
+{
+ _config.read();
+}
+
void print_main_help(Zypper & zypper)
{
static string help_global_options = _(" Global Options:\n"
@@ -391,7 +399,11 @@ void Zypper::processGlobalOptions()
_gopts.no_abbrev = true;
}
else
- _out_ptr = new OutNormal(verbosity);
+ {
+ OutNormal * p = new OutNormal(verbosity);
+ p->setUseColors(_config.do_colors);
+ _out_ptr = p;
+ }
out().info(boost::str(format(_("Verbosity: %d")) % _gopts.verbosity), Out::HIGH);
DBG << "Verbosity " << verbosity << endl;
diff --git a/src/Zypper.h b/src/Zypper.h
index a1fd7b2..d5189da 100644
--- a/src/Zypper.h
+++ b/src/Zypper.h
@@ -21,6 +21,7 @@
#include "zypp/SrcPackage.h"
#include "zypp/TmpPath.h"
+#include "Config.h"
#include "Command.h"
#include "utils/getopt.h"
#include "output/Out.h"
@@ -31,6 +32,8 @@
/**
* Structure for holding global options.
+ *
+ * \deprecated To be replaced by Config
*/
struct GlobalOptions
{
@@ -159,6 +162,7 @@ public:
// setters & getters
Out & out();
+ const Config & config() const { return _config; }
const GlobalOptions & globalOpts() const { return _gopts; }
const parsed_opts & cOpts() const { return _copts; }
const ZypperCommand & command() const { return _command; }
@@ -184,6 +188,7 @@ public:
private:
Zypper();
+ void readConfig();
void processGlobalOptions();
void processCommandOptions();
void commandShell();
@@ -204,6 +209,7 @@ private:
char ** _argv;
Out * _out_ptr;
+ Config _config;
GlobalOptions _gopts;
parsed_opts _copts;
ZypperCommand _command;
diff --git a/src/output/OutNormal.cc b/src/output/OutNormal.cc
index ebad7de..4414398 100644
--- a/src/output/OutNormal.cc
+++ b/src/output/OutNormal.cc
@@ -28,7 +28,7 @@ using std::ostringstream;
OutNormal::OutNormal(Verbosity verbosity)
: Out(TYPE_NORMAL, verbosity),
- _has_colors(has_colors()), _isatty(isatty(STDOUT_FILENO))
+ _use_colors(false), _isatty(isatty(STDOUT_FILENO))
{}
OutNormal::~OutNormal()
@@ -58,7 +58,7 @@ void OutNormal::info(const std::string & msg, Verbosity verbosity, Type mask)
if (infoWarningFilter(verbosity, mask))
return;
- if (_has_colors && verbosity > Out::QUIET)
+ if (_use_colors && verbosity > Out::QUIET)
cout << COLOR_WHITE << msg << COLOR_RESET << endl;
else
cout << msg << endl;
@@ -69,7 +69,7 @@ void OutNormal::warning(const std::string & msg, Verbosity verbosity, Type mask)
if (infoWarningFilter(verbosity, mask))
return;
- if (_has_colors)
+ if (_use_colors)
cout << COLOR_YELLOW_BOLD << _("Warning: ") << COLOR_RESET << msg << endl;
else
cout << msg << endl;
@@ -77,7 +77,7 @@ void OutNormal::warning(const std::string & msg, Verbosity verbosity, Type mask)
void OutNormal::error(const std::string & problem_desc, const std::string & hint)
{
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RED_BOLD << problem_desc << COLOR_RESET;
else
cerr << problem_desc;
@@ -92,7 +92,7 @@ void OutNormal::error(const zypp::Exception & e,
const string & problem_desc,
const string & hint)
{
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RED_BOLD;
// problem
@@ -100,7 +100,7 @@ void OutNormal::error(const zypp::Exception & e,
// cause
cerr << zyppExceptionReport(e) << endl;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
// hint
@@ -153,7 +153,7 @@ void OutNormal::progressStart(const std::string & id,
if (progressFilter())
return;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
if (!_isatty)
@@ -164,7 +164,7 @@ void OutNormal::progressStart(const std::string & id,
else
display_progress(id, label, 0);
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
}
@@ -173,7 +173,7 @@ void OutNormal::progress(const std::string & id, const string & label, int value
if (progressFilter())
return;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
if (value)
@@ -181,7 +181,7 @@ void OutNormal::progress(const std::string & id, const string & label, int value
else
display_tick(id, label);
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
}
@@ -190,7 +190,7 @@ void OutNormal::progressEnd(const std::string & id, const string & label, bool e
if (progressFilter())
return;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
if (_isatty)
@@ -203,7 +203,7 @@ void OutNormal::progressEnd(const std::string & id, const string & label, bool e
}
cout << "]";
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
cout << endl << std::flush;
@@ -215,7 +215,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
if (verbosity() < NORMAL)
return;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
if (isatty(STDOUT_FILENO))
@@ -230,7 +230,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
else
cout << " [" ;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
cout << std::flush;
@@ -249,7 +249,7 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
return;
}
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
cout << CLEARLN << _("Retrieving:") << " ";
@@ -268,7 +268,7 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
cout << "]";
}
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
cout << std::flush;
@@ -279,7 +279,7 @@ void OutNormal::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error)
if (verbosity() < NORMAL)
return;
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_WHITE;
if (_isatty)
@@ -302,7 +302,7 @@ void OutNormal::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error)
cout << " (" << zypp::ByteCount(rate) << "/s)";
cout << "]";
- if (_has_colors)
+ if (_use_colors)
cerr << COLOR_RESET;
cout << endl << std::flush;
@@ -322,7 +322,7 @@ void OutNormal::prompt(PromptId id,
cout << startdesc << endl;
cout << prompt;
if (!poptions.empty())
- cout << " [" << (_has_colors ? poptions.optionStringColored() : poptions.optionString()) << "]";
+ cout << " [" << (_use_colors ? poptions.optionStringColored() : poptions.optionString()) << "]";
cout << ": " << std::flush;
}
@@ -349,5 +349,5 @@ void OutNormal::promptHelp(const PromptOptions & poptions)
}
}
- cout << endl << "[" << (_has_colors ? poptions.optionStringColored() : poptions.optionString()) << "]: " << std::flush;
+ cout << endl << "[" << (_use_colors ? poptions.optionStringColored() : poptions.optionString()) << "]: " << std::flush;
}
diff --git a/src/output/OutNormal.h b/src/output/OutNormal.h
index efb3e95..f7186ff 100644
--- a/src/output/OutNormal.h
+++ b/src/output/OutNormal.h
@@ -67,12 +67,15 @@ public:
virtual void promptHelp(const PromptOptions & poptions);
+ void setUseColors(bool value)
+ { _use_colors = value; }
+
protected:
virtual bool mine(Type type);
private:
bool infoWarningFilter(Verbosity verbosity, Type mask);
- bool _has_colors;
+ bool _use_colors;
bool _isatty;
};
diff --git a/zypper.conf b/zypper.conf
new file mode 100644
index 0000000..495d5d2
--- /dev/null
+++ b/zypper.conf
@@ -0,0 +1,42 @@
+## Configuration file for Zypper.
+##
+## You can edit this file by hand (try to keep the structure) or by using
+## 'zypper conf' command (TODO).
+## The options known also to libzypp will be overriden by zypper.conf, if set.
+##
+## Boolean values are 0 1 yes no on off true false
+
+[main]
+
+## Level of verbosity to use.
+##
+## Valid values: debug, verbose, normal, quiet
+## Default value: normal
+##
+# verbosity = normal
+
+[solver]
+
+## Do not install soft dependencies (recommended packages)
+##
+## Valid values: boolean
+## Default value: yes
+##
+# installRecommends = yes
+
+[color]
+
+## Whether to use colors
+##
+## Valid values: always, never, or autodetect
+## Default value: never
+##
+# useColors = never
+
+## Do you use dark or light terminal background?
+##
+## Default value: dark
+##
+# background = dark
+
+## TODO: options to configure specific colorization
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org