Add --enable-color option to enable/disable terminal color output

pull/195/head
Tatsuhiro Tsujikawa 2014-01-30 00:12:24 +09:00
parent f2fa24b418
commit 30e4077440
14 changed files with 57 additions and 9 deletions

View File

@ -1138,6 +1138,11 @@ Advanced Options
printed for each requested file in each row.
Default: ``default``
.. option:: --enable-color[=true|false]
Enable color output for a terminal.
Default: ``true``
.. option:: --enable-mmap[=true|false]
Map files into memory. This option may not work if the file space

View File

@ -63,6 +63,7 @@
#include "FileEntry.h"
#include "console.h"
#include "ColorizedStream.h"
#include "Option.h"
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
@ -264,15 +265,18 @@ void printProgressSummary(const RequestGroupList& groups, size_t cols,
}
} // namespace
ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval,
bool colorOutput,
bool humanReadable):
summaryInterval_(summaryInterval),
readoutVisibility_(true),
truncate_(true),
#ifdef __MINGW32__
isTTY_(true)
isTTY_(true),
#else // !__MINGW32__
isTTY_(isatty(STDOUT_FILENO) == 1)
isTTY_(isatty(STDOUT_FILENO) == 1),
#endif // !__MINGW32__
colorOutput_(colorOutput)
{
if(humanReadable) {
sizeFormatter_.reset(new AbbrevSizeFormatter());
@ -327,7 +331,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
return;
}
size_t numGroup = e->getRequestGroupMan()->countRequestGroup();
const bool color = global::cout()->supportsColor() && isTTY_;
const bool color = global::cout()->supportsColor() && isTTY_ && colorOutput_;
if(numGroup == 1) {
const std::shared_ptr<RequestGroup>& rg =
*e->getRequestGroupMan()->getRequestGroups().begin();

View File

@ -67,8 +67,10 @@ private:
bool readoutVisibility_;
bool truncate_;
bool isTTY_;
bool colorOutput_;
public:
ConsoleStatCalc(time_t summaryInterval, bool humanReadable = true);
ConsoleStatCalc(time_t summaryInterval, bool colorOutput = true,
bool humanReadable = true);
virtual ~ConsoleStatCalc() {}

View File

@ -168,6 +168,7 @@ Context::Context(bool standalone,
LogFactory::setLogFile(op->get(PREF_LOG));
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
LogFactory::setColorOutput(op->getAsBool(PREF_ENABLE_COLOR));
if(op->getAsBool(PREF_QUIET)) {
LogFactory::setConsoleOutput(false);
}

View File

@ -48,6 +48,7 @@ std::shared_ptr<Logger> LogFactory::logger_;
bool LogFactory::consoleOutput_ = true;
Logger::LEVEL LogFactory::logLevel_ = Logger::A2_DEBUG;
Logger::LEVEL LogFactory::consoleLogLevel_ = Logger::A2_NOTICE;
bool LogFactory::colorOutput_ = true;
void LogFactory::openLogger(const std::shared_ptr<Logger>& logger)
{
@ -59,6 +60,7 @@ void LogFactory::openLogger(const std::shared_ptr<Logger>& logger)
logger->setLogLevel(logLevel_);
logger->setConsoleLogLevel(consoleLogLevel_);
logger->setConsoleOutput(consoleOutput_);
logger->setColorOutput(colorOutput_);
}
void LogFactory::adjustDependentLevels() {
@ -154,6 +156,11 @@ void LogFactory::setConsoleLogLevel(const std::string& level)
adjustDependentLevels();
}
void LogFactory::setColorOutput(bool enabled)
{
colorOutput_ = enabled;
}
void LogFactory::release() {
logger_.reset();
}

View File

@ -51,6 +51,7 @@ private:
static bool consoleOutput_;
static Logger::LEVEL logLevel_;
static Logger::LEVEL consoleLogLevel_;
static bool colorOutput_;
static void openLogger(const std::shared_ptr<Logger>& logger);
@ -101,6 +102,12 @@ public:
*/
static void setConsoleLogLevel(const std::string& level);
/**
* Enable color output if |enabled| is true. By default, color
* output is enabled for terminal.
*/
static void setColorOutput(bool enabled);
/**
* Releases used resources
*/

View File

@ -54,7 +54,7 @@ Logger::Logger()
: logLevel_(Logger::A2_DEBUG),
consoleLogLevel_(Logger::A2_NOTICE),
consoleOutput_(true),
useColor_(global::cout()->supportsColor())
colorOutput_(global::cout()->supportsColor())
{}
Logger::~Logger()
@ -86,6 +86,11 @@ void Logger::setConsoleOutput(bool enabled)
consoleOutput_ = enabled;
}
void Logger::setColorOutput(bool enabled)
{
colorOutput_ = enabled;
}
bool Logger::fileLogEnabled(LEVEL level)
{
return level >= logLevel_ && fpp_;
@ -207,7 +212,7 @@ void Logger::writeLog
}
if(consoleLogEnabled(level)) {
global::cout()->printf("\n");
writeHeaderConsole(*global::cout(), level, useColor_);
writeHeaderConsole(*global::cout(), level, colorOutput_);
global::cout()->printf("%s\n", msg);
writeStackTrace(*global::cout(), trace);
global::cout()->flush();

View File

@ -62,7 +62,7 @@ private:
LEVEL consoleLogLevel_;
// true if console log output is enabled.
bool consoleOutput_;
bool useColor_;
bool colorOutput_;
// Don't allow copying
Logger(const Logger&);
Logger& operator=(const Logger&);
@ -127,6 +127,8 @@ public:
void setConsoleOutput(bool enabled);
void setColorOutput(bool enabled);
// Returns true if this logger actually writes debug log message to
// either file or stdout.
bool levelEnabled(LEVEL level);

View File

@ -136,6 +136,7 @@ std::unique_ptr<StatCalc> getStatCalc(const std::shared_ptr<Option>& op)
return make_unique<NullStatCalc>();
}
auto impl = make_unique<ConsoleStatCalc>(op->getAsInt(PREF_SUMMARY_INTERVAL),
op->getAsBool(PREF_ENABLE_COLOR),
op->getAsBool(PREF_HUMAN_READABLE));
impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));

