Skip to content

Commit

Permalink
Fix visual changes
Browse files Browse the repository at this point in the history
Fixed clippy warnings & code review comments
  • Loading branch information
piotrwach committed Dec 8, 2023
1 parent 1a46d8f commit b8ad16b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
8 changes: 4 additions & 4 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions src/interactive/app/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
Expand Down Expand Up @@ -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
Expand Down
98 changes: 59 additions & 39 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Entries {
let total: u128 = entries.iter().map(|b| b.data.size).sum();
let title = title(&current_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),
Expand All @@ -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)
})
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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),
Expand All @@ -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 {
Expand Down

0 comments on commit b8ad16b

Please sign in to comment.