Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit e2de9d2

Browse files
committed
Merge pull request ninja-build#1147 from colincross/elapsed_status
Fix NINJA_STATUS %e and %r on dumb terminals
2 parents 644f6b1 + af4973d commit e2de9d2

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/build.cc

+18-11
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void BuildStatus::BuildEdgeStarted(Edge* edge) {
9797
++started_edges_;
9898

9999
if (edge->use_console() || printer_.is_smart_terminal())
100-
PrintStatus(edge);
100+
PrintStatus(edge, kEdgeStarted);
101101

102102
if (edge->use_console())
103103
printer_.SetConsoleLocked(true);
@@ -109,6 +109,12 @@ void BuildStatus::BuildEdgeFinished(Edge* edge,
109109
int* start_time,
110110
int* end_time) {
111111
int64_t now = GetTimeMillis();
112+
113+
if (finished_edges_ == 0) {
114+
overall_rate_.Restart();
115+
current_rate_.Restart();
116+
}
117+
112118
++finished_edges_;
113119

114120
RunningEdgeMap::iterator i = running_edges_.find(edge);
@@ -123,7 +129,7 @@ void BuildStatus::BuildEdgeFinished(Edge* edge,
123129
return;
124130

125131
if (!edge->use_console())
126-
PrintStatus(edge);
132+
PrintStatus(edge, kEdgeFinished);
127133

128134
// Print the command that is spewing before printing its output.
129135
if (!success) {
@@ -164,7 +170,7 @@ void BuildStatus::BuildFinished() {
164170
}
165171

166172
string BuildStatus::FormatProgressStatus(
167-
const char* progress_status_format) const {
173+
const char* progress_status_format, EdgeStatus status) const {
168174
string out;
169175
char buf[32];
170176
int percent;
@@ -189,10 +195,15 @@ string BuildStatus::FormatProgressStatus(
189195
break;
190196

191197
// Running edges.
192-
case 'r':
193-
snprintf(buf, sizeof(buf), "%d", started_edges_ - finished_edges_);
198+
case 'r': {
199+
int running_edges = started_edges_ - finished_edges_;
200+
// count the edge that just finished as a running edge
201+
if (status == kEdgeFinished)
202+
running_edges++;
203+
snprintf(buf, sizeof(buf), "%d", running_edges);
194204
out += buf;
195205
break;
206+
}
196207

197208
// Unstarted edges.
198209
case 'u':
@@ -246,7 +257,7 @@ string BuildStatus::FormatProgressStatus(
246257
return out;
247258
}
248259

249-
void BuildStatus::PrintStatus(Edge* edge) {
260+
void BuildStatus::PrintStatus(Edge* edge, EdgeStatus status) {
250261
if (config_.verbosity == BuildConfig::QUIET)
251262
return;
252263

@@ -256,11 +267,7 @@ void BuildStatus::PrintStatus(Edge* edge) {
256267
if (to_print.empty() || force_full_command)
257268
to_print = edge->GetBinding("command");
258269

259-
if (finished_edges_ == 0) {
260-
overall_rate_.Restart();
261-
current_rate_.Restart();
262-
}
263-
to_print = FormatProgressStatus(progress_status_format_) + to_print;
270+
to_print = FormatProgressStatus(progress_status_format_, status) + to_print;
264271

265272
printer_.Print(to_print,
266273
force_full_command ? LinePrinter::FULL : LinePrinter::ELIDE);

src/build.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,21 @@ struct BuildStatus {
202202
int* start_time, int* end_time);
203203
void BuildFinished();
204204

205+
enum EdgeStatus {
206+
kEdgeStarted,
207+
kEdgeFinished,
208+
};
209+
205210
/// Format the progress status string by replacing the placeholders.
206211
/// See the user manual for more information about the available
207212
/// placeholders.
208213
/// @param progress_status_format The format of the progress status.
209-
string FormatProgressStatus(const char* progress_status_format) const;
214+
/// @param finished True if the edge being printed just finished
215+
string FormatProgressStatus(const char* progress_status_format,
216+
EdgeStatus kEdgeFinished) const;
210217

211218
private:
212-
void PrintStatus(Edge* edge);
219+
void PrintStatus(Edge* edge, EdgeStatus status);
213220

214221
const BuildConfig& config_;
215222

src/build_test.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,8 @@ TEST_F(BuildWithLogTest, RestatTest) {
12861286
ASSERT_EQ("", err);
12871287
EXPECT_TRUE(builder_.Build(&err));
12881288
ASSERT_EQ("", err);
1289-
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
1289+
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]",
1290+
BuildStatus::kEdgeStarted));
12901291
command_runner_.commands_ran_.clear();
12911292
state_.Reset();
12921293

@@ -1726,7 +1727,8 @@ TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
17261727

17271728
TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
17281729
EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1729-
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1730+
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]",
1731+
BuildStatus::kEdgeStarted));
17301732
}
17311733

17321734
TEST_F(BuildTest, FailedDepsParse) {

0 commit comments

Comments
 (0)