diff options
author | Benji Nguyen <45523555+solidiquis@users.noreply.github.com> | 2023-06-26 10:20:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-26 10:20:44 -0700 |
commit | cb3a5df076c1746206c783657f1d5a67185edbdd (patch) | |
tree | c0ba86b3bc1a068a7006321563ac87b5168f6533 | |
parent | be3cc7143f4ba9fae23a7ccf4d589c062d2b8eb8 (diff) | |
parent | 2e7a36b12f75211de48a37f1773372dab6d3e5a5 (diff) |
Merge pull request #206 from solidiquis/inverted-flat
inverted flat layout
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/context/config/toml/mod.rs | 1 | ||||
-rw-r--r-- | src/context/layout.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/render/layout/flat_inverted.rs | 44 | ||||
-rw-r--r-- | src/render/layout/mod.rs | 3 | ||||
-rw-r--r-- | src/render/mod.rs | 3 |
7 files changed, 62 insertions, 4 deletions
@@ -227,6 +227,7 @@ Options: - regular: Outputs the tree with the root node at the bottom of the output - inverted: Outputs the tree with the root node at the top of the output - flat: Outputs a flat layout using paths rather than an ASCII tree + - iflat: Outputs an inverted flat layout with the root at the top of the output -., --hidden Show hidden files @@ -525,7 +526,7 @@ Additionally, the word and line-count of directories are the summation of all of ### Layouts -`erdtree` comes with three layouts: +`erdtree` comes with four layouts: ``` -y, --layout <LAYOUT> @@ -537,6 +538,7 @@ Additionally, the word and line-count of directories are the summation of all of - regular: Outputs the tree with the root node at the bottom of the output - inverted: Outputs the tree with the root node at the top of the output - flat: Outputs a flat layout using paths rather than an ASCII tree + - iflat: Outputs an inverted flat layout with the root at the top of the output ``` * The `inverted` layout a more traditional `tree`-like layout where the root node is at the very top of the output. diff --git a/src/context/config/toml/mod.rs b/src/context/config/toml/mod.rs index f27f6dc..6660ebe 100644 --- a/src/context/config/toml/mod.rs +++ b/src/context/config/toml/mod.rs @@ -199,7 +199,6 @@ mod unix { mod windows { use super::super::{ERDTREE_CONFIG_TOML, ERDTREE_DIR}; use config::{Config, File}; - use std::{env, path::PathBuf}; /// Try to read in config from the following location: /// - `%APPDATA%\erdtree\.erdtree.toml` diff --git a/src/context/layout.rs b/src/context/layout.rs index 4c7557b..7e88bcc 100644 --- a/src/context/layout.rs +++ b/src/context/layout.rs @@ -10,6 +10,9 @@ pub enum Type { /// Outputs the tree with the root node at the top of the output Inverted, - /// Outputs a flat layout using paths rather than an ASCII tree. + /// Outputs a flat layout using paths rather than an ASCII tree Flat, + + /// Outputs an inverted flat layout with the root at the top of the output + Iflat, } diff --git a/src/main.rs b/src/main.rs index 9727aad..b999124 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ use clap::CommandFactory; use context::{layout, Context}; use progress::Message; -use render::{Engine, Flat, Inverted, Regular}; +use render::{Engine, Flat, FlatInverted, Inverted, Regular}; use std::{error::Error, io::stdout, process::ExitCode}; use tree::Tree; @@ -97,6 +97,10 @@ fn run() -> Result<(), Box<dyn Error>> { let render = Engine::<Flat>::new(tree, ctx); format!("{render}") } + layout::Type::Iflat => { + let render = Engine::<FlatInverted>::new(tree, ctx); + format!("{render}") + } layout::Type::Inverted => { let render = Engine::<Inverted>::new(tree, ctx); format!("{render}") diff --git a/src/render/layout/flat_inverted.rs b/src/render/layout/flat_inverted.rs new file mode 100644 index 0000000..4cff075 --- /dev/null +++ b/src/render/layout/flat_inverted.rs @@ -0,0 +1,44 @@ +use crate::{ + render::{ + grid::{self, Row}, + Engine, FlatInverted, + }, + tree::{count::FileCount, Tree}, +}; +use indextree::NodeEdge; +use std::fmt::{self, Display}; + +impl Display for Engine<FlatInverted> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let ctx = self.context(); + let tree = self.tree(); + let arena = tree.arena(); + let root_id = tree.root_id(); + let max_depth = ctx.level(); + let mut file_count_data = vec![]; + + for edge in root_id.traverse(arena) { + let node_id = match edge { + NodeEdge::Start(id) => id, + NodeEdge::End(_) => continue, + }; + file_count_data.push(Tree::compute_file_count(node_id, arena)); + + let node = arena[node_id].get(); + + if node.depth() > max_depth { + continue; + } + + let row = Row::<grid::Flat>::new(node, ctx, None); + + writeln!(f, "{row}")?; + } + + if !file_count_data.is_empty() { + write!(f, "\n{}", FileCount::from(file_count_data))?; + } + + Ok(()) + } +} diff --git a/src/render/layout/mod.rs b/src/render/layout/mod.rs index 29b727f..4ef0adc 100644 --- a/src/render/layout/mod.rs +++ b/src/render/layout/mod.rs @@ -4,5 +4,8 @@ pub mod regular; /// See [`super::Flat`] pub mod flat; +/// See [`super::FlatInverted`] +pub mod flat_inverted; + /// See [`super::Inverted`] pub mod inverted; diff --git a/src/render/mod.rs b/src/render/mod.rs index d70a5ad..b06440e 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -26,6 +26,9 @@ pub struct Engine<T> { /// The flat output that is similar to `du`, without the ASCII tree. pub struct Flat; +/// Same as [`Flat`] but the root is at the top of the output. +pub struct FlatInverted; + /// The tree output with the root directory at the bottom of the output. pub struct Regular; |