Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Unified Diff: lib/src/utils.dart

Issue 971123002: Add a variant on ConsoleReporter that doesn't import dart:io. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/runner/reporter/no_io_compact.dart ('k') | lib/unittest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/utils.dart
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 6b0939fe5ccf805bcf9ffb33e5e9d86b045eb1c1..06bd8dff5ab7b657076ef15c9b931b4ca0401c1e 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -63,3 +63,44 @@ List flatten(Iterable nested) {
helper(nested);
return result;
}
+
+/// Truncates [text] to fit within [maxLength].
+///
+/// This will try to truncate along word boundaries and preserve words both at
+/// the beginning and the end of [text].
+String truncate(String text, int maxLength) {
+ // Return the full message if it fits.
+ if (text.length <= maxLength) return text;
+
+ // If we can fit the first and last three words, do so.
+ var words = text.split(' ');
+ if (words.length > 1) {
+ var i = words.length;
+ var length = words.first.length + 4;
+ do {
+ i--;
+ length += 1 + words[i].length;
+ } while (length <= maxLength && i > 0);
+ if (length > maxLength || i == 0) i++;
+ if (i < words.length - 4) {
+ // Require at least 3 words at the end.
+ var buffer = new StringBuffer();
+ buffer.write(words.first);
+ buffer.write(' ...');
+ for ( ; i < words.length; i++) {
+ buffer.write(' ');
+ buffer.write(words[i]);
+ }
+ return buffer.toString();
+ }
+ }
+
+ // Otherwise truncate to return the trailing text, but attempt to start at
+ // the beginning of a word.
+ var result = text.substring(text.length - maxLength + 4);
+ var firstSpace = result.indexOf(' ');
+ if (firstSpace > 0) {
+ result = result.substring(firstSpace);
+ }
+ return '...$result';
+}
« no previous file with comments | « lib/src/runner/reporter/no_io_compact.dart ('k') | lib/unittest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698