23 const int ProgressIndicator::spanStartDef = 0,
24 ProgressIndicator::spanEndDef = 100;
25 const std::string ProgressIndicator::messageDef =
"Progress: ";
26 const std::string ProgressIndicator::backspaceSequence =
"\033[A";
32 initialize(spanStartDef, spanEndDef, messageDef, 0);
40 const std::string &message,
41 const unsigned minLogLevel)
43 initialize(spanStartDef, spanEnd, message, minLogLevel);
52 const std::string &message,
53 const unsigned minLogLevel)
55 initialize(spanStart, spanEnd, message, minLogLevel);
58 ProgressIndicator::~ProgressIndicator()
60 if (state_ == running)
64 void ProgressIndicator::initialize(
const int spanStart,
66 const std::string &message,
67 const unsigned minLogLevel)
69 minLogLevel_ = minLogLevel;
71 int shellIsInteractive, shellTerminalFileNumber;
78 spanStart_ = spanStart;
80 spanPos_ = spanStart_;
81 spanLength_ = spanEnd_ - spanStart_;
86 shellTerminalFileNumber = STDIN_FILENO;
87 shellIsInteractive = isatty(shellTerminalFileNumber);
89 if (shellIsInteractive &&
90 getenv(
"TERM") != NULL &&
91 std::string(getenv(
"TERM")) !=
"dumb")
93 colorStart_ = NUKLEI_DBLACK;
94 colorEnd_ = NUKLEI_NOCOLOR;
97 if ( !shellIsInteractive || !ENABLE_CONSOLE_BACKSPACE )
109 std::cout <<
"isatty: " << shellIsInteractive << std::endl
110 <<
"spanLength: " << spanLength_ <<
" "
111 << spanStart_ <<
"->" << spanEnd_ << std::endl;
116 void ProgressIndicator::setMinLogLevel(
const unsigned l)
117 { minLogLevel_ = l; }
118 unsigned ProgressIndicator::getMinLogLevel()
const
119 {
return minLogLevel_; }
121 void ProgressIndicator::setBackspace(
const bool backspace)
123 backspace_ = backspace;
133 if (state_ == finished)
return;
134 if (spanLength_ == 0)
return;
138 if (LOG_LEVEL >= minLogLevel_) std::cout << colorStart_ << message_ <<
"[ 0% ]" << colorEnd_ << std::endl;
140 if (LOG_LEVEL >= minLogLevel_) LAST_OUTPUT_LINE_IS_PROGRESS =
true;
143 int count = (value - spanStart_) * scale_ / spanLength_;
144 if (count > current_)
147 if (LOG_LEVEL >= minLogLevel_)
151 if (LAST_OUTPUT_LINE_IS_PROGRESS)
152 std::cout << backspaceSequence;
153 std::cout << colorStart_ << message_ <<
"[ ";
155 int width = std::cout.width();
157 std::cout << int(current_ * 100 / scale_);
158 std::cout.width(width);
160 std::cout <<
"% ]" << colorEnd_ << std::endl;
161 LAST_OUTPUT_LINE_IS_PROGRESS =
true;
165 std::cout << colorStart_ << message_ <<
"[ ";
166 int width = std::cout.width();
168 std::cout << int(current_ * 100 / scale_);
169 std::cout.width(width);
170 std::cout <<
"% ]" << colorEnd_ << std::endl;
171 LAST_OUTPUT_LINE_IS_PROGRESS =
true;
174 if (current_ == scale_)
200 boost::unique_lock<boost::mutex> lock(Log::mutex_);
204 const std::string& ProgressIndicator::getMessage()
const
209 void ProgressIndicator::setMessage(
const std::string& message)
233 int ProgressIndicator::main(
int argc,
char ** argv)
237 std::cout <<
"no no no, test 0 100" << std::endl;
241 int a = strtol(argv[1], NULL, 10);
242 int b = strtol(argv[2], NULL, 10);
246 for (
int i = a; i <= b; i++)
249 for (
unsigned int j = 0; j < 2000000; j++);
253 std::string message =
"Second use: ";
254 pi.setMessage(message);
256 for (
int i = a; i <= b + 50 - a; i++)
259 for (
unsigned int j = 0; j < 7000000; j++);