View File

@ -305,6 +305,15 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
handlers.push_back(op);
}
#endif // ENABLE_ASYNC_DNS
{
OptionHandler* op(new BooleanOptionHandler
(PREF_ENABLE_COLOR,
TEXT_ENABLE_COLOR,
A2_V_TRUE,
OptionHandler::OPT_ARG));
op->addTag(TAG_ADVANCED);
handlers.push_back(op);
}
#if defined(HAVE_MMAP) || defined(__MINGW32__)
{
OptionHandler* op(new BooleanOptionHandler

View File

@ -631,7 +631,7 @@ void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
}
std::string line(pathRowSize, '=');
o.printf("%s\n", line.c_str());
bool useColor = o.supportsColor();
bool useColor = o.supportsColor() && option_->getAsBool(PREF_ENABLE_COLOR);
int ok = 0;
int err = 0;
int inpr = 0;

View File

@ -358,6 +358,7 @@ PrefPtr PREF_DISK_CACHE = makePref("disk-cache");
PrefPtr PREF_GID = makePref("gid");
// values: 1*digit
PrefPtr PREF_SAVE_SESSION_INTERVAL = makePref("save-session-interval");
PrefPtr PREF_ENABLE_COLOR = makePref("enable-color");
/**
* FTP related preferences

View File

@ -294,6 +294,8 @@ extern PrefPtr PREF_DISK_CACHE;
extern PrefPtr PREF_GID;
// values: 1*digit
extern PrefPtr PREF_SAVE_SESSION_INTERVAL;
// value: true |false
extern PrefPtr PREF_ENABLE_COLOR;
/**
* FTP related preferences

View File

@ -958,3 +958,5 @@
" specified by --save-session option every SEC\n" \
" seconds. If 0 is given, file will be saved only\n" \
" when aria2 exits.")
#define TEXT_ENABLE_COLOR \
_(" --enable-color[=true|false] Enable color output for a terminal.")