From 8b590881013a05fd5017aa94185a02f6b7d09758 Mon Sep 17 00:00:00 2001 From: Nicolas Despres Date: Sat, 28 Jul 2012 14:41:20 +0200 Subject: Re-factor elide code and test it. --- src/build.cc | 15 ++------------- src/util.cc | 13 +++++++++++++ src/util.h | 4 ++++ src/util_test.cc | 11 +++++++++++ 4 files changed, 30 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(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 ca05292..cb3e141 100644 --- a/src/util.cc +++ b/src/util.cc @@ -311,3 +311,16 @@ double GetLoadAverage() return GetLoadAverage_unix(); #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; +} diff --git a/src/util.h b/src/util.h index fd91772..4c0f4bb 100644 --- a/src/util.h +++ b/src/util.h @@ -66,6 +66,10 @@ string StripAnsiEscapeCodes(const string& in); /// 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); +} -- cgit v0.12