Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * A test configuration that generates a compact 1-line progress bar. The bar is | 6 * A test configuration that generates a compact 1-line progress bar. The bar is |
| 7 * updated in-place before and after each test is executed. If all test pass, | 7 * updated in-place before and after each test is executed. If all test pass, |
| 8 * you should only see a couple lines in the terminal. If a test fails, the | 8 * you should only see a couple lines in the terminal. If a test fails, the |
| 9 * failure is shown and the progress bar continues to be updated below it. | 9 * failure is shown and the progress bar continues to be updated below it. |
| 10 */ | 10 */ |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 | 82 |
| 83 int _lastLength = 0; | 83 int _lastLength = 0; |
| 84 | 84 |
| 85 final int _nonVisiblePrefix = 1 + _GREEN.length + _NONE.length; | 85 final int _nonVisiblePrefix = 1 + _GREEN.length + _NONE.length; |
| 86 | 86 |
| 87 void _progressLine(DateTime startTime, int passed, int failed, String message, | 87 void _progressLine(DateTime startTime, int passed, int failed, String message, |
| 88 [String color = _NONE]) { | 88 [String color = _NONE]) { |
| 89 var duration = (new DateTime.now()).difference(startTime); | 89 var duration = (new DateTime.now()).difference(startTime); |
| 90 var buffer = new StringBuffer(); | 90 var buffer = new StringBuffer(); |
| 91 // \r moves back to the beginnig of the current line. | 91 // \r moves back to the beginnig of the current line. |
| 92 buffer.add('\r${_timeString(duration)} '); | 92 buffer.write('\r${_timeString(duration)} '); |
| 93 buffer.add(_GREEN); | 93 buffer.write(_GREEN); |
| 94 buffer.add('+'); | 94 buffer.write('+'); |
| 95 buffer.add(passed); | 95 buffer.write(passed); |
| 96 buffer.add(_NONE); | 96 buffer.write(_NONE); |
| 97 if (failed != 0) buffer.add(_RED); | 97 if (failed != 0) buffer.write(_RED); |
| 98 buffer.add(' -'); | 98 buffer.write(' -'); |
| 99 buffer.add(failed); | 99 buffer.write(failed); |
| 100 if (failed != 0) buffer.add(_NONE); | 100 if (failed != 0) buffer.write(_NONE); |
| 101 buffer.add(': '); | 101 buffer.write(': '); |
| 102 buffer.add(color); | 102 buffer.write(color); |
| 103 | 103 |
| 104 int nonVisible = _nonVisiblePrefix + color.length + | 104 int nonVisible = _nonVisiblePrefix + color.length + |
|
gram
2013/02/22 17:36:11
Can you add a comment to this block of code please
Siggi Cherem (dart-lang)
2013/02/22 17:39:00
already submitted this one, but will do in a separ
| |
| 105 (failed != 0 ? (_RED.length + _NONE.length) : 0); | 105 (failed != 0 ? (_RED.length + _NONE.length) : 0); |
| 106 int len = buffer.length - nonVisible; | 106 int len = buffer.length - nonVisible; |
| 107 var mx = MAX_LINE - len; | 107 var mx = MAX_LINE - len; |
| 108 buffer.add(_snippet(message, MAX_LINE - len)); | 108 buffer.write(_snippet(message, MAX_LINE - len)); |
| 109 buffer.add(_NONE); | 109 buffer.write(_NONE); |
| 110 | 110 |
| 111 // Pad the rest of the line so that it looks erased. | 111 // Pad the rest of the line so that it looks erased. |
| 112 len = buffer.length - nonVisible - _NONE.length; | 112 len = buffer.length - nonVisible - _NONE.length; |
| 113 if (len > _lastLength) { | 113 if (len > _lastLength) { |
| 114 _lastLength = len; | 114 _lastLength = len; |
| 115 } else { | 115 } else { |
| 116 while (len < _lastLength) { | 116 while (len < _lastLength) { |
| 117 buffer.add(' '); | 117 buffer.write(' '); |
| 118 _lastLength--; | 118 _lastLength--; |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 stdout.writeString(buffer.toString()); | 121 stdout.addString(buffer.toString()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 String _padTime(int time) => | 124 String _padTime(int time) => |
| 125 (time == 0) ? '00' : ((time < 10) ? '0$time' : '$time'); | 125 (time == 0) ? '00' : ((time < 10) ? '0$time' : '$time'); |
| 126 | 126 |
| 127 String _timeString(Duration duration) { | 127 String _timeString(Duration duration) { |
| 128 var min = duration.inMinutes; | 128 var min = duration.inMinutes; |
| 129 var sec = duration.inSeconds % 60; | 129 var sec = duration.inSeconds % 60; |
| 130 return '${_padTime(min)}:${_padTime(sec)}'; | 130 return '${_padTime(min)}:${_padTime(sec)}'; |
| 131 } | 131 } |
| 132 | 132 |
| 133 String _snippet(String text, int maxLength) { | 133 String _snippet(String text, int maxLength) { |
| 134 // Return the full message if it fits | 134 // Return the full message if it fits |
| 135 if (text.length <= maxLength) return text; | 135 if (text.length <= maxLength) return text; |
| 136 | 136 |
| 137 // If we can fit the first and last three words, do so. | 137 // If we can fit the first and last three words, do so. |
| 138 var words = text.split(' '); | 138 var words = text.split(' '); |
| 139 if (words.length > 1) { | 139 if (words.length > 1) { |
| 140 int i = words.length; | 140 int i = words.length; |
| 141 var len = words.first.length + 4; | 141 var len = words.first.length + 4; |
| 142 do { | 142 do { |
| 143 len += 1 + words[--i].length; | 143 len += 1 + words[--i].length; |
| 144 } while (len <= maxLength && i > 0); | 144 } while (len <= maxLength && i > 0); |
| 145 if (len > maxLength || i == 0) i++; | 145 if (len > maxLength || i == 0) i++; |
| 146 if (i < words.length - 4) { | 146 if (i < words.length - 4) { |
| 147 // Require at least 3 words at the end. | 147 // Require at least 3 words at the end. |
| 148 var buffer = new StringBuffer(); | 148 var buffer = new StringBuffer(); |
| 149 buffer.add(words.first); | 149 buffer.write(words.first); |
| 150 buffer.add(' ...'); | 150 buffer.write(' ...'); |
| 151 for (; i < words.length; i++) { | 151 for (; i < words.length; i++) { |
| 152 buffer.add(' '); | 152 buffer.write(' '); |
| 153 buffer.add(words[i]); | 153 buffer.write(words[i]); |
| 154 } | 154 } |
| 155 return buffer.toString(); | 155 return buffer.toString(); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Otherwise truncate to return the trailing text, but attempt to start at | 159 // Otherwise truncate to return the trailing text, but attempt to start at |
| 160 // the beginning of a word. | 160 // the beginning of a word. |
| 161 var res = text.substring(text.length - maxLength + 4); | 161 var res = text.substring(text.length - maxLength + 4); |
| 162 var firstSpace = res.indexOf(' '); | 162 var firstSpace = res.indexOf(' '); |
| 163 if (firstSpace > 0) { | 163 if (firstSpace > 0) { |
| 164 res = res.substring(firstSpace); | 164 res = res.substring(firstSpace); |
| 165 } | 165 } |
| 166 return '...$res'; | 166 return '...$res'; |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 void useCompactVMConfiguration() { | 170 void useCompactVMConfiguration() { |
| 171 if (config != null) return; | 171 if (config != null) return; |
| 172 configure(new CompactVMConfiguration()); | 172 configure(new CompactVMConfiguration()); |
| 173 } | 173 } |
| OLD | NEW |