Skip to content

Commit 73224e6

Browse files
committed
feat: allow sorting by name in interactive mode
1 parent 14a276e commit 73224e6

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

src/interactive/app/common.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum SortMode {
1515
MTimeAscending,
1616
CountDescending,
1717
CountAscending,
18+
NameDescending,
19+
NameAscending,
1820
}
1921

2022
impl SortMode {
@@ -44,6 +46,15 @@ impl SortMode {
4446
_ => CountDescending,
4547
}
4648
}
49+
50+
pub fn toggle_name(&mut self) {
51+
use SortMode::*;
52+
*self = match self {
53+
NameAscending => NameDescending,
54+
NameDescending => NameAscending,
55+
_ => NameAscending,
56+
}
57+
}
4758
}
4859

4960
pub struct EntryDataBundle {
@@ -86,6 +97,15 @@ pub fn sorted_entries(
8697
.cmp(&r.entry_count)
8798
.then_with(|| l.name.cmp(&r.name))
8899
}
100+
fn cmp_name(l: &EntryDataBundle, r: &EntryDataBundle) -> Ordering {
101+
if l.is_dir && !r.is_dir {
102+
Ordering::Less
103+
} else if !l.is_dir && r.is_dir {
104+
Ordering::Greater
105+
} else {
106+
l.name.cmp(&r.name)
107+
}
108+
}
89109
tree.neighbors_directed(node_idx, Direction::Outgoing)
90110
.filter_map(|idx| {
91111
tree.node_weight(idx).map(|entry| {
@@ -121,6 +141,8 @@ pub fn sorted_entries(
121141
MTimeDescending => r.mtime.cmp(&l.mtime),
122142
CountAscending => cmp_count(l, r),
123143
CountDescending => cmp_count(l, r).reverse(),
144+
NameAscending => cmp_name(l, r),
145+
NameDescending => cmp_name(l, r).reverse(),
124146
})
125147
.collect()
126148
}

src/interactive/app/eventloop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl AppState {
332332
Char('M') => self.toggle_mtime_column(),
333333
Char('c') => self.cycle_count_sorting(&tree_view),
334334
Char('C') => self.toggle_count_column(),
335+
Char('n') => self.cycle_name_sorting(&tree_view),
335336
Char('g') | Char('S') => display.byte_vis.cycle(),
336337
Char('d') => self.mark_entry(
337338
CursorMode::Advance,

src/interactive/app/handlers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ impl AppState {
146146
);
147147
}
148148

149+
pub fn cycle_name_sorting(&mut self, tree_view: &TreeView<'_>) {
150+
self.sorting.toggle_name();
151+
self.entries = tree_view.sorted_entries(
152+
self.navigation().view_root,
153+
self.sorting,
154+
self.entry_check(),
155+
);
156+
}
157+
149158
pub fn toggle_mtime_column(&mut self) {
150159
self.toggle_column(Column::MTime);
151160
}

src/interactive/app/tests/journeys_readonly.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ fn simple_user_journey_read_only() -> Result<()> {
6868

6969
// SORTING
7070
{
71+
// when hitting the N key
72+
app.process_events(&mut terminal, into_codes("n"))?;
73+
assert_eq!(
74+
app.state.sorting,
75+
SortMode::NameAscending,
76+
"it sets the sort mode to ascending by name"
77+
);
78+
// when hitting the N key again
79+
app.process_events(&mut terminal, into_codes("n"))?;
80+
assert_eq!(
81+
app.state.sorting,
82+
SortMode::NameDescending,
83+
"it sets the sort mode to descending by name"
84+
);
7185
// when hitting the M key
7286
app.process_events(&mut terminal, into_codes("m"))?;
7387
assert_eq!(

src/interactive/widgets/footer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ impl Footer {
4545
SortMode::MTimeDescending => "modified descending",
4646
SortMode::CountAscending => "items ascending",
4747
SortMode::CountDescending => "items descending",
48+
SortMode::NameAscending => "name ascending",
49+
SortMode::NameDescending => "name descending",
4850
},
4951
format.display(*total_bytes),
5052
entries_traversed,

src/interactive/widgets/help.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl HelpPane {
152152
hotkey("M", "Show/hide modified time.", None);
153153
hotkey("c", "Toggle sort by entries descending/ascending.", None);
154154
hotkey("C", "Show/hide entry count.", None);
155+
hotkey("n", "Toggle sort by name ascending/descending.", None);
155156
hotkey(
156157
"g/S",
157158
"Cycle through percentage display and bar options.",

0 commit comments

Comments
 (0)