/// <summary>Display a tree of a directory's children</summary> public static async Task Tree(string[] path, IVarTuple extras, IFdbDatabase db, TextWriter log, CancellationToken ct) { log = log ?? Console.Out; Program.Comment(log, $"# Tree of {String.Join("/", path)}:"); FdbDirectorySubspace root = null; if (path.Length > 0) { root = await db.Directory.TryOpenAsync(db, path, ct : ct); } await TreeDirectoryWalk(root, new List <bool>(), db, log, ct); Program.Comment(log, "# done"); }
/// <summary>Display a tree of a directory's children</summary> public static async Task Tree(string[] path, IFdbTuple extras, IFdbDatabase db, TextWriter log, CancellationToken ct) { if (log == null) { log = Console.Out; } log.WriteLine("# Tree of {0}:", String.Join("/", path)); FdbDirectorySubspace root = null; if (path.Length > 0) { root = await db.Directory.TryOpenAsync(path, cancellationToken : ct); } await TreeDirectoryWalk(root, new List <bool>(), db, log, ct); log.WriteLine("# done"); }
private static async Task TreeDirectoryWalk(FdbDirectorySubspace folder, List <bool> last, IFdbDatabase db, TextWriter stream, CancellationToken ct) { ct.ThrowIfCancellationRequested(); var sb = new StringBuilder(last.Count * 4); if (last.Count > 0) { for (int i = 0; i < last.Count - 1; i++) { sb.Append(last[i] ? " " : "| "); } sb.Append(last[last.Count - 1] ? "`-- " : "|-- "); } IFdbDirectory node; if (folder == null) { stream.WriteLine(sb.ToString() + "<root>"); node = db.Directory; } else { stream.WriteLine($"{sb}{(folder.Layer.ToString() == "partition" ? ("<" + folder.Name + ">") : folder.Name)}{(folder.Layer.IsNullOrEmpty ? string.Empty : (" [" + folder.Layer.ToString() + "]"))}"); node = folder; } var children = await Fdb.Directory.BrowseAsync(db, node, ct); int n = children.Count; foreach (var child in children) { last.Add((n--) == 1); await TreeDirectoryWalk(child.Value, last, db, stream, ct); last.RemoveAt(last.Count - 1); } }