summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-26 10:14:09 -0700
committerBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-26 10:14:09 -0700
commit28697fbc0c98e401f9cc587e1c9c1453066f131a (patch)
tree014db8c91193740a0a741a3023922c9cb65c0347
parentbe3cc7143f4ba9fae23a7ccf4d589c062d2b8eb8 (diff)
inverted flat layout
-rw-r--r--src/context/config/toml/mod.rs1
-rw-r--r--src/context/layout.rs5
-rw-r--r--src/main.rs6
-rw-r--r--src/render/layout/flat_inverted.rs44
-rw-r--r--src/render/layout/mod.rs3
-rw-r--r--src/render/mod.rs3
6 files changed, 59 insertions, 3 deletions
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..92e8826 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 to
+ InvFlat,
}
diff --git a/src/main.rs b/src/main.rs
index 9727aad..f0b6ce6 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::InvFlat => {
+ 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;