Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(683)

Side by Side Diff: utils/pub/directory_tree.dart

Issue 14297021: Move pub into sdk/lib/_internal. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Disallow package: imports of pub. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/pub/command_version.dart ('k') | utils/pub/entrypoint.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 'package:pathos/path.dart' as path;
9
10 import 'log.dart' as log;
11
12 /// Draws a directory tree for the given list of files. Given a list of files
13 /// like:
14 ///
15 /// TODO
16 /// example/console_example.dart
17 /// example/main.dart
18 /// example/web copy/web_example.dart
19 /// test/absolute_test.dart
20 /// test/basename_test.dart
21 /// test/dirname_test.dart
22 /// test/extension_test.dart
23 /// test/is_absolute_test.dart
24 /// test/is_relative_test.dart
25 /// test/join_test.dart
26 /// test/normalize_test.dart
27 /// test/relative_test.dart
28 /// test/split_test.dart
29 /// .gitignore
30 /// README.md
31 /// lib/path.dart
32 /// pubspec.yaml
33 /// test/all_test.dart
34 /// test/path_posix_test.dart
35 /// test/path_windows_test.dart
36 ///
37 /// this will render:
38 ///
39 /// |-- .gitignore
40 /// |-- README.md
41 /// |-- TODO
42 /// |-- example
43 /// | |-- console_example.dart
44 /// | |-- main.dart
45 /// | '-- web copy
46 /// | '-- web_example.dart
47 /// |-- lib
48 /// | '-- path.dart
49 /// |-- pubspec.yaml
50 /// '-- test
51 /// |-- absolute_test.dart
52 /// |-- all_test.dart
53 /// |-- basename_test.dart
54 /// | (7 more...)
55 /// |-- path_windows_test.dart
56 /// |-- relative_test.dart
57 /// '-- split_test.dart
58 ///
59 String generateTree(List<String> files) {
60 // Parse out the files into a tree of nested maps.
61 var root = {};
62 for (var file in files) {
63 var parts = path.split(file);
64 var directory = root;
65 for (var part in path.split(file)) {
66 directory = directory.putIfAbsent(part, () => {});
67 }
68 }
69
70 // Walk the map recursively and render to a string.
71 var buffer = new StringBuffer();
72 _draw(buffer, '', false, null, root);
73 return buffer.toString();
74 }
75
76 void _drawLine(StringBuffer buffer, String prefix, bool isLastChild,
77 String name) {
78 // Print lines.
79 buffer.write(prefix);
80 if (name != null) {
81 if (isLastChild) {
82 buffer.write("'-- ");
83 } else {
84 buffer.write("|-- ");
85 }
86 }
87
88 // Print name.
89 buffer.writeln(name);
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 (name == null || 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.write(prefix);
125 buffer.write(_getPrefix(name == null, isLast));
126 buffer.writeln('| (${childNames.length - 6} more...)');
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 }
OLDNEW
« no previous file with comments | « utils/pub/command_version.dart ('k') | utils/pub/entrypoint.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698