diff options
author | Piotr Wach <pwach@bloomberg.net> | 2023-12-08 13:17:51 +0000 |
---|---|---|
committer | Piotr Wach <pwach@bloomberg.net> | 2023-12-08 13:37:03 +0000 |
commit | b8ad16b493c29c56d94f6ec01a9dc790687a1bdb (patch) | |
tree | 21720848665254c5b0c1de6e99042f1fd895bbfc | |
parent | 1a46d8f4ab64c78c6ff6495225872f28e58f309d (diff) |
Fix visual changes
Fixed clippy warnings & code review comments
-rw-r--r-- | src/interactive/app/eventloop.rs | 8 | ||||
-rw-r--r-- | src/interactive/app/handlers.rs | 4 | ||||
-rw-r--r-- | src/interactive/widgets/entries.rs | 98 |
3 files changed, 65 insertions, 45 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 478a5b2..a8108bc 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -257,7 +257,7 @@ impl TerminalApp { Some(s) => { s.entries = sorted_entries(&traversal.tree, s.root, s.sorting); if !received_events { - s.selected = s.entries.get(0).map(|b| b.index); + s.selected = s.entries.first().map(|b| b.index); } s } @@ -269,7 +269,7 @@ impl TerminalApp { AppState { root: traversal.root_index, sorting, - selected: entries.get(0).map(|b| b.index), + selected: entries.first().map(|b| b.index), entries, is_scanning: true, ..Default::default() @@ -316,9 +316,9 @@ impl TerminalApp { s.is_scanning = false; s.entries = sorted_entries(&traversal.tree, s.root, s.sorting); s.selected = if received_events { - s.selected.or_else(|| s.entries.get(0).map(|b| b.index)) + s.selected.or_else(|| s.entries.first().map(|b| b.index)) } else { - s.entries.get(0).map(|b| b.index) + s.entries.first().map(|b| b.index) }; s }, diff --git a/src/interactive/app/handlers.rs b/src/interactive/app/handlers.rs index 4cc1741..4948c95 100644 --- a/src/interactive/app/handlers.rs +++ b/src/interactive/app/handlers.rs @@ -84,7 +84,7 @@ impl AppState { .bookmarks .get(&parent_idx) .copied() - .or_else(|| self.entries.get(0).map(|b| b.index)); + .or_else(|| self.entries.first().map(|b| b.index)); } None => self.message = Some("Top level reached".into()), } @@ -321,7 +321,7 @@ impl AppState { .and_then(|selected| self.entries.iter().find(|e| e.index == selected)) .is_none() { - self.selected = self.entries.get(0).map(|e| e.index); + self.selected = self.entries.first().map(|e| e.index); } self.recompute_sizes_recursively(parent_idx, traversal); entries_deleted diff --git a/src/interactive/widgets/entries.rs b/src/interactive/widgets/entries.rs index 33cad83..d4803b6 100644 --- a/src/interactive/widgets/entries.rs +++ b/src/interactive/widgets/entries.rs @@ -67,7 +67,7 @@ impl Entries { let total: u128 = entries.iter().map(|b| b.data.size).sum(); let title = title(¤t_path(tree, root), entries.len()); let title_block = title_block(&title, border_style); - let entry_in_view = entry_in_view(selected, entries); + let entry_in_view = entry_in_view(selected.as_ref(), entries); let props = ListProps { block: Some(title_block), @@ -76,45 +76,52 @@ impl Entries { let lines = entries.iter().map( |EntryDataBundle { index: node_idx, - data: w, + data: entry_data, is_dir, exists, }| { + let is_marked = is_marked(marked.as_deref(), node_idx); let is_selected = is_selected(selected, node_idx); - let fraction = w.size as f32 / total as f32; + let fraction = entry_data.size as f32 / total as f32; let style = style(is_selected, is_focussed); + let local_style = local_style(fraction, style); let mut columns = Vec::new(); if should_show_mtime_column(sort_mode) { - columns.push(mtime_column(w, sort_mode, style)); + columns.push(mtime_column(entry_data, sort_mode, style)); } - columns.push(bytes_column(display, w, sort_mode, style)); - columns.push(percentage_column(display, fraction, style)); + columns.push(bytes_column(display, entry_data, sort_mode, style)); + columns.push(percentage_column(display, fraction, local_style)); columns.push(name_column( - w, is_dir, is_top, root, area, marked, node_idx, exists, style, + entry_data, + is_dir, + is_top, + root, + area, + name_style(is_marked, *exists, *is_dir, style), )); - columns_with_separators(columns, style) + columns_with_separators(columns, local_style) }, ); list.render(props, lines, area, buf); if *is_focussed { - let bound = draw_top_right_help(area, title, buf); + let bound = draw_top_right_help(area, &title, buf); draw_bottom_right_help(bound, buf); } } } fn entry_in_view( - selected: &Option<petgraph::stable_graph::NodeIndex>, - entries: &&[EntryDataBundle], + selected: Option<&petgraph::stable_graph::NodeIndex>, + entries: &[EntryDataBundle], ) -> Option<usize> { selected.map(|selected| { entries .iter() - .find_position(|b| b.index == selected) + .find_position(|b| b.index == *selected) .map(|(idx, _)| idx) .unwrap_or(0) }) @@ -167,14 +174,14 @@ fn draw_bottom_right_help(bound: Rect, buf: &mut Buffer) { } } -fn draw_top_right_help(area: Rect, title: String, buf: &mut Buffer) -> Rect { +fn draw_top_right_help(area: Rect, title: &str, buf: &mut Buffer) -> Rect { let help_text = " . = o|.. = u ── ⇊ = Ctrl+d|↓ = j|⇈ = Ctrl+u|↑ = k "; let help_text_block_width = block_width(help_text); let bound = Rect { width: area.width.saturating_sub(1), ..area }; - if block_width(&title) + help_text_block_width <= bound.width { + if block_width(title) + help_text_block_width <= bound.width { draw_text_nowrap_fn( rect::snap_to_right(bound, help_text_block_width), buf, @@ -185,16 +192,24 @@ fn draw_top_right_help(area: Rect, title: String, buf: &mut Buffer) -> Rect { bound } +fn is_marked( + marked: Option< + &std::collections::BTreeMap<petgraph::stable_graph::NodeIndex, super::EntryMark>, + >, + node_idx: &petgraph::stable_graph::NodeIndex, +) -> bool { + marked.map(|m| m.contains_key(node_idx)).unwrap_or(false) +} + fn is_selected( selected: &Option<petgraph::stable_graph::NodeIndex>, node_idx: &petgraph::stable_graph::NodeIndex, ) -> bool { - let is_selected = if let Some(idx) = selected { + if let Some(idx) = selected { *idx == *node_idx } else { false - }; - is_selected + } } fn style(is_selected: bool, is_focussed: &bool) -> Style { @@ -208,6 +223,15 @@ fn style(is_selected: bool, is_focussed: &bool) -> Style { style } +fn local_style(fraction: f32, style: Style) -> Style { + let should_avoid_showing_a_big_reversed_bar = fraction > 0.9; + if should_avoid_showing_a_big_reversed_bar { + style.remove_modifier(Modifier::REVERSED) + } else { + style + } +} + fn columns_with_separators(columns: Vec<Span<'_>>, style: Style) -> Vec<Span<'_>> { let mut columns_with_separators = Vec::new(); let column_count = columns.len(); @@ -220,8 +244,8 @@ fn columns_with_separators(columns: Vec<Span<'_>>, style: Style) -> Vec<Span<'_> columns_with_separators } -fn mtime_column<'a>(w: &'a EntryData, sort_mode: &'a SortMode, style: Style) -> Span<'a> { - let datetime = DateTime::<chrono::Utc>::from(w.mtime); +fn mtime_column<'a>(entry_data: &'a EntryData, sort_mode: &'a SortMode, style: Style) -> Span<'a> { + let datetime = DateTime::<chrono::Utc>::from(entry_data.mtime); let formatted_time = datetime.format("%d/%m/%Y %H:%M:%S").to_string(); Span::styled( format!("{:>20}", formatted_time), @@ -236,54 +260,50 @@ fn mtime_column<'a>(w: &'a EntryData, sort_mode: &'a SortMode, style: Style) -> } fn name_column<'a>( - w: &'a dua::traverse::EntryData, + entry_data: &'a dua::traverse::EntryData, is_dir: &'a bool, is_top: impl Fn(petgraph::stable_graph::NodeIndex) -> bool, root: &'a petgraph::stable_graph::NodeIndex, area: Rect, - marked: &Option< - &std::collections::BTreeMap<petgraph::stable_graph::NodeIndex, super::EntryMark>, - >, - node_idx: &'a petgraph::stable_graph::NodeIndex, - exists: &'a bool, style: Style, ) -> Span<'a> { Span::styled( fill_background_to_right( format!( "{prefix}{}", - w.name.to_string_lossy(), + entry_data.name.to_string_lossy(), prefix = if *is_dir && !is_top(*root) { "/" } else { " " } ), area.width, ), - { - let is_marked = marked.map(|m| m.contains_key(&node_idx)).unwrap_or(false); - let fg = if !exists { - // non-existing - always red! - Some(Color::Red) - } else { - entry_color(style.fg, !is_dir, is_marked) - }; - Style { fg, ..style } - }, + style, ) } -fn percentage_column<'a>(display: &'a DisplayOptions, fraction: f32, style: Style) -> Span<'a> { +fn name_style(is_marked: bool, exists: bool, is_dir: bool, style: Style) -> Style { + let fg = if !exists { + // non-existing - always red! + Some(Color::Red) + } else { + entry_color(style.fg, !is_dir, is_marked) + }; + Style { fg, ..style } +} + +fn percentage_column(display: &DisplayOptions, fraction: f32, style: Style) -> Span { Span::styled(format!("{}", display.byte_vis.display(fraction)), style) } fn bytes_column<'a>( display: &'a DisplayOptions, - w: &'a dua::traverse::EntryData, + entry_data: &'a dua::traverse::EntryData, sort_mode: &'a SortMode, style: Style, ) -> Span<'a> { Span::styled( format!( "{:>byte_column_width$}", - display.byte_format.display(w.size).to_string(), // we would have to impl alignment/padding ourselves otherwise... + display.byte_format.display(entry_data.size).to_string(), // we would have to impl alignment/padding ourselves otherwise... byte_column_width = display.byte_format.width() ), Style { |