Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flat Layout Sorting Fix #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
context::{column, Context},
context::{column, layout, Context},
disk_usage::file_size::FileSize,
fs::inode::Inode,
progress::{IndicatorHandle, Message},
utils,
};

use count::FileCount;
use error::Error;
use ignore::{WalkBuilder, WalkParallel};
Expand Down Expand Up @@ -55,7 +56,28 @@ impl Tree {
) -> Result<(Self, Context)> {
let mut column_properties = column::Properties::from(&ctx);

let (arena, root_id) = Self::traverse(&ctx, &mut column_properties, indicator)?;
let (mut arena, root_id) = Self::traverse(&ctx, &mut column_properties, indicator)?;

match ctx.layout {
layout::Type::Flat | layout::Type::Iflat => {
let mut nodes: Vec<NodeId> = Vec::new();
for child in root_id.children(&arena).into_iter() {
nodes.push(child)
}
let node_comparator = node::cmp::comparator(&ctx);

nodes.sort_by(|&id_a, &id_b| {
let node_a = arena.get(id_a).unwrap().get();
let node_b = arena.get(id_b).unwrap().get();
node_comparator(node_a, node_b)
});

for node in nodes.iter() {
root_id.append(*node, &mut arena)
}
},
_ => {},
};

ctx.update_column_properties(&column_properties);

Expand All @@ -64,7 +86,6 @@ impl Tree {
}

let tree = Self::new(arena, root_id);

if tree.is_stump() {
return Err(Error::NoMatches);
}
Expand Down Expand Up @@ -163,6 +184,7 @@ impl Tree {
&mut inodes,
column_properties,
ctx,
root_id,
);

if ctx.prune || ctx.pattern.is_some() {
Expand Down Expand Up @@ -197,6 +219,7 @@ impl Tree {
inode_set: &mut HashSet<Inode>,
column_properties: &mut column::Properties,
ctx: &Context,
root_id: NodeId,
) {
let current_node = tree[current_node_id].get_mut();

Expand All @@ -216,6 +239,7 @@ impl Tree {
inode_set,
column_properties,
ctx,
root_id,
);
}

Expand Down Expand Up @@ -249,15 +273,25 @@ impl Tree {

Self::update_column_properties(column_properties, dir, ctx);

children.sort_by(|&id_a, &id_b| {
let node_a = tree[id_a].get();
let node_b = tree[id_b].get();
node_comparator(node_a, node_b)
});
match ctx.layout {
layout::Type::Flat | layout::Type::Iflat => {
// don't bother sorting, flat layouts will need to be resorted downstream
},
_ => {
children.sort_by(|&id_a, &id_b| {
let node_a = tree[id_a].get();
let node_b = tree[id_b].get();
node_comparator(node_a, node_b)
});
},
}

// Append children to current node.
for child_id in children {
current_node_id.append(child_id, tree);
match ctx.layout {
layout::Type::Flat | layout::Type::Iflat => root_id.append(child_id, tree),
_ => current_node_id.append(child_id, tree),
}
}
}

Expand Down
49 changes: 24 additions & 25 deletions tests/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ mod utils;
fn flat() {
assert_eq!(
utils::run_cmd(&["--layout", "flat", "tests/data"]),
indoc!(
"143 B the_yellow_king/cassildas_song.md
143 B the_yellow_king
100 B nylarlathotep.txt
161 B nemesis.txt
83 B necronomicon.txt
446 B lipsum/lipsum.txt
446 B lipsum
308 B dream_cycle/polaris.txt
308 B dream_cycle
1241 B data

3 directories, 6 files"
indoc!("143 B the_yellow_king
308 B dream_cycle/polaris.txt
100 B nylarlathotep.txt
161 B nemesis.txt
83 B necronomicon.txt
446 B lipsum/lipsum.txt
446 B lipsum
308 B dream_cycle
143 B the_yellow_king/cassildas_song.md
1241 B data

3 directories, 6 files"
)
)
}
Expand All @@ -28,18 +27,18 @@ fn flat_human() {
assert_eq!(
utils::run_cmd(&["--layout", "flat", "--human", "tests/data"]),
indoc!(
"143 B the_yellow_king/cassildas_song.md
143 B the_yellow_king
100 B nylarlathotep.txt
161 B nemesis.txt
83 B necronomicon.txt
446 B lipsum/lipsum.txt
446 B lipsum
308 B dream_cycle/polaris.txt
308 B dream_cycle
1.2 KiB data
3 directories, 6 files"
"143 B the_yellow_king
308 B dream_cycle/polaris.txt
100 B nylarlathotep.txt
161 B nemesis.txt
83 B necronomicon.txt
446 B lipsum/lipsum.txt
446 B lipsum
308 B dream_cycle
143 B the_yellow_king/cassildas_song.md
1.2 KiB data

3 directories, 6 files"
)
)
}
Expand Down
Loading