OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// A simple library for rendering a list of files as a directory tree. |
| 6 library directory_tree; |
| 7 |
| 8 import 'log.dart' as log; |
| 9 import 'path.dart' as path; |
| 10 |
| 11 /// Draws a directory tree for the given list of files. Given a list of files |
| 12 /// like: |
| 13 /// |
| 14 /// TODO |
| 15 /// example/console_example.dart |
| 16 /// example/main.dart |
| 17 /// example/web copy/web_example.dart |
| 18 /// test/absolute_test.dart |
| 19 /// test/basename_test.dart |
| 20 /// test/dirname_test.dart |
| 21 /// test/extension_test.dart |
| 22 /// test/is_absolute_test.dart |
| 23 /// test/is_relative_test.dart |
| 24 /// test/join_test.dart |
| 25 /// test/normalize_test.dart |
| 26 /// test/relative_test.dart |
| 27 /// test/split_test.dart |
| 28 /// .gitignore |
| 29 /// README.md |
| 30 /// lib/path.dart |
| 31 /// pubspec.yaml |
| 32 /// test/all_test.dart |
| 33 /// test/path_posix_test.dart |
| 34 /// test/path_windows_test.dart |
| 35 /// |
| 36 /// this will render: |
| 37 /// |
| 38 /// |-- .gitignore |
| 39 /// |-- README.md |
| 40 /// |-- TODO |
| 41 /// |-- example |
| 42 /// | |-- console_example.dart |
| 43 /// | |-- main.dart |
| 44 /// | '-- web copy |
| 45 /// | '-- web_example.dart |
| 46 /// |-- lib |
| 47 /// | '-- path.dart |
| 48 /// |-- pubspec.yaml |
| 49 /// '-- test |
| 50 /// |-- absolute_test.dart |
| 51 /// |-- all_test.dart |
| 52 /// |-- basename_test.dart |
| 53 /// | (7 more...) |
| 54 /// |-- path_windows_test.dart |
| 55 /// |-- relative_test.dart |
| 56 /// '-- split_test.dart |
| 57 /// |
| 58 String generateTree(List<String> files) { |
| 59 // Parse out the files into a tree of nested maps. |
| 60 var root = {}; |
| 61 for (var file in files) { |
| 62 var parts = path.split(file); |
| 63 var directory = root; |
| 64 for (var part in path.split(file)) { |
| 65 directory = directory.putIfAbsent(part, () => {}); |
| 66 } |
| 67 } |
| 68 |
| 69 // Walk the map recursively and render to a string. |
| 70 var buffer = new StringBuffer(); |
| 71 _draw(buffer, '', false, null, root); |
| 72 return buffer.toString(); |
| 73 } |
| 74 |
| 75 void _drawLine(StringBuffer buffer, String prefix, bool isLastChild, |
| 76 String name) { |
| 77 // Print lines. |
| 78 buffer.add(prefix); |
| 79 if (name != null) { |
| 80 if (isLastChild) { |
| 81 buffer.add("'-- "); |
| 82 } else { |
| 83 buffer.add("|-- "); |
| 84 } |
| 85 } |
| 86 |
| 87 // Print name. |
| 88 buffer.add(name); |
| 89 buffer.add('\n'); |
| 90 } |
| 91 |
| 92 String _getPrefix(bool isRoot, bool isLast) { |
| 93 if (isRoot) return ""; |
| 94 if (isLast) return " "; |
| 95 return "| "; |
| 96 } |
| 97 |
| 98 void _draw(StringBuffer buffer, String prefix, bool isLast, |
| 99 String name, Map children) { |
| 100 // Don't draw a line for the root node. |
| 101 if (name != null) _drawLine(buffer, prefix, isLast, name); |
| 102 |
| 103 // Recurse to the children. |
| 104 var childNames = new List.from(children.keys); |
| 105 childNames.sort(); |
| 106 |
| 107 _drawChild(bool isLastChild, String child) { |
| 108 var childPrefix = _getPrefix(name == null, isLast); |
| 109 _draw(buffer, '$prefix$childPrefix', isLastChild, child, children[child]); |
| 110 } |
| 111 |
| 112 if (childNames.length <= 10) { |
| 113 // Not too many, so show all the children. |
| 114 for (var i = 0; i < childNames.length; i++) { |
| 115 _drawChild(i == childNames.length - 1, childNames[i]); |
| 116 } |
| 117 } else { |
| 118 // Show the first few. |
| 119 _drawChild(false, childNames[0]); |
| 120 _drawChild(false, childNames[1]); |
| 121 _drawChild(false, childNames[2]); |
| 122 |
| 123 // Elide the middle ones. |
| 124 buffer.add(prefix); |
| 125 buffer.add(_getPrefix(name == null, isLast)); |
| 126 buffer.add('| (${childNames.length - 6} more...)\n'); |
| 127 |
| 128 // Show the last few. |
| 129 _drawChild(false, childNames[childNames.length - 3]); |
| 130 _drawChild(false, childNames[childNames.length - 2]); |
| 131 _drawChild(true, childNames[childNames.length - 1]); |
| 132 } |
| 133 } |
OLD | NEW |