OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// A simple library for rendering tree-like structures in ASCII. | 5 /// A simple library for rendering tree-like structures in ASCII. |
6 import 'package:path/path.dart' as path; | 6 import 'package:path/path.dart' as path; |
7 | 7 |
8 import 'log.dart' as log; | 8 import 'log.dart' as log; |
9 import 'utils.dart'; | 9 import 'utils.dart'; |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 /// |-- path_windows_test.dart | 53 /// |-- path_windows_test.dart |
54 /// |-- relative_test.dart | 54 /// |-- relative_test.dart |
55 /// '-- split_test.dart | 55 /// '-- split_test.dart |
56 /// | 56 /// |
57 /// If [baseDir] is passed, it will be used as the root of the tree. | 57 /// If [baseDir] is passed, it will be used as the root of the tree. |
58 /// | 58 /// |
59 /// If [showAllChildren] is `false`, then directories with more than ten items | 59 /// If [showAllChildren] is `false`, then directories with more than ten items |
60 /// will have their contents truncated. Defaults to `false`. | 60 /// will have their contents truncated. Defaults to `false`. |
61 String fromFiles(List<String> files, {String baseDir, bool showAllChildren}) { | 61 String fromFiles(List<String> files, {String baseDir, bool showAllChildren}) { |
62 // Parse out the files into a tree of nested maps. | 62 // Parse out the files into a tree of nested maps. |
63 var root = {}; | 63 var root = <String, Map>{}; |
64 for (var file in files) { | 64 for (var file in files) { |
65 if (baseDir != null) file = path.relative(file, from: baseDir); | 65 if (baseDir != null) file = path.relative(file, from: baseDir); |
66 var directory = root; | 66 var directory = root; |
67 for (var part in path.split(file)) { | 67 for (var part in path.split(file)) { |
68 directory = directory.putIfAbsent(part, () => {}); | 68 directory = directory.putIfAbsent(part, () => <String, Map>{}) |
69 as Map<String, Map>; | |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 // Walk the map recursively and render to a string. | 73 // Walk the map recursively and render to a string. |
73 return fromMap(root, showAllChildren: showAllChildren); | 74 return fromMap(root, showAllChildren: showAllChildren); |
74 } | 75 } |
75 | 76 |
76 /// Draws a tree from a nested map. Given a map like: | 77 /// Draws a tree from a nested map. Given a map like: |
77 /// | 78 /// |
78 /// { | 79 /// { |
(...skipping 11 matching lines...) Expand all Loading... | |
90 /// analyzer | 91 /// analyzer |
91 /// |-- args | 92 /// |-- args |
92 /// | '-- collection | 93 /// | '-- collection |
93 /// '---logging | 94 /// '---logging |
94 /// barback | 95 /// barback |
95 /// | 96 /// |
96 /// Items with no children should have an empty map as the value. | 97 /// Items with no children should have an empty map as the value. |
97 /// | 98 /// |
98 /// If [showAllChildren] is `false`, then directories with more than ten items | 99 /// If [showAllChildren] is `false`, then directories with more than ten items |
99 /// will have their contents truncated. Defaults to `false`. | 100 /// will have their contents truncated. Defaults to `false`. |
100 String fromMap(Map map, {bool showAllChildren}) { | 101 String fromMap(Map<String, Map> map, {bool showAllChildren}) { |
101 var buffer = new StringBuffer(); | 102 var buffer = new StringBuffer(); |
102 _draw(buffer, "", null, map, showAllChildren: showAllChildren); | 103 _draw(buffer, "", null, map, showAllChildren: showAllChildren); |
103 return buffer.toString(); | 104 return buffer.toString(); |
104 } | 105 } |
105 | 106 |
106 void _drawLine(StringBuffer buffer, String prefix, bool isLastChild, | 107 void _drawLine(StringBuffer buffer, String prefix, bool isLastChild, |
107 String name) { | 108 String name) { |
108 // Print lines. | 109 // Print lines. |
109 buffer.write(prefix); | 110 buffer.write(prefix); |
110 if (name != null) { | 111 if (name != null) { |
111 if (isLastChild) { | 112 if (isLastChild) { |
112 buffer.write(log.gray("'-- ")); | 113 buffer.write(log.gray("'-- ")); |
113 } else { | 114 } else { |
114 buffer.write(log.gray("|-- ")); | 115 buffer.write(log.gray("|-- ")); |
115 } | 116 } |
116 } | 117 } |
117 | 118 |
118 // Print name. | 119 // Print name. |
119 buffer.writeln(name); | 120 buffer.writeln(name); |
120 } | 121 } |
121 | 122 |
122 String _getPrefix(bool isRoot, bool isLast) { | 123 String _getPrefix(bool isRoot, bool isLast) { |
123 if (isRoot) return ""; | 124 if (isRoot) return ""; |
124 if (isLast) return " "; | 125 if (isLast) return " "; |
125 return log.gray("| "); | 126 return log.gray("| "); |
126 } | 127 } |
127 | 128 |
128 void _draw(StringBuffer buffer, String prefix, String name, Map children, | 129 void _draw(StringBuffer buffer, String prefix, String name, |
129 {bool showAllChildren, bool isLast: false}) { | 130 Map<String, Map> children, {bool showAllChildren, |
131 bool isLast: false}) { | |
Bob Nystrom
2016/07/29 00:31:22
Did dartfmt do this? If so, can you file a bug. It
nweiz
2016/08/01 19:59:15
No, I did. Changed to formatter style.
| |
130 if (showAllChildren == null) showAllChildren = false; | 132 if (showAllChildren == null) showAllChildren = false; |
131 | 133 |
132 // Don't draw a line for the root node. | 134 // Don't draw a line for the root node. |
133 if (name != null) _drawLine(buffer, prefix, isLast, name); | 135 if (name != null) _drawLine(buffer, prefix, isLast, name); |
134 | 136 |
135 // Recurse to the children. | 137 // Recurse to the children. |
136 var childNames = ordered(children.keys); | 138 var childNames = ordered(children.keys); |
137 | 139 |
138 drawChild(bool isLastChild, String child) { | 140 drawChild(bool isLastChild, String child) { |
139 var childPrefix = _getPrefix(name == null, isLast); | 141 var childPrefix = _getPrefix(name == null, isLast); |
140 _draw(buffer, '$prefix$childPrefix', child, children[child], | 142 _draw( |
141 showAllChildren: showAllChildren, isLast: isLastChild); | 143 buffer, |
144 '$prefix$childPrefix', | |
145 child, | |
146 children[child] as Map<String, Map>, | |
147 showAllChildren: showAllChildren, | |
148 isLast: isLastChild); | |
142 } | 149 } |
143 | 150 |
144 if (name == null || showAllChildren || childNames.length <= 10) { | 151 if (name == null || showAllChildren || childNames.length <= 10) { |
145 // Not too many, so show all the children. | 152 // Not too many, so show all the children. |
146 for (var i = 0; i < childNames.length; i++) { | 153 for (var i = 0; i < childNames.length; i++) { |
147 drawChild(i == childNames.length - 1, childNames[i]); | 154 drawChild(i == childNames.length - 1, childNames[i]); |
148 } | 155 } |
149 } else { | 156 } else { |
150 // Show the first few. | 157 // Show the first few. |
151 drawChild(false, childNames[0]); | 158 drawChild(false, childNames[0]); |
152 drawChild(false, childNames[1]); | 159 drawChild(false, childNames[1]); |
153 drawChild(false, childNames[2]); | 160 drawChild(false, childNames[2]); |
154 | 161 |
155 // Elide the middle ones. | 162 // Elide the middle ones. |
156 buffer.write(prefix); | 163 buffer.write(prefix); |
157 buffer.write(_getPrefix(name == null, isLast)); | 164 buffer.write(_getPrefix(name == null, isLast)); |
158 buffer.writeln(log.gray('| (${childNames.length - 6} more...)')); | 165 buffer.writeln(log.gray('| (${childNames.length - 6} more...)')); |
159 | 166 |
160 // Show the last few. | 167 // Show the last few. |
161 drawChild(false, childNames[childNames.length - 3]); | 168 drawChild(false, childNames[childNames.length - 3]); |
162 drawChild(false, childNames[childNames.length - 2]); | 169 drawChild(false, childNames[childNames.length - 2]); |
163 drawChild(true, childNames[childNames.length - 1]); | 170 drawChild(true, childNames[childNames.length - 1]); |
164 } | 171 } |
165 } | 172 } |
OLD | NEW |