Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 5, 2021
1 parent 1fb6bad commit c3c103e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
41 changes: 24 additions & 17 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ pub struct TerminalApp {
type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Key>, TerminalApp);

impl TerminalApp {
pub fn refresh_view<B>(&mut self, terminal: &mut Terminal<B>)
where
B: Backend,
{
// Use an event that does nothing to trigger a refresh
self.state
.process_events(
&mut self.window,
&mut self.traversal,
&mut self.display,
terminal,
std::iter::once(Key::Alt('\r')),
)
.ok();
}
pub fn process_events<B>(
&mut self,
terminal: &mut Terminal<B>,
Expand All @@ -208,7 +223,7 @@ impl TerminalApp {
pub fn initialize<B>(
terminal: &mut Terminal<B>,
options: WalkOptions,
input: Vec<PathBuf>,
input_paths: Vec<PathBuf>,
mode: Interaction,
) -> Result<Option<KeyboardInputAndApp>>
where
Expand Down Expand Up @@ -237,7 +252,7 @@ impl TerminalApp {

let mut state = None::<AppState>;
let mut received_events = false;
let traversal = Traversal::from_walk(options, input, |traversal| {
let traversal = Traversal::from_walk(options, input_paths, |traversal| {
let s = match state.as_mut() {
Some(s) => {
s.entries = sorted_entries(&traversal.tree, s.root, s.sorting);
Expand Down Expand Up @@ -279,14 +294,13 @@ impl TerminalApp {
};
Ok(should_exit)
})?;
let mut traversal = match traversal {
let traversal = match traversal {
Some(t) => t,
None => return Ok(None),
};

Ok(Some((
keys_rx,
TerminalApp {
Ok(Some((keys_rx, {
let mut app = TerminalApp {
state: {
let mut s = state.unwrap_or_else(|| {
let sorting = Default::default();
Expand All @@ -306,22 +320,15 @@ impl TerminalApp {
} else {
s.entries.get(0).map(|b| b.index)
};
// Force event processing with a key that doesn't do anything.
s.process_events(
&mut window,
&mut traversal,
&mut display,
terminal,
std::iter::once(Key::Alt('\r')),
)
.ok();
s
},
display,
traversal,
window,
},
)))
};
app.refresh_view(terminal);
app
})))
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/interactive/app/tests/journeys_readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ use anyhow::Result;
use pretty_assertions::assert_eq;
use std::ffi::OsString;

use crate::interactive::app::tests::utils::{
fixture_str, index_by_name, initialized_app_and_terminal_from_fixture, into_keys,
node_by_index, node_by_name,
use crate::interactive::{
app::tests::{
utils::{
fixture_str, index_by_name, initialized_app_and_terminal_from_fixture, into_keys,
node_by_index, node_by_name,
},
FIXTURE_PATH,
},
SortMode,
};
use crate::interactive::app::tests::FIXTURE_PATH;
use crate::interactive::SortMode;

#[test]
fn init_from_pdu_results() -> Result<()> {
use crate::interactive::app::tests::utils::new_test_terminal;
let _terminal = new_test_terminal()?;

Ok(())
}

#[test]
fn simple_user_journey_read_only() -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/interactive/app/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn initialized_app_and_terminal_with_closure(
let mut terminal = new_test_terminal()?;
std::env::set_current_dir(Path::new(env!("CARGO_MANIFEST_DIR")))?;

let input = fixture_paths.iter().map(|c| convert(c.as_ref())).collect();
let input_paths = fixture_paths.iter().map(|c| convert(c.as_ref())).collect();
let app = TerminalApp::initialize(
&mut terminal,
WalkOptions {
Expand All @@ -179,7 +179,7 @@ pub fn initialized_app_and_terminal_with_closure(
sorting: TraversalSorting::AlphabeticalByFileName,
cross_filesystems: false,
},
input,
input_paths,
Interaction::None,
)?
.map(|(_, app)| app);
Expand Down

0 comments on commit c3c103e

Please sign in to comment.