Skip to content

Commit

Permalink
Allow to change also order of description in NINJA_STATUS
Browse files Browse the repository at this point in the history
This commit adds new format string %d for printing description. When %d is
not specified in NINJA_STATUS, then description is automatically printed at
the end of line.

This allows to add additional strings after description, for example
changing colors on ANSI terminals via NINJA_STATUS variable.
  • Loading branch information
pali committed Oct 28, 2015
1 parent 00fbdde commit f0fa276
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ void BuildStatus::BuildFinished() {
}

string BuildStatus::FormatProgressStatus(
const char* progress_status_format) const {
const char* progress_status_format,
const string to_print) const {
string out;
char buf[32];
int percent;
bool has_desc = false;
char total_edges_str[32];
snprintf(total_edges_str, sizeof(total_edges_str), "%d", total_edges_);
int total_edges_len = strlen(total_edges_str);
Expand Down Expand Up @@ -228,6 +230,12 @@ string BuildStatus::FormatProgressStatus(
break;
}

// Description (to_print)
case 'd':
out += to_print;
has_desc = true;
break;

default:
Fatal("unknown placeholder '%%%c' in $NINJA_STATUS", *s);
return "";
Expand All @@ -237,6 +245,9 @@ string BuildStatus::FormatProgressStatus(
}
}

if (!has_desc)
out += to_print;

return out;
}

Expand All @@ -254,7 +265,7 @@ void BuildStatus::PrintStatus(Edge* edge) {
overall_rate_.Restart();
current_rate_.Restart();
}
to_print = FormatProgressStatus(progress_status_format_) + to_print;
to_print = FormatProgressStatus(progress_status_format_, to_print);

printer_.Print(to_print,
force_full_command ? LinePrinter::FULL : LinePrinter::ELIDE);
Expand Down
2 changes: 1 addition & 1 deletion src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ struct BuildStatus {
/// See the user manual for more information about the available
/// placeholders.
/// @param progress_status_format The format of the progress status.
string FormatProgressStatus(const char* progress_status_format) const;
string FormatProgressStatus(const char* progress_status_format, const string to_print) const;

private:
void PrintStatus(Edge* edge);
Expand Down
4 changes: 2 additions & 2 deletions src/build_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ TEST_F(BuildWithLogTest, RestatTest) {
ASSERT_EQ("", err);
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ("", err);
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]", ""));
command_runner_.commands_ran_.clear();
state_.Reset();

Expand Down Expand Up @@ -1621,7 +1621,7 @@ TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {

TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]", ""));
}

TEST_F(BuildTest, FailedDepsParse) {
Expand Down

0 comments on commit f0fa276

Please sign in to comment.