From 9fb3113d788ff746873bd67f6ed508ec1fcf1b02 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 9 Dec 2023 08:10:41 +0100 Subject: refactor * copy when possible * mild function renames * restore previous sorting direction precedence --- src/interactive/app/common.rs | 10 +++---- src/interactive/app/tests/journeys_readonly.rs | 37 +++++++++++++++++++++----- src/interactive/app/tests/utils.rs | 1 - src/interactive/widgets/entries.rs | 8 +++--- src/traverse.rs | 19 +++++++------ 5 files changed, 50 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/interactive/app/common.rs b/src/interactive/app/common.rs index c977f4c..2eb784e 100644 --- a/src/interactive/app/common.rs +++ b/src/interactive/app/common.rs @@ -9,10 +9,10 @@ pub enum SortMode { #[default] SizeDescending, SizeAscending, - MTimeAscending, MTimeDescending, - CountAscending, + MTimeAscending, CountDescending, + CountAscending, } impl SortMode { @@ -21,7 +21,7 @@ impl SortMode { *self = match self { SizeDescending => SizeAscending, SizeAscending => SizeDescending, - _ => SizeAscending, + _ => SizeDescending, } } @@ -30,7 +30,7 @@ impl SortMode { *self = match self { MTimeAscending => MTimeDescending, MTimeDescending => MTimeAscending, - _ => MTimeAscending, + _ => MTimeDescending, } } @@ -39,7 +39,7 @@ impl SortMode { *self = match self { CountAscending => CountDescending, CountDescending => CountAscending, - _ => CountAscending, + _ => CountDescending, } } } diff --git a/src/interactive/app/tests/journeys_readonly.rs b/src/interactive/app/tests/journeys_readonly.rs index 9bb4e27..7a1df0e 100644 --- a/src/interactive/app/tests/journeys_readonly.rs +++ b/src/interactive/app/tests/journeys_readonly.rs @@ -67,35 +67,58 @@ fn simple_user_journey_read_only() -> Result<()> { app.process_events(&mut terminal, into_keys(b"m".iter()))?; assert_eq!( app.state.sorting, - SortMode::MTimeAscending, + SortMode::MTimeDescending, "it sets the sort mode to descending by mtime" ); // when hitting the M key again app.process_events(&mut terminal, into_keys(b"m".iter()))?; assert_eq!( app.state.sorting, - SortMode::MTimeDescending, + SortMode::MTimeAscending, "it sets the sort mode to ascending by mtime" ); - // when hitting the S key - app.process_events(&mut terminal, into_keys(b"s".iter()))?; + // when hitting the C key + app.process_events(&mut terminal, into_keys(b"c".iter()))?; assert_eq!( app.state.sorting, - SortMode::SizeAscending, - "it sets the sort mode to ascending by size" + SortMode::CountDescending, + "it sets the sort mode to descending by count" + ); + // when hitting the C key again + app.process_events(&mut terminal, into_keys(b"c".iter()))?; + assert_eq!( + app.state.sorting, + SortMode::CountAscending, + "it sets the sort mode to ascending by count" ); assert_eq!( node_by_index(&app, app.state.entries[0].index), node_by_name(&app, fixture_str(long_root)), "it recomputes the cached entries" ); - // when hitting the S key again + // when hitting the S key app.process_events(&mut terminal, into_keys(b"s".iter()))?; assert_eq!( app.state.sorting, SortMode::SizeDescending, "it sets the sort mode to descending by size" ); + assert_eq!( + node_by_index(&app, app.state.entries[1].index), + node_by_name(&app, fixture_str(long_root)), + "it recomputes the cached entries" + ); + // when hitting the S key again + app.process_events(&mut terminal, into_keys(b"s".iter()))?; + assert_eq!( + app.state.sorting, + SortMode::SizeAscending, + "it sets the sort mode to ascending by size" + ); + // hit the S key again to get Descending - the rest depends on it + app.process_events(&mut terminal, into_keys(b"s".iter()))?; + assert_eq!(app.state.sorting, SortMode::SizeDescending,); + assert_eq!( node_by_index(&app, app.state.entries[0].index), node_by_name(&app, fixture_str(short_root)), diff --git a/src/interactive/app/tests/utils.rs b/src/interactive/app/tests/utils.rs index 68fc85b..2ded843 100644 --- a/src/interactive/app/tests/utils.rs +++ b/src/interactive/app/tests/utils.rs @@ -13,7 +13,6 @@ use std::{ fs::{copy, create_dir_all, remove_dir, remove_file}, io::ErrorKind, path::{Path, PathBuf}, - time::UNIX_EPOCH, }; use tui::backend::TestBackend; use tui_react::Terminal; diff --git a/src/interactive/widgets/entries.rs b/src/interactive/widgets/entries.rs index 8db9409..2e1062d 100644 --- a/src/interactive/widgets/entries.rs +++ b/src/interactive/widgets/entries.rs @@ -89,7 +89,7 @@ impl Entries { let percentage_style = percentage_style(fraction, text_style); let mut columns = Vec::new(); - if should_show_mtime_column(sort_mode) { + if show_mtime_column(sort_mode) { columns.push(mtime_column( entry_data.mtime, column_style(Column::MTime, *sort_mode, text_style), @@ -101,7 +101,7 @@ impl Entries { column_style(Column::Bytes, *sort_mode, text_style), )); columns.push(percentage_column(*display, fraction, percentage_style)); - if should_show_count_column(sort_mode) { + if show_count_column(sort_mode) { columns.push(count_column( entry_data.entry_count, *is_dir, @@ -330,14 +330,14 @@ fn column_style(column: Column, sort_mode: SortMode, style: Style) -> Style { } } -fn should_show_mtime_column(sort_mode: &SortMode) -> bool { +fn show_mtime_column(sort_mode: &SortMode) -> bool { matches!( sort_mode, SortMode::MTimeAscending | SortMode::MTimeDescending ) } -fn should_show_count_column(sort_mode: &SortMode) -> bool { +fn show_count_column(sort_mode: &SortMode) -> bool { matches!( sort_mode, SortMode::CountAscending | SortMode::CountDescending diff --git a/src/traverse.rs b/src/traverse.rs index a157994..3d54784 100644 --- a/src/traverse.rs +++ b/src/traverse.rs @@ -73,7 +73,7 @@ impl Traversal { input: Vec, mut update: impl FnMut(&mut Traversal) -> Result, ) -> Result> { - #[derive(Default)] + #[derive(Default, Copy, Clone)] struct DirectoryInfo { size: u128, entries_count: u64, @@ -81,13 +81,16 @@ impl Traversal { fn set_directory_info_or_panic( tree: &mut Tree, node_idx: TreeIndex, - current_directory_at_depth: &DirectoryInfo, + DirectoryInfo { + size, + entries_count, + }: DirectoryInfo, ) { let node = tree .node_weight_mut(node_idx) .expect("node for parent index we just retrieved"); - node.size = current_directory_at_depth.size; - node.entry_count = current_directory_at_depth.entries_count; + node.size = size; + node.entry_count = entries_count; } fn parent_or_panic(tree: &mut Tree, parent_node_idx: TreeIndex) -> TreeIndex { tree.neighbors_directed(parent_node_idx, Direction::Incoming) @@ -209,7 +212,7 @@ impl Traversal { set_directory_info_or_panic( &mut t.tree, parent_node_idx, - ¤t_directory_at_depth, + current_directory_at_depth, ); let dir_info = pop_or_panic(&mut directory_info_per_depth_level); @@ -224,7 +227,7 @@ impl Traversal { set_directory_info_or_panic( &mut t.tree, parent_node_idx, - ¤t_directory_at_depth, + current_directory_at_depth, ); } _ => { @@ -265,14 +268,14 @@ impl Traversal { current_directory_at_depth.size += dir_info.size; current_directory_at_depth.entries_count += dir_info.entries_count; - set_directory_info_or_panic(&mut t.tree, parent_node_idx, ¤t_directory_at_depth); + set_directory_info_or_panic(&mut t.tree, parent_node_idx, current_directory_at_depth); parent_node_idx = parent_or_panic(&mut t.tree, parent_node_idx); } let root_size = t.recompute_root_size(); set_directory_info_or_panic( &mut t.tree, t.root_index, - &DirectoryInfo { + DirectoryInfo { size: root_size, entries_count: t.entries_traversed, }, -- cgit v1.2.3