Skip to content

Commit cb3a5df

Browse files
authored
Merge pull request #206 from solidiquis/inverted-flat
inverted flat layout
2 parents be3cc71 + 2e7a36b commit cb3a5df

File tree

7 files changed

+62
-4
lines changed

7 files changed

+62
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Options:
227227
- regular: Outputs the tree with the root node at the bottom of the output
228228
- inverted: Outputs the tree with the root node at the top of the output
229229
- flat: Outputs a flat layout using paths rather than an ASCII tree
230+
- iflat: Outputs an inverted flat layout with the root at the top of the output
230231
231232
-., --hidden
232233
Show hidden files
@@ -525,7 +526,7 @@ Additionally, the word and line-count of directories are the summation of all of
525526

526527
### Layouts
527528

528-
`erdtree` comes with three layouts:
529+
`erdtree` comes with four layouts:
529530

530531
```
531532
-y, --layout <LAYOUT>
@@ -537,6 +538,7 @@ Additionally, the word and line-count of directories are the summation of all of
537538
- regular: Outputs the tree with the root node at the bottom of the output
538539
- inverted: Outputs the tree with the root node at the top of the output
539540
- flat: Outputs a flat layout using paths rather than an ASCII tree
541+
- iflat: Outputs an inverted flat layout with the root at the top of the output
540542
```
541543

542544
* The `inverted` layout a more traditional `tree`-like layout where the root node is at the very top of the output.

src/context/config/toml/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ mod unix {
199199
mod windows {
200200
use super::super::{ERDTREE_CONFIG_TOML, ERDTREE_DIR};
201201
use config::{Config, File};
202-
use std::{env, path::PathBuf};
203202

204203
/// Try to read in config from the following location:
205204
/// - `%APPDATA%\erdtree\.erdtree.toml`

src/context/layout.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub enum Type {
1010
/// Outputs the tree with the root node at the top of the output
1111
Inverted,
1212

13-
/// Outputs a flat layout using paths rather than an ASCII tree.
13+
/// Outputs a flat layout using paths rather than an ASCII tree
1414
Flat,
15+
16+
/// Outputs an inverted flat layout with the root at the top of the output
17+
Iflat,
1518
}

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use clap::CommandFactory;
2323
use context::{layout, Context};
2424
use progress::Message;
25-
use render::{Engine, Flat, Inverted, Regular};
25+
use render::{Engine, Flat, FlatInverted, Inverted, Regular};
2626
use std::{error::Error, io::stdout, process::ExitCode};
2727
use tree::Tree;
2828

@@ -97,6 +97,10 @@ fn run() -> Result<(), Box<dyn Error>> {
9797
let render = Engine::<Flat>::new(tree, ctx);
9898
format!("{render}")
9999
}
100+
layout::Type::Iflat => {
101+
let render = Engine::<FlatInverted>::new(tree, ctx);
102+
format!("{render}")
103+
}
100104
layout::Type::Inverted => {
101105
let render = Engine::<Inverted>::new(tree, ctx);
102106
format!("{render}")

src/render/layout/flat_inverted.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{
2+
render::{
3+
grid::{self, Row},
4+
Engine, FlatInverted,
5+
},
6+
tree::{count::FileCount, Tree},
7+
};
8+
use indextree::NodeEdge;
9+
use std::fmt::{self, Display};
10+
11+
impl Display for Engine<FlatInverted> {
12+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13+
let ctx = self.context();
14+
let tree = self.tree();
15+
let arena = tree.arena();
16+
let root_id = tree.root_id();
17+
let max_depth = ctx.level();
18+
let mut file_count_data = vec![];
19+
20+
for edge in root_id.traverse(arena) {
21+
let node_id = match edge {
22+
NodeEdge::Start(id) => id,
23+
NodeEdge::End(_) => continue,
24+
};
25+
file_count_data.push(Tree::compute_file_count(node_id, arena));
26+
27+
let node = arena[node_id].get();
28+
29+
if node.depth() > max_depth {
30+
continue;
31+
}
32+
33+
let row = Row::<grid::Flat>::new(node, ctx, None);
34+
35+
writeln!(f, "{row}")?;
36+
}
37+
38+
if !file_count_data.is_empty() {
39+
write!(f, "\n{}", FileCount::from(file_count_data))?;
40+
}
41+
42+
Ok(())
43+
}
44+
}

src/render/layout/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ pub mod regular;
44
/// See [`super::Flat`]
55
pub mod flat;
66

7+
/// See [`super::FlatInverted`]
8+
pub mod flat_inverted;
9+
710
/// See [`super::Inverted`]
811
pub mod inverted;

src/render/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct Engine<T> {
2626
/// The flat output that is similar to `du`, without the ASCII tree.
2727
pub struct Flat;
2828

29+
/// Same as [`Flat`] but the root is at the top of the output.
30+
pub struct FlatInverted;
31+
2932
/// The tree output with the root directory at the bottom of the output.
3033
pub struct Regular;
3134

0 commit comments

Comments
 (0)