Chromium Code Reviews| 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 | |
|
nweiz
2012/12/12 21:21:21
This is really nice-looking.
Bob Nystrom
2012/12/12 21:45:36
Thanks!
| |
| 57 /// | |
| 58 String generateTree(List<String> files) { | |
|
nweiz
2012/12/12 21:21:21
I feel like this should have the same signature as
Bob Nystrom
2012/12/12 21:45:36
So passing in the root directory? Otherwise, how d
nweiz
2012/12/12 21:59:51
Yes. createTarGz takes a list of files and a base
| |
| 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, '', null, 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 (isLastChild == true) { | |
|
nweiz
2012/12/12 21:21:21
if (isLastChild)
Bob Nystrom
2012/12/12 21:45:36
It's nullable. But fixed.
nweiz
2012/12/12 21:59:51
In practice, I don't think null is ever passed in
Bob Nystrom
2012/12/12 22:14:32
The name != null check is new. That's how I avoid
nweiz
2012/12/12 22:19:33
I meant the one in _draw. _drawLine is never calle
| |
| 80 buffer.add("'-- "); | |
| 81 } else if (isLastChild == false) { | |
|
nweiz
2012/12/12 21:21:21
if (!isLastChild)
Bob Nystrom
2012/12/12 21:45:36
Done.
| |
| 82 buffer.add("|-- "); | |
| 83 } | |
| 84 | |
| 85 // Print name. | |
| 86 buffer.add(name); | |
| 87 buffer.add('\n'); | |
| 88 } | |
| 89 | |
| 90 String _getPrefix(bool isLast) { | |
|
nweiz
2012/12/12 21:21:21
Tri-state booleans are awful. Just pass in two sep
Bob Nystrom
2012/12/12 21:45:36
Done.
| |
| 91 if (isLast == true) { | |
| 92 return " "; | |
| 93 } else if (isLast == false) { | |
| 94 return "| "; | |
| 95 } else { | |
| 96 return ""; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void _draw(StringBuffer buffer, String prefix, bool isLast, | |
| 101 String name, Map children) { | |
| 102 // Don't draw a line for the root node. | |
| 103 if (name != null) _drawLine(buffer, prefix, isLast, name); | |
| 104 | |
| 105 // Recurse to the children. | |
| 106 var childNames = new List.from(children.keys); | |
| 107 childNames.sort(); | |
| 108 | |
| 109 _drawChild(bool isLastChild, String child) { | |
| 110 var childPrefix = _getPrefix(isLast); | |
| 111 _draw(buffer, '$prefix$childPrefix', isLastChild, child, children[child]); | |
| 112 } | |
| 113 | |
| 114 if (childNames.length <= 10) { | |
| 115 // Not too many, so show all the children. | |
| 116 for (var i = 0; i < childNames.length; i++) { | |
| 117 _drawChild(i == childNames.length - 1, childNames[i]); | |
| 118 } | |
| 119 } else { | |
| 120 // Show the first few. | |
| 121 _drawChild(false, childNames[0]); | |
| 122 _drawChild(false, childNames[1]); | |
| 123 _drawChild(false, childNames[2]); | |
| 124 | |
| 125 // Elide the middle ones. | |
| 126 buffer.add(prefix); | |
| 127 buffer.add(_getPrefix(isLast)); | |
| 128 buffer.add('| (${childNames.length - 6} more...)\n'); | |
| 129 | |
| 130 // Show the last few. | |
| 131 _drawChild(false, childNames[childNames.length - 3]); | |
| 132 _drawChild(false, childNames[childNames.length - 2]); | |
| 133 _drawChild(true, childNames[childNames.length - 1]); | |
| 134 } | |
| 135 } | |
| OLD | NEW |