OLD | NEW |
(Empty) | |
| 1 library pub.ascii_tree; |
| 2 import 'package:path/path.dart' as path; |
| 3 import 'log.dart' as log; |
| 4 import 'utils.dart'; |
| 5 String fromFiles(List<String> files, {String baseDir, bool showAllChildren}) { |
| 6 var root = {}; |
| 7 for (var file in files) { |
| 8 if (baseDir != null) file = path.relative(file, from: baseDir); |
| 9 var parts = path.split(file); |
| 10 var directory = root; |
| 11 for (var part in path.split(file)) { |
| 12 directory = directory.putIfAbsent(part, () => {}); |
| 13 } |
| 14 } |
| 15 return fromMap(root, showAllChildren: showAllChildren); |
| 16 } |
| 17 String fromMap(Map map, {bool showAllChildren}) { |
| 18 var buffer = new StringBuffer(); |
| 19 _draw(buffer, "", null, map, showAllChildren: showAllChildren); |
| 20 return buffer.toString(); |
| 21 } |
| 22 void _drawLine(StringBuffer buffer, String prefix, bool isLastChild, |
| 23 String name) { |
| 24 buffer.write(prefix); |
| 25 if (name != null) { |
| 26 if (isLastChild) { |
| 27 buffer.write(log.gray("'-- ")); |
| 28 } else { |
| 29 buffer.write(log.gray("|-- ")); |
| 30 } |
| 31 } |
| 32 buffer.writeln(name); |
| 33 } |
| 34 String _getPrefix(bool isRoot, bool isLast) { |
| 35 if (isRoot) return ""; |
| 36 if (isLast) return " "; |
| 37 return log.gray("| "); |
| 38 } |
| 39 void _draw(StringBuffer buffer, String prefix, String name, Map children, |
| 40 {bool showAllChildren, bool isLast: false}) { |
| 41 if (showAllChildren == null) showAllChildren = false; |
| 42 if (name != null) _drawLine(buffer, prefix, isLast, name); |
| 43 var childNames = ordered(children.keys); |
| 44 drawChild(bool isLastChild, String child) { |
| 45 var childPrefix = _getPrefix(name == null, isLast); |
| 46 _draw( |
| 47 buffer, |
| 48 '$prefix$childPrefix', |
| 49 child, |
| 50 children[child], |
| 51 showAllChildren: showAllChildren, |
| 52 isLast: isLastChild); |
| 53 } |
| 54 if (name == null || showAllChildren || childNames.length <= 10) { |
| 55 for (var i = 0; i < childNames.length; i++) { |
| 56 drawChild(i == childNames.length - 1, childNames[i]); |
| 57 } |
| 58 } else { |
| 59 drawChild(false, childNames[0]); |
| 60 drawChild(false, childNames[1]); |
| 61 drawChild(false, childNames[2]); |
| 62 buffer.write(prefix); |
| 63 buffer.write(_getPrefix(name == null, isLast)); |
| 64 buffer.writeln(log.gray('| (${childNames.length - 6} more...)')); |
| 65 drawChild(false, childNames[childNames.length - 3]); |
| 66 drawChild(false, childNames[childNames.length - 2]); |
| 67 drawChild(true, childNames[childNames.length - 1]); |
| 68 } |
| 69 } |
OLD | NEW |