summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Despres <nicolas.despres@gmail.com>2012-07-28 12:41:20 (GMT)
committerNicolas Despres <nicolas.despres@gmail.com>2012-07-31 07:24:52 (GMT)
commit8b590881013a05fd5017aa94185a02f6b7d09758 (patch)
treee83647d4ae44cbaa6f7401b350980f74adb06034
parentb26f978ac3d7c5fd91de675bba62f31f704e0e04 (diff)
downloadNinja-8b590881013a05fd5017aa94185a02f6b7d09758.zip
Ninja-8b590881013a05fd5017aa94185a02f6b7d09758.tar.gz
Ninja-8b590881013a05fd5017aa94185a02f6b7d09758.tar.bz2
Re-factor elide code and test it.
-rw-r--r--src/build.cc15
-rw-r--r--src/util.cc13
-rw-r--r--src/util.h4
-rw-r--r--src/util_test.cc11
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<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 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);
+}