Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ANSI escape color codes in NINJA_STATUS #1454

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,21 @@ string BuildStatus::FormatProgressStatus(
char buf[32];
int percent;
for (const char* s = progress_status_format; *s != '\0'; ++s) {
if (*s == '%') {
// Support ANSI color escape codes in NINJA_STATUS
if (strncmp(s, "\\033[", 5) == 0) {
const char *end = strchr(s + 5, 'm');
if (end == NULL) {
// Not a valid ANSI color sequence, treat as regular text
} else {
out.append("\x1B[");
for (const char *t = s + 5; t <= end; ++t) {
out.push_back(*t);
}
s = end;
continue;
}
}
else if (*s == '%') {
++s;
switch (*s) {
case '%':
Expand Down
15 changes: 14 additions & 1 deletion src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,23 @@ double GetLoadAverage() {
}
#endif // _WIN32

// Calculate the width of a string accounting for ANSI escape codes
size_t CalculateWidth(const string& str) {
const int initial_width = str.size();
size_t zero_width_start = str.find("\x1B[");
if (zero_width_start != string::npos) {
size_t zero_width_end = str.find("m", zero_width_start);
if (zero_width_end != string::npos) {
return initial_width - (zero_width_end - zero_width_start) - 1;
}
}
return initial_width;
}

string ElideMiddle(const string& str, size_t width) {
const int kMargin = 3; // Space for "...".
string result = str;
if (result.size() + kMargin > width) {
if (CalculateWidth(result) + kMargin > width) {
size_t elide_size = (width - kMargin) / 2;
result = result.substr(0, elide_size)
+ "..."
Expand Down