| 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 library pretty_print; | 5 library pretty_print; |
| 6 | 6 |
| 7 import 'utils.dart'; | 7 import 'utils.dart'; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Returns a pretty-printed representation of [object]. | 10 * Returns a pretty-printed representation of [object]. |
| 11 * | 11 * |
| 12 * If [maxLineLength] is passed, this will attempt to ensure that each line is | 12 * If [maxLineLength] is passed, this will attempt to ensure that each line is |
| 13 * no longer than [maxLineLength] characters long. This isn't guaranteed, since | 13 * no longer than [maxLineLength] characters long. This isn't guaranteed, since |
| 14 * individual objects may have string representations that are too long, but | 14 * individual objects may have string representations that are too long, but |
| 15 * most lines will be less than [maxLineLength] long. | 15 * most lines will be less than [maxLineLength] long. |
| 16 * | 16 * |
| 17 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first | 17 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first |
| 18 * [maxItems] members or key/value pairs, respectively. | 18 * [maxItems] members or key/value pairs, respectively. |
| 19 */ | 19 */ |
| 20 String prettyPrint(object, {int maxLineLength, int maxItems}) { | 20 String prettyPrint(object, {int maxLineLength, int maxItems}) { |
| 21 String _prettyPrint(object, int indent, Set seen, bool top) { | 21 String _prettyPrint(object, int indent, Set seen, bool top) { |
| 22 // Avoid looping infinitely on recursively-nested data structures. | 22 // Avoid looping infinitely on recursively-nested data structures. |
| 23 if (seen.contains(object)) return "(recursive)"; | 23 if (seen.contains(object)) return "(recursive)"; |
| 24 seen = seen.union(new Set.from([object])); | 24 seen = seen.union(new Set.from([object])); |
| 25 String pp(child) => _prettyPrint(child, indent + 2, seen, false); | 25 String pp(child) => _prettyPrint(child, indent + 2, seen, false); |
| 26 | 26 |
| 27 if (object is Iterable) { | 27 if (object is Iterable) { |
| 28 // Print the type name for non-List iterables. | 28 // Print the type name for non-List iterables. |
| 29 var typeName = object is List ? "" : typeName(object) + ":"; | 29 var type = object is List ? "" : typeName(object) + ":"; |
| 30 | 30 |
| 31 // Truncate the list of strings if it's longer than [maxItems]. | 31 // Truncate the list of strings if it's longer than [maxItems]. |
| 32 var strings = object.map(pp).toList(); | 32 var strings = object.map(pp).toList(); |
| 33 if (maxItems != null && strings.length > maxItems) { | 33 if (maxItems != null && strings.length > maxItems) { |
| 34 strings.replaceRange(maxItems - 1, strings.length, ['...']); | 34 strings.replaceRange(maxItems - 1, strings.length, ['...']); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // If the printed string is short and doesn't contain a newline, print it | 37 // If the printed string is short and doesn't contain a newline, print it |
| 38 // as a single line. | 38 // as a single line. |
| 39 var singleLine = "$typeName[${strings.join(', ')}]"; | 39 var singleLine = "$type[${strings.join(', ')}]"; |
| 40 if ((maxLineLength == null || | 40 if ((maxLineLength == null || |
| 41 singleLine.length + indent <= maxLineLength) && | 41 singleLine.length + indent <= maxLineLength) && |
| 42 !singleLine.contains("\n")) { | 42 !singleLine.contains("\n")) { |
| 43 return singleLine; | 43 return singleLine; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Otherwise, print each member on its own line. | 46 // Otherwise, print each member on its own line. |
| 47 return "$typeName[\n" + strings.map((string) { | 47 return "$type[\n" + strings.map((string) { |
| 48 return _indent(indent + 2) + string; | 48 return _indent(indent + 2) + string; |
| 49 }).join(",\n") + "\n" + _indent(indent) + "]"; | 49 }).join(",\n") + "\n" + _indent(indent) + "]"; |
| 50 } else if (object is Map) { | 50 } else if (object is Map) { |
| 51 // Convert the contents of the map to string representations. | 51 // Convert the contents of the map to string representations. |
| 52 var strings = object.keys.map((key) { | 52 var strings = object.keys.map((key) { |
| 53 return '${pp(key)}: ${pp(object[key])}'; | 53 return '${pp(key)}: ${pp(object[key])}'; |
| 54 }).toList(); | 54 }).toList(); |
| 55 | 55 |
| 56 // Truncate the list of strings if it's longer than [maxItems]. | 56 // Truncate the list of strings if it's longer than [maxItems]. |
| 57 if (maxItems != null && strings.length > maxItems) { | 57 if (maxItems != null && strings.length > maxItems) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } else { | 93 } else { |
| 94 return "${typeName(object)}:$value"; | 94 return "${typeName(object)}:$value"; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 return _prettyPrint(object, 0, new Set(), true); | 99 return _prettyPrint(object, 0, new Set(), true); |
| 100 } | 100 } |
| 101 | 101 |
| 102 String _indent(int length) => new List.filled(length, ' ').join(''); | 102 String _indent(int length) => new List.filled(length, ' ').join(''); |
| OLD | NEW |