Skip to content

Commit

Permalink
feat: show and hide mtime and item count columns with 'M' and 'C' res…
Browse files Browse the repository at this point in the history
…pectively
  • Loading branch information
piotrwach committed Jan 9, 2024
1 parent 1812227 commit 1544e8d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ impl AppState {
}
Char('s') => self.cycle_sorting(&tree_view),
Char('m') => self.cycle_mtime_sorting(&tree_view),
Char('M') => self.toggle_mtime_column(),
Char('c') => self.cycle_count_sorting(&tree_view),
Char('g') => display.byte_vis.cycle(),
Char('C') => self.toggle_count_column(),
Char('g') | Char('S') => display.byte_vis.cycle(),
Char('d') => self.mark_entry(
CursorMode::Advance,
MarkEntryMode::Toggle,
Expand Down
18 changes: 17 additions & 1 deletion src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::interactive::{
app::tree_view::TreeView,
widgets::{GlobPane, HelpPane, MainWindow, MarkMode, MarkPane},
widgets::{Column, GlobPane, HelpPane, MainWindow, MarkMode, MarkPane},
DisplayOptions, EntryDataBundle,
};
use crosstermion::input::Key;
Expand Down Expand Up @@ -135,6 +135,22 @@ impl AppState {
self.entries = tree_view.sorted_entries(self.navigation().view_root, self.sorting);
}

pub fn toggle_mtime_column(&mut self) {
self.toggle_column(Column::MTime);
}

pub fn toggle_count_column(&mut self) {
self.toggle_column(Column::Count);
}

fn toggle_column(&mut self, column: Column) {
if self.show_columns.contains(&column) {
self.show_columns.remove(&column);
} else {
self.show_columns.insert(column);
}
}

pub fn toggle_glob_search(&mut self, window: &mut MainWindow) {
self.focussed = match self.focussed {
Main | Mark | Help => {
Expand Down
5 changes: 5 additions & 0 deletions src/interactive/app/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::collections::HashSet;

use dua::traverse::BackgroundTraversal;

use crate::interactive::widgets::Column;

use super::{navigation::Navigation, EntryDataBundle, SortMode};

#[derive(Default, Copy, Clone, PartialEq)]
Expand All @@ -24,6 +28,7 @@ pub struct AppState {
pub glob_navigation: Option<Navigation>,
pub entries: Vec<EntryDataBundle>,
pub sorting: SortMode,
pub show_columns: HashSet<Column>,
pub message: Option<String>,
pub focussed: FocussedPane,
pub received_events: bool,
Expand Down
19 changes: 11 additions & 8 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use chrono::DateTime;
use dua::traverse::TreeIndex;
use itertools::Itertools;
use std::borrow::{Borrow, Cow};
use std::collections::HashSet;
use std::time::SystemTime;
use tui::{
buffer::Buffer,
Expand All @@ -33,6 +34,7 @@ pub struct EntriesProps<'a> {
pub border_style: Style,
pub is_focussed: bool,
pub sort_mode: SortMode,
pub show_columns: &'a HashSet<Column>,
}

#[derive(Default)]
Expand All @@ -56,6 +58,7 @@ impl Entries {
border_style,
is_focussed,
sort_mode,
show_columns,
} = props.borrow();
let list = &mut self.list;

Expand Down Expand Up @@ -87,7 +90,7 @@ impl Entries {
let percentage_style = percentage_style(fraction, text_style);

let mut columns = Vec::new();
if show_mtime_column(sort_mode) {
if show_mtime_column(sort_mode, show_columns) {
columns.push(mtime_column(
bundle.mtime,
column_style(Column::MTime, *sort_mode, text_style),
Expand All @@ -99,7 +102,7 @@ impl Entries {
column_style(Column::Bytes, *sort_mode, text_style),
));
columns.push(percentage_column(*display, fraction, percentage_style));
if show_count_column(sort_mode) {
if show_count_column(sort_mode, show_columns) {
columns.push(count_column(
bundle.entry_count,
column_style(Column::Count, *sort_mode, text_style),
Expand Down Expand Up @@ -323,8 +326,8 @@ fn bytes_column(display: DisplayOptions, entry_size: u128, style: Style) -> Span
)
}

#[derive(PartialEq)]
enum Column {
#[derive(PartialEq, Eq, Hash)]
pub enum Column {
Bytes,
MTime,
Count,
Expand All @@ -344,18 +347,18 @@ fn column_style(column: Column, sort_mode: SortMode, style: Style) -> Style {
}
}

fn show_mtime_column(sort_mode: &SortMode) -> bool {
fn show_mtime_column(sort_mode: &SortMode, show_columns: &HashSet<Column>) -> bool {
matches!(
sort_mode,
SortMode::MTimeAscending | SortMode::MTimeDescending
)
) || show_columns.contains(&Column::MTime)
}

fn show_count_column(sort_mode: &SortMode) -> bool {
fn show_count_column(sort_mode: &SortMode, show_columns: &HashSet<Column>) -> bool {
matches!(
sort_mode,
SortMode::CountAscending | SortMode::CountDescending
)
) || show_columns.contains(&Column::Count)
}

/// Note that this implementation isn't correct as `width` is the amount of blocks to display,
Expand Down
4 changes: 3 additions & 1 deletion src/interactive/widgets/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ impl HelpPane {
"Toggle sort by modified time descending/ascending.",
None,
);
hotkey("M", "Show/hide modified time.", None);
hotkey("c", "Toggle sort by items descending/ascending.", None);
hotkey("C", "Show/hide item count.", None);
hotkey(
"g",
"g/S",
"Cycle through percentage display and bar options.",
None,
);
Expand Down
1 change: 1 addition & 0 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl MainWindow {
border_style: entries_style,
is_focussed: matches!(state.focussed, Main),
sort_mode: state.sorting,
show_columns: &state.show_columns,
};
self.entries_pane
.render(props, entries_area, terminal.current_buffer_mut());
Expand Down

0 comments on commit 1544e8d

Please sign in to comment.