From 13c381bebc6a64e553ec11793ec8880f868e712c Mon Sep 17 00:00:00 2001 From: Piotr Wach Date: Sun, 7 Jan 2024 09:44:10 +0000 Subject: Clean-up init function --- src/aggregate.rs | 8 +++--- src/interactive/app/app_state.rs | 2 +- src/interactive/app/eventloop.rs | 8 ++++-- src/interactive/app/mod.rs | 4 +-- src/interactive/app/terminal_app.rs | 26 ++++++++--------- src/interactive/app/tests/utils.rs | 18 ++++-------- src/interactive/widgets/main.rs | 3 +- src/main.rs | 57 +++++++++++++++++-------------------- 8 files changed, 59 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/aggregate.rs b/src/aggregate.rs index d314571..163a7eb 100644 --- a/src/aggregate.rs +++ b/src/aggregate.rs @@ -1,4 +1,4 @@ -use crate::{crossdev, InodeFilter, Throttle, WalkOptions, WalkResult, ByteFormat}; +use crate::{crossdev, ByteFormat, InodeFilter, Throttle, WalkOptions, WalkResult}; use anyhow::Result; use filesize::PathExt; use owo_colors::{AnsiColors as Color, OwoColorize}; @@ -95,7 +95,7 @@ pub fn aggregate( num_bytes, num_errors, path_color_of(&path), - byte_format + byte_format, )?; } total += num_bytes; @@ -116,7 +116,7 @@ pub fn aggregate( num_bytes, num_errors, path_color_of(&path), - byte_format + byte_format, )?; } } @@ -129,7 +129,7 @@ pub fn aggregate( total, res.num_errors, None, - byte_format + byte_format, )?; } Ok((res, stats)) diff --git a/src/interactive/app/app_state.rs b/src/interactive/app/app_state.rs index 7983374..dd0f9a0 100644 --- a/src/interactive/app/app_state.rs +++ b/src/interactive/app/app_state.rs @@ -32,4 +32,4 @@ pub struct AppState { pub enum ProcessingResult { Finished(WalkResult), ExitRequested(WalkResult), -} \ No newline at end of file +} diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 98e13c2..df297e3 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -1,9 +1,10 @@ use crate::interactive::{ app::navigation::Navigation, + app_state::FocussedPane, sorted_entries, widgets::{glob_search, MainWindow, MainWindowProps}, ByteVisualization, CursorDirection, CursorMode, DisplayOptions, EntryDataBundle, MarkEntryMode, - SortMode, app_state::FocussedPane, + SortMode, }; use anyhow::Result; use crossbeam::channel::Receiver; @@ -17,8 +18,11 @@ use std::path::PathBuf; use tui::backend::Backend; use tui_react::Terminal; -use super::{input::input_channel, app_state::{AppState, Cursor, ProcessingResult}}; use super::tree_view::TreeView; +use super::{ + app_state::{AppState, Cursor, ProcessingResult}, + input::input_channel, +}; impl AppState { pub fn navigation_mut(&mut self) -> &mut Navigation { diff --git a/src/interactive/app/mod.rs b/src/interactive/app/mod.rs index 742127a..2dd74eb 100644 --- a/src/interactive/app/mod.rs +++ b/src/interactive/app/mod.rs @@ -1,12 +1,12 @@ +pub mod app_state; mod bytevis; mod common; mod eventloop; mod handlers; pub mod input; mod navigation; -pub mod tree_view; pub mod terminal_app; -pub mod app_state; +pub mod tree_view; pub use bytevis::*; pub use common::*; diff --git a/src/interactive/app/terminal_app.rs b/src/interactive/app/terminal_app.rs index 9d97ae7..0c96140 100644 --- a/src/interactive/app/terminal_app.rs +++ b/src/interactive/app/terminal_app.rs @@ -1,16 +1,21 @@ use std::path::PathBuf; +use anyhow::Result; use crossbeam::channel::Receiver; use crosstermion::input::Event; -use dua::{traverse::{Traversal, Tree, EntryData}, WalkResult, WalkOptions, ByteFormat}; +use dua::{ + traverse::{EntryData, Traversal, Tree}, + ByteFormat, WalkOptions, WalkResult, +}; use tui::prelude::Backend; use tui_react::Terminal; -use anyhow::Result; use crate::interactive::widgets::MainWindow; -use super::{DisplayOptions, ByteVisualization, sorted_entries, refresh_key, app_state::{ProcessingResult, AppState}}; - +use super::{ + app_state::{AppState, ProcessingResult}, + refresh_key, sorted_entries, ByteVisualization, DisplayOptions, +}; /// State and methods representing the interactive disk usage analyser for the terminal pub struct TerminalApp { @@ -58,18 +63,13 @@ impl TerminalApp { } } - pub fn initialize( - terminal: &mut Terminal, - byte_format: ByteFormat, - input_paths: Vec, - keys_rx: Receiver, - ) -> Result> + pub fn initialize(terminal: &mut Terminal, byte_format: ByteFormat) -> Result where B: Backend, { terminal.hide_cursor()?; terminal.clear()?; - + let mut display = DisplayOptions::new(byte_format); let mut window = MainWindow::default(); @@ -133,7 +133,7 @@ impl TerminalApp { // state.is_scanning = false; // if !received_events { - // } + // } let traversal = { let mut tree = Tree::new(); @@ -166,6 +166,6 @@ impl TerminalApp { }; app.refresh_view(terminal); - Ok(Some((keys_rx, app))) + Ok(app) } } diff --git a/src/interactive/app/tests/utils.rs b/src/interactive/app/tests/utils.rs index e5c432a..64d561c 100644 --- a/src/interactive/app/tests/utils.rs +++ b/src/interactive/app/tests/utils.rs @@ -175,19 +175,11 @@ 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 (_, keys_rx) = crossbeam::channel::unbounded(); - let input_paths = fixture_paths.iter().map(|c| convert(c.as_ref())).collect(); - let app = TerminalApp::initialize( - &mut terminal, - ByteFormat::Metric, - input_paths, - keys_rx, - )? - .map(|(_, app)| app); - Ok(( - terminal, - app.expect("app that didn't try to abort iteration"), - )) + // let (_, keys_rx) = crossbeam::channel::unbounded(); + // let input_paths = fixture_paths.iter().map(|c| convert(c.as_ref())).collect(); + let app = TerminalApp::initialize(&mut terminal, ByteFormat::Metric)?; + + Ok((terminal, app)) // WalkOptions { // threads: 1, diff --git a/src/interactive/widgets/main.rs b/src/interactive/widgets/main.rs index 6a4bfb8..3df375a 100644 --- a/src/interactive/widgets/main.rs +++ b/src/interactive/widgets/main.rs @@ -1,9 +1,10 @@ use crate::interactive::{ + app_state::{AppState, Cursor, FocussedPane}, widgets::{ Entries, EntriesProps, Footer, FooterProps, GlobPane, GlobPaneProps, Header, HelpPane, HelpPaneProps, MarkPane, MarkPaneProps, COLOR_MARKED, }, - DisplayOptions, app_state::{AppState, FocussedPane, Cursor}, + DisplayOptions, }; use std::borrow::Borrow; use tui::backend::Backend; diff --git a/src/main.rs b/src/main.rs index 7694cf1..e579aad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,46 +68,41 @@ fn main() -> Result<()> { ) .with_context(|| "Could not instantiate terminal")?; + // TODO: use + // extract_paths_maybe_set_cwd(input, !opt.stay_on_filesystem)?, + let keys_rx = input_channel(); - let res = TerminalApp::initialize( - &mut terminal, - byte_format, - extract_paths_maybe_set_cwd(input, !opt.stay_on_filesystem)?, - keys_rx, - )? - .map(|(keys_rx, mut app)| { - let res = app.process_events(&mut terminal, keys_rx.into_iter()); - - let res = res.map(|r| { - ( - r, - app.window - .mark_pane - .take() - .map(|marked| marked.into_paths()), - ) - }); - // Leak app memory to avoid having to wait for the hashmap to deallocate, - // which causes a noticeable delay shortly before the the program exits anyway. - std::mem::forget(app); - res + let mut app = TerminalApp::initialize(&mut terminal, byte_format)?; + + let res = app.process_events(&mut terminal, keys_rx.into_iter()); + + let res = res.map(|r| { + ( + r, + app.window + .mark_pane + .take() + .map(|marked| marked.into_paths()), + ) }); + // Leak app memory to avoid having to wait for the hashmap to deallocate, + // which causes a noticeable delay shortly before the the program exits anyway. + std::mem::forget(app); drop(terminal); io::stderr().flush().ok(); // Exit 'quickly' to avoid having to not have to deal with slightly different types in the other match branches std::process::exit( - res.transpose()? - .map(|(walk_result, paths)| { - if let Some(paths) = paths { - for path in paths { - println!("{}", path.display()) - } + res.map(|(walk_result, paths)| { + if let Some(paths) = paths { + for path in paths { + println!("{}", path.display()) } - walk_result.to_exit_code() - }) - .unwrap_or(0), + } + walk_result.to_exit_code() + }) + .unwrap_or(0), ); } Some(Aggregate { -- cgit v1.2.3