diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/build.cc | 15 | ||||
-rw-r--r-- | src/util.cc | 12 | ||||
-rw-r--r-- | src/util.h | 4 | ||||
-rw-r--r-- | src/util_test.cc | 11 |
4 files changed, 29 insertions, 13 deletions
diff --git a/src/build.cc b/src/build.cc index 09d7f65..a1c94e4 100644 --- a/src/build.cc +++ b/src/build.cc @@ -236,28 +236,17 @@ void BuildStatus::PrintStatus(Edge* edge) { to_print = FormatProgressStatus(progress_status_format_) + to_print; if (smart_terminal_ && !force_full_command) { - const int kMargin = 3; // Space for "...". #ifndef _WIN32 // Limit output to width of the terminal if provided so we don't cause // line-wrapping. winsize size; if ((ioctl(0, TIOCGWINSZ, &size) == 0) && size.ws_col) { - if (to_print.size() + kMargin > size.ws_col) { - int elide_size = (size.ws_col - kMargin) / 2; - to_print = to_print.substr(0, elide_size) - + "..." - + to_print.substr(to_print.size() - elide_size, elide_size); - } + to_print = ElideMiddle(to_print, size.ws_col); } #else // Don't use the full width or console will move to next line. size_t width = static_cast<size_t>(csbi.dwSize.X) - 1; - if (to_print.size() + kMargin > width) { - int elide_size = (width - kMargin) / 2; - to_print = to_print.substr(0, elide_size) - + "..." - + to_print.substr(to_print.size() - elide_size, elide_size); - } + to_print = ElideMiddle(to_print, width); #endif } diff --git a/src/util.cc b/src/util.cc index 4ce610b..be2347c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -330,3 +330,15 @@ double GetLoadAverage() { return loadavg[0]; } #endif // _WIN32 + +string ElideMiddle(const string& str, size_t width) { + const int kMargin = 3; // Space for "...". + string result = str; + if (result.size() + kMargin > width) { + int elide_size = (width - kMargin) / 2; + result = result.substr(0, elide_size) + + "..." + + result.substr(result.size() - elide_size, elide_size); + } + return result; +} @@ -70,6 +70,10 @@ int GetProcessorCount(); /// on error. double GetLoadAverage(); +/// Elide the given string @a str with '...' in the middle if the length +/// exceeds @a width. +string ElideMiddle(const string& str, size_t width); + #ifdef _MSC_VER #define snprintf _snprintf #define fileno _fileno diff --git a/src/util_test.cc b/src/util_test.cc index 23d4b83..5ace5e7 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -136,3 +136,14 @@ TEST(StripAnsiEscapeCodes, StripColors) { EXPECT_EQ("affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]", stripped); } + +TEST(ElideMiddle, NothingToElide) { + string input = "Nothing to elide in this short string."; + EXPECT_EQ(input, ElideMiddle(input, 80)); +} + +TEST(ElideMiddle, ElideInTheMiddle) { + string input = "01234567890123456789"; + string elided = ElideMiddle(input, 10); + EXPECT_EQ("012...789", elided); +} |