Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library pretty_print; | |
| 6 | |
| 7 import 'utils.dart'; | |
| 8 | |
| 9 /** | |
| 10 * Returns a pretty-printed representation of [object]. | |
| 11 * | |
| 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 | |
| 14 * individual objects may have string representations that are too long, but | |
| 15 * most lines will be less than [maxLineLength] long. | |
| 16 * | |
| 17 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first | |
| 18 * [maxItems] members or key/value pairs, respectively. | |
| 19 */ | |
| 20 String prettyPrint(object, {int maxLineLength, int maxItems}) { | |
| 21 String _prettyPrint(object, int indent, Set seen, bool top) { | |
| 22 // Avoid looping infinitely on recursively-nested data structures. | |
| 23 if (seen.contains(object)) return "(recursive)"; | |
| 24 seen = seen.union(new Set.from([object])); | |
| 25 String pp(child) => _prettyPrint(child, indent + 2, seen, false); | |
| 26 | |
| 27 if (object is Iterable) { | |
| 28 // Print the type name for non-List iterables. | |
| 29 var typeName = object is List ? "" : typeName(object) + ":"; | |
| 30 | |
| 31 // Truncate the list of strings if it's longer than [maxItems]. | |
| 32 var strings = object.map(pp).toList(); | |
| 33 if (maxItems != null && strings.length > maxItems) { | |
| 34 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
| 35 } | |
| 36 | |
| 37 // If the printed string is short and doesn't contain a newline, print it | |
| 38 // as a single line. | |
| 39 var singleLine = "$typeName[${strings.join(', ')}]"; | |
| 40 if ((maxLineLength == null || | |
| 41 singleLine.length + indent <= maxLineLength) && | |
| 42 !singleLine.contains("\n")) { | |
| 43 return singleLine; | |
| 44 } | |
| 45 | |
| 46 // Otherwise, print each member on its own line. | |
| 47 return "$typeName[\n" + strings.map((string) { | |
| 48 return _indent(indent + 2) + string; | |
| 49 }).join(",\n") + "\n" + _indent(indent) + "]"; | |
| 50 } else if (object is Map) { | |
| 51 // Convert the contents of the map to string representations. | |
| 52 var strings = object.keys.map((key) { | |
| 53 return '${pp(key)}: ${pp(object[key])}'; | |
| 54 }).toList(); | |
| 55 | |
| 56 // Truncate the list of strings if it's longer than [maxItems]. | |
| 57 if (maxItems != null && strings.length > maxItems) { | |
| 58 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
| 59 } | |
| 60 | |
| 61 // If the printed string is short and doesn't contain a newline, print it | |
| 62 // as a single line. | |
| 63 var singleLine = "{${strings.join(", ")}}"; | |
| 64 if ((maxLineLength == null || | |
| 65 singleLine.length + indent <= maxLineLength) && | |
| 66 !singleLine.contains("\n")) { | |
| 67 return singleLine; | |
| 68 } | |
| 69 | |
| 70 // Otherwise, print each key/value pair on its own line. | |
| 71 return "{\n" + strings.map((string) { | |
| 72 return _indent(indent + 2) + string; | |
| 73 }).join(",\n") + "\n" + _indent(indent) + "}"; | |
| 74 } else if (object is String) { | |
| 75 // Escape strings and print each line on its own line. | |
| 76 var lines = object.split("\n"); | |
| 77 return "'" + lines.map(escapeString) | |
| 78 .join("\\n'\n${_indent(indent + 2)}'") + "'"; | |
| 79 } else { | |
| 80 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); | |
| 81 var defaultToString = value.startsWith("Instance of "); | |
| 82 | |
| 83 // If this is the top-level call to [prettyPrint], wrap the value on angle | |
| 84 // brackets to set it apart visually. | |
| 85 if (top) value = "<$value>"; | |
| 86 | |
| 87 // Print the type of objects with custom [toString] methods. Primitive | |
| 88 // objects and objects that don't implement a custom [toString] don't need | |
| 89 // to have their types printed. | |
| 90 if (object is num || object is bool || object is Function || | |
| 91 object == null || defaultToString) { | |
| 92 return value; | |
| 93 } else { | |
| 94 return "${typeName(object)}:$value"; | |
|
gram
2013/05/17 20:38:33
The latest version has a lot of "Unknown:<value>"
nweiz
2013/05/17 21:07:21
Changed to "?" as per offline discussion.
| |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 return _prettyPrint(object, 0, new Set(), true); | |
| 100 } | |
| 101 | |
| 102 String _indent(int length) => new List.filled(length, ' ').join(''); | |
| OLD | NEW |