Skip to content

Commit

Permalink
Keep selecting the first element during iteration unless…
Browse files Browse the repository at this point in the history
…the user provided input already.

Related to #77
  • Loading branch information
Byron committed Feb 15, 2021
1 parent 6ca9e6c commit 6d7b3cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ impl TerminalApp {
};

let mut state = None::<AppState>;
let mut received_events = false;
let traversal = Traversal::from_walk(options, input, |traversal| {
let s = match state.as_mut() {
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
}
None => {
Expand All @@ -260,12 +264,15 @@ impl TerminalApp {
}
};
s.reset_message(); // force "scanning" to appear
let events = fetch_buffered_key_events();
received_events |= !events.is_empty();

let should_exit = match s.process_events(
&mut window,
traversal,
&mut display,
terminal,
fetch_buffered_key_events().into_iter(),
events.into_iter(),
)? {
ProcessingResult::ExitRequested(_) => true,
ProcessingResult::Finished(_) => false,
Expand Down Expand Up @@ -294,7 +301,11 @@ impl TerminalApp {
});
s.is_scanning = false;
s.entries = sorted_entries(&traversal.tree, s.root, s.sorting);
s.selected = s.selected.or_else(|| s.entries.get(0).map(|b| b.index));
s.selected = if received_events {
s.selected.or_else(|| s.entries.get(0).map(|b| b.index))
} else {
s.entries.get(0).map(|b| b.index)
};
s
},
display,
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![forbid(unsafe_code)]
use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::Clap;
use dua::{ByteFormat, TraversalSorting};
use std::{fs, io, io::Write, path::PathBuf, process};
Expand All @@ -24,7 +24,7 @@ fn main() -> Result<()> {
#[cfg(any(feature = "tui-unix", feature = "tui-crossplatform"))]
Some(Interactive { input }) => {
use crate::interactive::{Interaction, TerminalApp};
use anyhow::Context;
use anyhow::{anyhow, Context};
use crosstermion::terminal::{tui::new_terminal, AlternateRawScreen};

let no_tty_msg = "Interactive mode requires a connected terminal";
Expand Down

0 comments on commit 6d7b3cd

Please sign in to comment.