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 Nov 16, 2018
1 parent e622266 commit 56dd115
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ void BuildStatus::BuildFinished() {
}

string BuildStatus::FormatProgressStatus(
const char* progress_status_format, EdgeStatus status) const {
const char* progress_status_format, EdgeStatus status, 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 @@ -261,6 +262,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 @@ -270,6 +277,9 @@ string BuildStatus::FormatProgressStatus(
}
}

if (!has_desc)
out += to_print;

return out;
}

Expand All @@ -283,7 +293,7 @@ void BuildStatus::PrintStatus(Edge* edge, EdgeStatus status) {
if (to_print.empty() || force_full_command)
to_print = edge->GetBinding("command");

to_print = FormatProgressStatus(progress_status_format_, status) + to_print;
to_print = FormatProgressStatus(progress_status_format_, status, 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 @@ -231,7 +231,7 @@ struct BuildStatus {
/// @param progress_status_format The format of the progress status.
/// @param status The status of the edge.
string FormatProgressStatus(const char* progress_status_format,
EdgeStatus status) const;
EdgeStatus status, const string to_print) const;

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

Expand Down Expand Up @@ -1768,13 +1768,13 @@ TEST_F(BuildTest, StatusFormatElapsed) {
// Before any task is done, the elapsed time must be zero.
EXPECT_EQ("[%/e0.000]",
status_.FormatProgressStatus("[%%/e%e]",
BuildStatus::kEdgeStarted));
BuildStatus::kEdgeStarted, ""));
}

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

TEST_F(BuildTest, FailedDepsParse) {
Expand Down

0 comments on commit 56dd115

Please sign in to comment.