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

Unified Diff: sdk/lib/_internal/pub/lib/src/ascii_tree.dart

Issue 217343004: Add a "pub dependencies" command. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/command.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/lib/src/ascii_tree.dart
diff --git a/sdk/lib/_internal/pub/lib/src/directory_tree.dart b/sdk/lib/_internal/pub/lib/src/ascii_tree.dart
similarity index 57%
rename from sdk/lib/_internal/pub/lib/src/directory_tree.dart
rename to sdk/lib/_internal/pub/lib/src/ascii_tree.dart
index b0d07109b622751316d2ffc306f8c3717460a54d..7ff711cf9b0cd091fe17eb7bb9810184be2b844e 100644
--- a/sdk/lib/_internal/pub/lib/src/directory_tree.dart
+++ b/sdk/lib/_internal/pub/lib/src/ascii_tree.dart
@@ -2,15 +2,15 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-/// A simple library for rendering a list of files as a directory tree.
-library pub.directory_tree;
+/// A simple library for rendering tree-like structures in ASCII.
+library pub.ascii_tree;
import 'package:path/path.dart' as path;
+import 'log.dart' as log;
import 'utils.dart';
-/// Draws a directory tree for the given list of files. Given a list of files
-/// like:
+/// Draws a tree for the given list of files. Given files like:
///
/// TODO
/// example/console_example.dart
@@ -34,7 +34,7 @@ import 'utils.dart';
/// test/path_posix_test.dart
/// test/path_windows_test.dart
///
-/// this will render:
+/// this renders:
///
/// |-- .gitignore
/// |-- README.md
@@ -57,7 +57,11 @@ import 'utils.dart';
/// '-- split_test.dart
///
/// If [baseDir] is passed, it will be used as the root of the tree.
-String generateTree(List<String> files, {String baseDir}) {
+///
+/// If [showAllChildren] is `false`, then directories with more than ten items
+/// will have their contents truncated. Defaults to `false`.
+String fromFiles(List<String> files, {String baseDir,
+ bool showAllChildren}) {
nweiz 2014/04/02 03:11:12 Indent +2. I still prefer always using default va
Bob Nystrom 2014/04/02 19:18:37 I don't want to repeat the default here and in _dr
// Parse out the files into a tree of nested maps.
var root = {};
for (var file in files) {
@@ -70,8 +74,36 @@ String generateTree(List<String> files, {String baseDir}) {
}
// Walk the map recursively and render to a string.
+ return fromMap(root, showAllChildren: showAllChildren);
+}
+
+/// Draws a tree from a nested map. Given a map like:
+///
+/// {
+/// "analyzer": {
+/// "args": {
+/// "collection": ""
+/// },
+/// "logging": {}
+/// },
+/// "barback": {}
+/// }
+///
+/// this renders:
+///
+/// analyzer
+/// |-- args
+/// | '-- collection
+/// '---logging
+/// barback
+///
+/// Items with no children should have an empty map as the value.
+///
+/// If [showAllChildren] is `false`, then directories with more than ten items
+/// will have their contents truncated. Defaults to `false`.
+String fromMap(Map map, {bool showAllChildren}) {
var buffer = new StringBuffer();
- _draw(buffer, '', false, null, root);
+ _draw(buffer, "", null, map, showAllChildren: showAllChildren);
return buffer.toString();
}
@@ -81,9 +113,9 @@ void _drawLine(StringBuffer buffer, String prefix, bool isLastChild,
buffer.write(prefix);
if (name != null) {
if (isLastChild) {
- buffer.write("'-- ");
+ buffer.write(log.gray("'-- "));
} else {
- buffer.write("|-- ");
+ buffer.write(log.gray("|-- "));
}
}
@@ -94,41 +126,44 @@ void _drawLine(StringBuffer buffer, String prefix, bool isLastChild,
String _getPrefix(bool isRoot, bool isLast) {
if (isRoot) return "";
if (isLast) return " ";
- return "| ";
+ return log.gray("| ");
}
-void _draw(StringBuffer buffer, String prefix, bool isLast,
- String name, Map children) {
+void _draw(StringBuffer buffer, String prefix, String name, Map children,
+ {bool showAllChildren, bool isLast: false}) {
+ if (showAllChildren == null) showAllChildren = false;
+
// Don't draw a line for the root node.
if (name != null) _drawLine(buffer, prefix, isLast, name);
// Recurse to the children.
var childNames = ordered(children.keys);
- _drawChild(bool isLastChild, String child) {
+ drawChild(bool isLastChild, String child) {
var childPrefix = _getPrefix(name == null, isLast);
- _draw(buffer, '$prefix$childPrefix', isLastChild, child, children[child]);
+ _draw(buffer, '$prefix$childPrefix', child, children[child],
+ showAllChildren: showAllChildren, isLast: isLastChild);
}
- if (name == null || childNames.length <= 10) {
+ if (name == null || showAllChildren || childNames.length <= 10) {
// Not too many, so show all the children.
for (var i = 0; i < childNames.length; i++) {
- _drawChild(i == childNames.length - 1, childNames[i]);
+ drawChild(i == childNames.length - 1, childNames[i]);
}
} else {
// Show the first few.
- _drawChild(false, childNames[0]);
- _drawChild(false, childNames[1]);
- _drawChild(false, childNames[2]);
+ drawChild(false, childNames[0]);
+ drawChild(false, childNames[1]);
+ drawChild(false, childNames[2]);
// Elide the middle ones.
buffer.write(prefix);
buffer.write(_getPrefix(name == null, isLast));
- buffer.writeln('| (${childNames.length - 6} more...)');
+ buffer.writeln(log.gray('| (${childNames.length - 6} more...)'));
// Show the last few.
- _drawChild(false, childNames[childNames.length - 3]);
- _drawChild(false, childNames[childNames.length - 2]);
- _drawChild(true, childNames[childNames.length - 1]);
+ drawChild(false, childNames[childNames.length - 3]);
+ drawChild(false, childNames[childNames.length - 2]);
+ drawChild(true, childNames[childNames.length - 1]);
}
}
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/command.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698