diff --git a/benches/criterion.rs b/benches/criterion.rs index 4e48a58..98f7d9b 100644 --- a/benches/criterion.rs +++ b/benches/criterion.rs @@ -14,8 +14,7 @@ const PWD: &str = "/tmp/xplr_bench"; fn navigation_benchmark(c: &mut Criterion) { fs::create_dir_all(PWD).unwrap(); (1..10000).for_each(|i| { - fs::File::create(std::path::Path::new(PWD).join(i.to_string())) - .unwrap(); + fs::File::create(std::path::Path::new(PWD).join(i.to_string())).unwrap(); }); let lua = mlua::Lua::new(); @@ -94,8 +93,7 @@ fn navigation_benchmark(c: &mut Criterion) { fn draw_benchmark(c: &mut Criterion) { fs::create_dir_all(PWD).unwrap(); (1..10000).for_each(|i| { - fs::File::create(std::path::Path::new(PWD).join(i.to_string())) - .unwrap(); + fs::File::create(std::path::Path::new(PWD).join(i.to_string())).unwrap(); }); let lua = mlua::Lua::new(); diff --git a/rustfmt.toml b/rustfmt.toml index 471c53a..4ef263b 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,4 @@ edition = "2021" -max_width = 80 +max_width = 89 tab_spaces = 4 use_field_init_shorthand = true diff --git a/src/app.rs b/src/app.rs index a40b139..658cd85 100644 --- a/src/app.rs +++ b/src/app.rs @@ -200,8 +200,7 @@ impl App { None } } else { - let path = - PathBuf::from("/").join("etc").join("xplr").join("init.lua"); + let path = PathBuf::from("/").join("etc").join("xplr").join("init.lua"); if path.exists() { Some(path) } else { @@ -342,10 +341,7 @@ impl App { self } - pub fn handle_batch_external_msgs( - mut self, - msgs: Vec, - ) -> Result { + pub fn handle_batch_external_msgs(mut self, msgs: Vec) -> Result { for task in msgs .into_iter() .map(|msg| Task::new(MsgIn::External(msg), None)) @@ -376,11 +372,7 @@ impl App { } } - fn handle_external( - self, - msg: ExternalMsg, - key: Option, - ) -> Result { + fn handle_external(self, msg: ExternalMsg, key: Option) -> Result { if self.config.general.read_only && !msg.is_read_only() { self.log_error("Cannot execute code in read-only mode.".into()) } else { @@ -402,9 +394,7 @@ impl App { self.focus_previous_by_relative_index_from_input() } FocusNext => self.focus_next(), - FocusNextByRelativeIndex(i) => { - self.focus_next_by_relative_index(i) - } + FocusNextByRelativeIndex(i) => self.focus_next_by_relative_index(i), FocusNextByRelativeIndexFromInput => { self.focus_next_by_relative_index_from_input() } @@ -421,18 +411,14 @@ impl App { FollowSymlink => self.follow_symlink(), SetInputPrompt(p) => self.set_input_prompt(p), UpdateInputBuffer(op) => self.update_input_buffer(op), - UpdateInputBufferFromKey => { - self.update_input_buffer_from_key(key) - } + UpdateInputBufferFromKey => self.update_input_buffer_from_key(key), BufferInput(input) => self.buffer_input(&input), BufferInputFromKey => self.buffer_input_from_key(key), SetInputBuffer(input) => self.set_input_buffer(input), RemoveInputBufferLastCharacter => { self.remove_input_buffer_last_character() } - RemoveInputBufferLastWord => { - self.remove_input_buffer_last_word() - } + RemoveInputBufferLastWord => self.remove_input_buffer_last_word(), ResetInputBuffer => self.reset_input_buffer(), SwitchMode(mode) => self.switch_mode(&mode), SwitchModeKeepingInputBuffer(mode) => { @@ -447,9 +433,7 @@ impl App { self.switch_mode_custom_keeping_input_buffer(&mode) } PopMode => self.pop_mode(), - PopModeKeepingInputBuffer => { - self.pop_mode_keeping_input_buffer() - } + PopModeKeepingInputBuffer => self.pop_mode_keeping_input_buffer(), SwitchLayout(mode) => self.switch_layout(&mode), SwitchLayoutBuiltin(mode) => self.switch_layout_builtin(&mode), SwitchLayoutCustom(mode) => self.switch_layout_custom(&mode), @@ -474,9 +458,7 @@ impl App { AddNodeFilter(f) => self.add_node_filter(f), AddNodeFilterFromInput(f) => self.add_node_filter_from_input(f), RemoveNodeFilter(f) => self.remove_node_filter(f), - RemoveNodeFilterFromInput(f) => { - self.remove_node_filter_from_input(f) - } + RemoveNodeFilterFromInput(f) => self.remove_node_filter_from_input(f), ToggleNodeFilter(f) => self.toggle_node_filter(f), RemoveLastNodeFilter => self.remove_last_node_filter(), ResetNodeFilters => self.reset_node_filters(), @@ -650,10 +632,7 @@ impl App { Ok(self) } - fn focus_previous_by_relative_index( - mut self, - index: usize, - ) -> Result { + fn focus_previous_by_relative_index(mut self, index: usize) -> Result { let mut history = self.history.clone(); if let Some(dir) = self.directory_buffer_mut() { if let Some(n) = dir.focused_node() { @@ -737,11 +716,7 @@ impl App { } } - fn change_directory( - mut self, - dir: &str, - save_history: bool, - ) -> Result { + fn change_directory(mut self, dir: &str, save_history: bool) -> Result { let mut dir = PathBuf::from(dir); if dir.is_relative() { dir = PathBuf::from(self.pwd.clone()).join(dir); @@ -750,8 +725,7 @@ impl App { match env::set_current_dir(&dir) { Ok(()) => { let pwd = self.pwd.clone(); - let focus = - self.focused_node().map(|n| n.relative_path.clone()); + let focus = self.focused_node().map(|n| n.relative_path.clone()); self = self.add_last_focus(pwd, focus)?; self.pwd = dir.to_string_lossy().to_string(); if save_history { @@ -922,11 +896,7 @@ impl App { } } - pub fn focus_by_file_name( - mut self, - name: &str, - save_history: bool, - ) -> Result { + pub fn focus_by_file_name(mut self, name: &str, save_history: bool) -> Result { let mut history = self.history.clone(); if let Some(dir_buf) = self.directory_buffer_mut() { if let Some(focus) = dir_buf @@ -964,14 +934,11 @@ impl App { } if let Some(parent) = pathbuf.parent() { if let Some(filename) = pathbuf.file_name() { - self.change_directory( - &parent.to_string_lossy().to_string(), - false, - )? - .focus_by_file_name( - &filename.to_string_lossy().to_string(), - save_history, - ) + self.change_directory(&parent.to_string_lossy().to_string(), false)? + .focus_by_file_name( + &filename.to_string_lossy().to_string(), + save_history, + ) } else { self.log_error(format!("{} not found", path)) } @@ -1033,10 +1000,7 @@ impl App { .and_then(App::reset_input_buffer) } - fn switch_mode_builtin_keeping_input_buffer( - mut self, - mode: &str, - ) -> Result { + fn switch_mode_builtin_keeping_input_buffer(mut self, mode: &str) -> Result { if let Some(mode) = self.config.modes.builtin.get(mode).cloned() { self = self.push_mode(); self.mode = mode.sanitized(self.config.general.read_only); @@ -1051,10 +1015,7 @@ impl App { .and_then(App::reset_input_buffer) } - fn switch_mode_custom_keeping_input_buffer( - mut self, - mode: &str, - ) -> Result { + fn switch_mode_custom_keeping_input_buffer(mut self, mode: &str) -> Result { if let Some(mode) = self.config.modes.custom.get(mode).cloned() { self = self.push_mode(); self.mode = mode.sanitized(self.config.general.read_only); @@ -1174,8 +1135,7 @@ impl App { path = PathBuf::from(self.pwd.clone()).join(path); } let parent = path.parent().map(|p| p.to_string_lossy().to_string()); - let filename = - path.file_name().map(|p| p.to_string_lossy().to_string()); + let filename = path.file_name().map(|p| p.to_string_lossy().to_string()); if let (Some(p), Some(n)) = (parent, filename) { self.selection.insert(Node::new(p, n)); } @@ -1265,33 +1225,21 @@ impl App { Ok(self) } - fn add_node_filter_from_input( - mut self, - filter: NodeFilter, - ) -> Result { + fn add_node_filter_from_input(mut self, filter: NodeFilter) -> Result { if let Some(input) = self.input.buffer.as_ref() { self.explorer_config .filters - .insert(NodeFilterApplicable::new( - filter, - input.value().into(), - )); + .insert(NodeFilterApplicable::new(filter, input.value().into())); }; Ok(self) } - fn remove_node_filter( - mut self, - filter: NodeFilterApplicable, - ) -> Result { + fn remove_node_filter(mut self, filter: NodeFilterApplicable) -> Result { self.explorer_config.filters.retain(|f| f != &filter); Ok(self) } - fn remove_node_filter_from_input( - mut self, - filter: NodeFilter, - ) -> Result { + fn remove_node_filter_from_input(mut self, filter: NodeFilter) -> Result { if let Some(input) = self.input.buffer.as_ref() { let nfa = NodeFilterApplicable::new(filter, input.value().into()); self.explorer_config.filters.retain(|f| f != &nfa); @@ -1486,9 +1434,8 @@ impl App { fn refresh_selection(mut self) -> Result { // Should be able to select broken symlink - self.selection.retain(|n| { - PathBuf::from(&n.absolute_path).symlink_metadata().is_ok() - }); + self.selection + .retain(|n| PathBuf::from(&n.absolute_path).symlink_metadata().is_ok()); Ok(self) } diff --git a/src/config.rs b/src/config.rs index 0819baa..d6f67f1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -323,21 +323,16 @@ impl KeyBindings { .filter_map(|(k, a)| a.sanitized(read_only).map(|a| (k, a))) .collect(); - self.on_alphabet = - self.on_alphabet.and_then(|a| a.sanitized(read_only)); - self.on_number = - self.on_number.and_then(|a| a.sanitized(read_only)); + self.on_alphabet = self.on_alphabet.and_then(|a| a.sanitized(read_only)); + self.on_number = self.on_number.and_then(|a| a.sanitized(read_only)); self.on_alphanumeric = self.on_alphanumeric.and_then(|a| a.sanitized(read_only)); self.on_special_character = self .on_special_character .and_then(|a| a.sanitized(read_only)); - self.on_character = - self.on_character.and_then(|a| a.sanitized(read_only)); - self.on_navigation = - self.on_navigation.and_then(|a| a.sanitized(read_only)); - self.on_function = - self.on_function.and_then(|a| a.sanitized(read_only)); + self.on_character = self.on_character.and_then(|a| a.sanitized(read_only)); + self.on_navigation = self.on_navigation.and_then(|a| a.sanitized(read_only)); + self.on_function = self.on_function.and_then(|a| a.sanitized(read_only)); self.default = self.default.and_then(|a| a.sanitized(read_only)); }; self @@ -387,36 +382,32 @@ impl Mode { let lines = extra_help_lines .unwrap_or_default() .into_iter() - .chain(self.key_bindings.on_key.iter().filter_map( - |(k, a)| { - let remaps = self - .key_bindings - .on_key - .iter() - .filter_map(|(rk, ra)| { - if rk == k { - None - } else if a == ra { - Some(rk.clone()) - } else { - None - } - }) - .collect::>(); - a.help.clone().map(|h| { - HelpMenuLine::KeyMap(k.into(), remaps, h) + .chain(self.key_bindings.on_key.iter().filter_map(|(k, a)| { + let remaps = self + .key_bindings + .on_key + .iter() + .filter_map(|(rk, ra)| { + if rk == k { + None + } else if a == ra { + Some(rk.clone()) + } else { + None + } }) - }, - )) + .collect::>(); + a.help + .clone() + .map(|h| HelpMenuLine::KeyMap(k.into(), remaps, h)) + })) .chain( self.key_bindings .on_alphabet .iter() .map(|a| ("[a-Z]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -425,9 +416,7 @@ impl Mode { .iter() .map(|a| ("[0-9]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -436,9 +425,7 @@ impl Mode { .iter() .map(|a| ("[0-Z]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -447,9 +434,7 @@ impl Mode { .iter() .map(|a| ("[^0-Z]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -458,9 +443,7 @@ impl Mode { .iter() .map(|a| ("[*]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -469,9 +452,7 @@ impl Mode { .iter() .map(|a| ("[nav]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -480,9 +461,7 @@ impl Mode { .iter() .map(|a| ("[f1-f12]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ) .chain( @@ -491,9 +470,7 @@ impl Mode { .iter() .map(|a| ("[default]", a.help.clone())) .filter_map(|(k, mh)| { - mh.map(|h| { - HelpMenuLine::KeyMap(k.into(), vec![], h) - }) + mh.map(|h| HelpMenuLine::KeyMap(k.into(), vec![], h)) }), ); diff --git a/src/event_reader.rs b/src/event_reader.rs index 6ad4780..6b96a46 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -49,8 +49,7 @@ fn keep_reading( if rx_stopper.try_recv().unwrap_or(false) { tx_ack.send(()).unwrap(); break; - } else if event::poll(std::time::Duration::from_millis(150)) - .unwrap_or_default() + } else if event::poll(std::time::Duration::from_millis(150)).unwrap_or_default() { // NOTE: The poll timeout need to stay low, else spawning sub subshell // and start typing immediately will cause panic. diff --git a/src/explorer.rs b/src/explorer.rs index 9bb25b9..5a9102e 100644 --- a/src/explorer.rs +++ b/src/explorer.rs @@ -1,6 +1,5 @@ use crate::app::{ - DirectoryBuffer, ExplorerConfig, ExternalMsg, InternalMsg, MsgIn, Node, - Task, + DirectoryBuffer, ExplorerConfig, ExternalMsg, InternalMsg, MsgIn, Node, Task, }; use anyhow::{Error, Result}; use std::fs; diff --git a/src/lua.rs b/src/lua.rs index 5ebda1e..160c4a8 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -17,11 +17,8 @@ pub fn serialize<'lua, T: Serialize + Sized>( lua: &'lua mlua::Lua, value: &T, ) -> Result> { - lua.to_value_with( - value, - SerializeOptions::new().serialize_none_to_null(false), - ) - .map_err(Error::from) + lua.to_value_with(value, SerializeOptions::new().serialize_none_to_null(false)) + .map_err(Error::from) } fn parse_version(version: &str) -> Result<(u16, u16, u16, Option)> { @@ -46,11 +43,7 @@ pub fn check_version(version: &str, path: &str) -> Result<()> { let (rmajor, rminor, rbugfix, rbeta) = parse_version(VERSION)?; let (smajor, sminor, sbugfix, sbeta) = parse_version(version)?; - if rmajor == smajor - && rminor == sminor - && rbugfix >= sbugfix - && rbeta == sbeta - { + if rmajor == smajor && rminor == sminor && rbugfix >= sbugfix && rbeta == sbeta { Ok(()) } else { bail!( @@ -95,11 +88,10 @@ pub fn extend(lua: &Lua, path: &str) -> Result { lua.load(&script).set_name("init")?.exec()?; - let version: String = - match globals.get("version").and_then(|v| lua.from_value(v)) { - Ok(v) => v, - Err(_) => bail!("'version' must be defined globally in {}", path), - }; + let version: String = match globals.get("version").and_then(|v| lua.from_value(v)) { + Ok(v) => v, + Err(_) => bail!("'version' must be defined globally in {}", path), + }; check_version(&version, path)?; diff --git a/src/msg/in_/external.rs b/src/msg/in_/external.rs index 4a16b0d..b8029dd 100644 --- a/src/msg/in_/external.rs +++ b/src/msg/in_/external.rs @@ -1085,18 +1085,16 @@ impl NodeSorterApplicable { .unwrap_or_default(), ), - NodeSorter::ByICanonicalAbsolutePath => { - natord::compare_ignore_case( - &a.canonical - .as_ref() - .map(|s| s.absolute_path.clone()) - .unwrap_or_default(), - &b.canonical - .as_ref() - .map(|s| s.absolute_path.clone()) - .unwrap_or_default(), - ) - } + NodeSorter::ByICanonicalAbsolutePath => natord::compare_ignore_case( + &a.canonical + .as_ref() + .map(|s| s.absolute_path.clone()) + .unwrap_or_default(), + &b.canonical + .as_ref() + .map(|s| s.absolute_path.clone()) + .unwrap_or_default(), + ), NodeSorter::ByCanonicalExtension => a .canonical @@ -1292,26 +1290,18 @@ impl NodeFilter { fn apply(&self, node: &Node, input: &str, regex: Option<&Regex>) -> bool { match self { Self::RelativePathIs => node.relative_path.eq(input), - Self::IRelativePathIs => { - node.relative_path.eq_ignore_ascii_case(input) - } + Self::IRelativePathIs => node.relative_path.eq_ignore_ascii_case(input), Self::RelativePathIsNot => !node.relative_path.eq(input), - Self::IRelativePathIsNot => { - !node.relative_path.eq_ignore_ascii_case(input) - } + Self::IRelativePathIsNot => !node.relative_path.eq_ignore_ascii_case(input), - Self::RelativePathDoesStartWith => { - node.relative_path.starts_with(input) - } + Self::RelativePathDoesStartWith => node.relative_path.starts_with(input), Self::IRelativePathDoesStartWith => node .relative_path .to_lowercase() .starts_with(&input.to_lowercase()), - Self::RelativePathDoesNotStartWith => { - !node.relative_path.starts_with(input) - } + Self::RelativePathDoesNotStartWith => !node.relative_path.starts_with(input), Self::IRelativePathDoesNotStartWith => !node .relative_path .to_lowercase() @@ -1323,25 +1313,19 @@ impl NodeFilter { .to_lowercase() .contains(&input.to_lowercase()), - Self::RelativePathDoesNotContain => { - !node.relative_path.contains(input) - } + Self::RelativePathDoesNotContain => !node.relative_path.contains(input), Self::IRelativePathDoesNotContain => !node .relative_path .to_lowercase() .contains(&input.to_lowercase()), - Self::RelativePathDoesEndWith => { - node.relative_path.ends_with(input) - } + Self::RelativePathDoesEndWith => node.relative_path.ends_with(input), Self::IRelativePathDoesEndWith => node .relative_path .to_lowercase() .ends_with(&input.to_lowercase()), - Self::RelativePathDoesNotEndWith => { - !node.relative_path.ends_with(input) - } + Self::RelativePathDoesNotEndWith => !node.relative_path.ends_with(input), Self::IRelativePathDoesNotEndWith => !node .relative_path .to_lowercase() @@ -1362,26 +1346,18 @@ impl NodeFilter { .unwrap_or(false), Self::AbsolutePathIs => node.absolute_path.eq(input), - Self::IAbsolutePathIs => { - node.absolute_path.eq_ignore_ascii_case(input) - } + Self::IAbsolutePathIs => node.absolute_path.eq_ignore_ascii_case(input), Self::AbsolutePathIsNot => !node.absolute_path.eq(input), - Self::IAbsolutePathIsNot => { - !node.absolute_path.eq_ignore_ascii_case(input) - } + Self::IAbsolutePathIsNot => !node.absolute_path.eq_ignore_ascii_case(input), - Self::AbsolutePathDoesStartWith => { - node.absolute_path.starts_with(input) - } + Self::AbsolutePathDoesStartWith => node.absolute_path.starts_with(input), Self::IAbsolutePathDoesStartWith => node .absolute_path .to_lowercase() .starts_with(&input.to_lowercase()), - Self::AbsolutePathDoesNotStartWith => { - !node.absolute_path.starts_with(input) - } + Self::AbsolutePathDoesNotStartWith => !node.absolute_path.starts_with(input), Self::IAbsolutePathDoesNotStartWith => !node .absolute_path .to_lowercase() @@ -1393,25 +1369,19 @@ impl NodeFilter { .to_lowercase() .contains(&input.to_lowercase()), - Self::AbsolutePathDoesNotContain => { - !node.absolute_path.contains(input) - } + Self::AbsolutePathDoesNotContain => !node.absolute_path.contains(input), Self::IAbsolutePathDoesNotContain => !node .absolute_path .to_lowercase() .contains(&input.to_lowercase()), - Self::AbsolutePathDoesEndWith => { - node.absolute_path.ends_with(input) - } + Self::AbsolutePathDoesEndWith => node.absolute_path.ends_with(input), Self::IAbsolutePathDoesEndWith => node .absolute_path .to_lowercase() .ends_with(&input.to_lowercase()), - Self::AbsolutePathDoesNotEndWith => { - !node.absolute_path.ends_with(input) - } + Self::AbsolutePathDoesNotEndWith => !node.absolute_path.ends_with(input), Self::IAbsolutePathDoesNotEndWith => !node .absolute_path .to_lowercase() diff --git a/src/permissions.rs b/src/permissions.rs index 5a10346..7f41c6e 100644 --- a/src/permissions.rs +++ b/src/permissions.rs @@ -3,9 +3,7 @@ use serde::{Deserialize, Serialize}; use std::fs::Metadata; -#[derive( - Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Hash, Default, -)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Hash, Default)] pub struct Permissions { pub user_read: bool, pub user_write: bool, diff --git a/src/pipe.rs b/src/pipe.rs index b47999b..b9a6fdd 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -23,8 +23,7 @@ impl Pipe { let msg_in = path.join("msg_in").to_string_lossy().to_string(); - let selection_out = - path.join("selection_out").to_string_lossy().to_string(); + let selection_out = path.join("selection_out").to_string_lossy().to_string(); let result_out = path.join("result_out").to_string_lossy().to_string(); @@ -40,8 +39,7 @@ impl Pipe { let logs_out = path.join("logs_out").to_string_lossy().to_string(); - let history_out = - path.join("history_out").to_string_lossy().to_string(); + let history_out = path.join("history_out").to_string_lossy().to_string(); Ok(Self { path: path.to_string_lossy().to_string(), diff --git a/src/runner.rs b/src/runner.rs index 765d0ed..90698f9 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -172,12 +172,8 @@ impl Runner { pub fn run(self) -> Result> { // Why unsafe? See https://github.com/sayanarijit/xplr/issues/309 let lua = unsafe { mlua::Lua::unsafe_new() }; - let mut app = app::App::create( - self.pwd, - &lua, - self.config_file, - self.extra_config_files, - )?; + let mut app = + app::App::create(self.pwd, &lua, self.config_file, self.extra_config_files)?; app.config.general.read_only = self.read_only; fs::create_dir_all(app.session_path.clone())?; @@ -250,11 +246,7 @@ impl Runner { terminal.hide_cursor()?; // Threads - pwd_watcher::keep_watching( - app.pwd.as_ref(), - tx_msg_in.clone(), - rx_pwd_watcher, - )?; + pwd_watcher::keep_watching(app.pwd.as_ref(), tx_msg_in.clone(), rx_pwd_watcher)?; let mut event_reader = EventReader::new(tx_msg_in.clone()); event_reader.start(); @@ -328,9 +320,8 @@ impl Runner { explorer::explore_async( app.explorer_config.clone(), app.pwd.clone().into(), - app.focused_node().map(|n| { - n.relative_path.clone().into() - }), + app.focused_node() + .map(|n| n.relative_path.clone().into()), app.directory_buffer .as_ref() .map(|d| d.focus) @@ -344,9 +335,8 @@ impl Runner { explorer::explore_recursive_async( app.explorer_config.clone(), app.pwd.clone().into(), - app.focused_node().map(|n| { - n.relative_path.clone().into() - }), + app.focused_node() + .map(|n| n.relative_path.clone().into()), app.directory_buffer .as_ref() .map(|d| d.focus) @@ -361,11 +351,7 @@ impl Runner { let focus = app.focused_node(); if focus != last_focus.as_ref() { if let Some(ref mut file) = fifo { - writeln!( - file, - "{}", - app.focused_node_str() - )?; + writeln!(file, "{}", app.focused_node_str())?; }; last_focus = focus.cloned(); } @@ -376,16 +362,12 @@ impl Runner { // OSC 7: Change CWD if !(*ui::NO_COLOR) { - terminal - .backend_mut() - .write( - format!( - "\x1b]7;file://{}{}\x1b\\", - &app.hostname, &app.pwd - ) - .as_bytes(), - ) - .unwrap_or_default(); + write!( + terminal.backend_mut(), + "\x1b]7;file://{}{}\x1b\\", + &app.hostname, + &app.pwd + )?; } last_pwd = app.pwd.clone(); @@ -405,8 +387,7 @@ impl Runner { mouse_enabled = true; } Err(e) => { - app = - app.log_error(e.to_string())?; + app = app.log_error(e.to_string())?; } } } @@ -434,18 +415,14 @@ impl Runner { mouse_enabled = false; } Err(e) => { - app = - app.log_error(e.to_string())?; + app = app.log_error(e.to_string())?; } } } } StartFifo(path) => { - fifo = match start_fifo( - &path, - &app.focused_node_str(), - ) { + fifo = match start_fifo(&path, &app.focused_node_str()) { Ok(file) => Some(file), Err(e) => { app = app.log_error(e.to_string())?; @@ -466,25 +443,22 @@ impl Runner { fifo = None; std::mem::drop(file); } else { - fifo = match start_fifo( - &path, - &app.focused_node_str(), - ) { - Ok(file) => Some(file), - Err(e) => { - app = - app.log_error(e.to_string())?; - None + fifo = + match start_fifo(&path, &app.focused_node_str()) + { + Ok(file) => Some(file), + Err(e) => { + app = app.log_error(e.to_string())?; + None + } } - } } } CallLuaSilently(func) => { match call_lua_heavy(&app, &lua, &func, false) { Ok(Some(msgs)) => { - app = app - .handle_batch_external_msgs(msgs)?; + app = app.handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { @@ -510,8 +484,7 @@ impl Runner { match pipe::read_all(&app.pipe.msg_in) { Ok(msgs) => { - app = app - .handle_batch_external_msgs(msgs)?; + app = app.handle_batch_external_msgs(msgs)?; } Err(err) => { app = app.log_error(err.to_string())?; @@ -541,8 +514,7 @@ impl Runner { match call_lua_heavy(&app, &lua, &func, false) { Ok(Some(msgs)) => { - app = app - .handle_batch_external_msgs(msgs)?; + app = app.handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { @@ -564,8 +536,7 @@ impl Runner { mouse_enabled = true; } Err(e) => { - app = - app.log_error(e.to_string())?; + app = app.log_error(e.to_string())?; } } } @@ -591,9 +562,7 @@ impl Runner { match res { Ok(Value::Function(f)) => { let arg = app.to_lua_ctx_heavy(); - let res: Result< - Option>, - > = lua + let res: Result>> = lua .to_value(&arg) .and_then(|a| f.call(a)) .and_then(|v| lua.from_value(v)) @@ -601,36 +570,25 @@ impl Runner { match res { Ok(Some(msgs)) => { app = app - .handle_batch_external_msgs( - msgs, - )?; + .handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { - app = app.log_error( - err.to_string(), - )?; + app = app.log_error(err.to_string())?; } } } Ok(v) => { - let res: Result< - Option>, - > = lua - .from_value(v) - .map_err(Error::from); + let res: Result>> = + lua.from_value(v).map_err(Error::from); match res { Ok(Some(msgs)) => { app = app - .handle_batch_external_msgs( - msgs, - )?; + .handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { - app = app.log_error( - err.to_string(), - )?; + app = app.log_error(err.to_string())?; } } } @@ -653,8 +611,7 @@ impl Runner { mouse_enabled = true; } Err(e) => { - app = - app.log_error(e.to_string())?; + app = app.log_error(e.to_string())?; } } } @@ -667,9 +624,7 @@ impl Runner { match res { Ok(Value::Function(f)) => { let arg = app.to_lua_ctx_heavy(); - let res: Result< - Option>, - > = lua + let res: Result>> = lua .to_value(&arg) .and_then(|a| f.call(a)) .and_then(|v| lua.from_value(v)) @@ -677,36 +632,25 @@ impl Runner { match res { Ok(Some(msgs)) => { app = app - .handle_batch_external_msgs( - msgs, - )?; + .handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { - app = app.log_error( - err.to_string(), - )?; + app = app.log_error(err.to_string())?; } } } Ok(v) => { - let res: Result< - Option>, - > = lua - .from_value(v) - .map_err(Error::from); + let res: Result>> = + lua.from_value(v).map_err(Error::from); match res { Ok(Some(msgs)) => { app = app - .handle_batch_external_msgs( - msgs, - )?; + .handle_batch_external_msgs(msgs)?; } Ok(None) => {} Err(err) => { - app = app.log_error( - err.to_string(), - )?; + app = app.log_error(err.to_string())?; } } } @@ -747,8 +691,7 @@ impl Runner { // TODO remove duplicate segment match pipe::read_all(&app.pipe.msg_in) { Ok(msgs) => { - app = app - .handle_batch_external_msgs(msgs)?; + app = app.handle_batch_external_msgs(msgs)?; } Err(err) => { app = app.log_error(err.to_string())?; @@ -775,8 +718,7 @@ impl Runner { mouse_enabled = true; } Err(e) => { - app = - app.log_error(e.to_string())?; + app = app.log_error(e.to_string())?; } } } @@ -795,8 +737,7 @@ impl Runner { terminal.clear()?; terminal.set_cursor(0, 0)?; execute!(terminal.backend_mut(), term::LeaveAlternateScreen)?; - execute!(terminal.backend_mut(), event::DisableMouseCapture) - .unwrap_or_default(); + execute!(terminal.backend_mut(), event::DisableMouseCapture).unwrap_or_default(); term::disable_raw_mode()?; terminal.show_cursor()?; diff --git a/src/ui.rs b/src/ui.rs index 3d73cb0..2f8cd57 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -15,14 +15,12 @@ use std::env; use std::ops::BitXor; use tui::backend::Backend; use tui::layout::Rect as TuiRect; -use tui::layout::{ - Constraint as TuiConstraint, Direction, Layout as TuiLayout, -}; +use tui::layout::{Constraint as TuiConstraint, Direction, Layout as TuiLayout}; use tui::style::{Color, Modifier as TuiModifier, Style as TuiStyle}; use tui::text::{Span, Spans, Text}; use tui::widgets::{ - Block, BorderType as TuiBorderType, Borders as TuiBorders, Cell, List, - ListItem, Paragraph, Row, Table, + Block, BorderType as TuiBorderType, Borders as TuiBorders, Cell, List, ListItem, + Paragraph, Row, Table, }; use tui::Frame; @@ -43,8 +41,7 @@ fn string_to_text<'a>(string: String) -> Text<'a> { if *NO_COLOR { Text::raw(string) } else { - ansi_to_text(string.bytes()) - .unwrap_or_else(|e| Text::raw(format!("{:?}", e))) + ansi_to_text(string.bytes()).unwrap_or_else(|e| Text::raw(format!("{:?}", e))) } } @@ -67,8 +64,7 @@ pub struct LayoutOptions { impl LayoutOptions { pub fn extend(mut self, other: &Self) -> Self { self.margin = other.margin.or(self.margin); - self.horizontal_margin = - other.horizontal_margin.or(self.horizontal_margin); + self.horizontal_margin = other.horizontal_margin.or(self.horizontal_margin); self.vertical_margin = other.vertical_margin.or(self.vertical_margin); self.constraints = other.constraints.to_owned().or(self.constraints); self @@ -265,10 +261,8 @@ impl Style { pub fn extend(mut self, other: &Self) -> Self { self.fg = other.fg.or(self.fg); self.bg = other.bg.or(self.bg); - self.add_modifiers = - other.add_modifiers.to_owned().or(self.add_modifiers); - self.sub_modifiers = - other.sub_modifiers.to_owned().or(self.sub_modifiers); + self.add_modifiers = other.add_modifiers.to_owned().or(self.add_modifiers); + self.sub_modifiers = other.sub_modifiers.to_owned().or(self.sub_modifiers); self } } @@ -288,12 +282,8 @@ impl Into for Style { TuiStyle { fg: self.fg, bg: self.bg, - add_modifier: TuiModifier::from_bits_truncate(xor( - self.add_modifiers - )), - sub_modifier: TuiModifier::from_bits_truncate(xor( - self.sub_modifiers - )), + add_modifier: TuiModifier::from_bits_truncate(xor(self.add_modifiers)), + sub_modifier: TuiModifier::from_bits_truncate(xor(self.sub_modifiers)), } } } @@ -322,11 +312,7 @@ pub enum Constraint { } impl Constraint { - pub fn to_tui( - self, - screen_size: TuiRect, - layout_size: TuiRect, - ) -> TuiConstraint { + pub fn to_tui(self, screen_size: TuiRect, layout_size: TuiRect) -> TuiConstraint { match self { Self::Percentage(n) => TuiConstraint::Percentage(n), Self::Ratio(x, y) => TuiConstraint::Ratio(x, y), @@ -518,9 +504,8 @@ fn draw_table( let config = panel_config.default.to_owned().extend(&panel_config.table); let app_config = app.config.to_owned(); let header_height = app_config.general.table.header.height.unwrap_or(1); - let height: usize = (layout_size.height.max(header_height + 2) - - (header_height + 2)) - .into(); + let height: usize = + (layout_size.height.max(header_height + 2) - (header_height + 2)).into(); let rows = app .directory_buffer @@ -595,25 +580,18 @@ fn draw_table( let (relative_index, is_before_focus, is_after_focus) = match dir.focus.cmp(&index) { - Ordering::Greater => { - (dir.focus - index, true, false) - } + Ordering::Greater => (dir.focus - index, true, false), Ordering::Less => (index - dir.focus, false, true), Ordering::Equal => (0, false, false), }; let (mut prefix, mut suffix, mut style) = { let ui = app_config.general.default_ui.to_owned(); - ( - ui.prefix, - ui.suffix, - ui.style.extend(&node_type.style), - ) + (ui.prefix, ui.suffix, ui.style.extend(&node_type.style)) }; if is_focused && is_selected { - let ui = - app_config.general.focus_selection_ui.to_owned(); + let ui = app_config.general.focus_selection_ui.to_owned(); prefix = ui.prefix.to_owned().or(prefix); suffix = ui.suffix.to_owned().or(suffix); style = style.extend(&ui.style); @@ -688,9 +666,7 @@ fn draw_table( .widths(&table_constraints) .style(app_config.general.table.style.to_owned().into()) .highlight_style(app_config.general.focus_ui.style.to_owned().into()) - .column_spacing( - app_config.general.table.col_spacing.unwrap_or_default(), - ) + .column_spacing(app_config.general.table.col_spacing.unwrap_or_default()) .block(block( config, format!( @@ -777,8 +753,7 @@ fn draw_help_menu( if app.config.general.hide_remaps_in_help_menu { [Cell::from(k), Cell::from(h)].to_vec() } else { - [Cell::from(k), Cell::from(remaps.join("|")), Cell::from(h)] - .to_vec() + [Cell::from(k), Cell::from(remaps.join("|")), Cell::from(h)].to_vec() } }), }) @@ -857,9 +832,7 @@ fn draw_input_buffer( f.render_widget(input_buf, layout_size); f.set_cursor( // Put cursor past the end of the input text - layout_size.x - + (input.cursor() as u16).min(width) - + cursor_offset_left, + layout_size.x + (input.cursor() as u16).min(width) + cursor_offset_left, // Move one line down, from the border to the input line layout_size.y + 1, ); @@ -879,8 +852,7 @@ fn draw_sort_n_filter( .to_owned() .extend(&panel_config.sort_and_filter); let ui = app.config.general.sort_and_filter_ui.to_owned(); - let filter_by: &IndexSet = - &app.explorer_config.filters; + let filter_by: &IndexSet = &app.explorer_config.filters; let sort_by: &IndexSet = &app.explorer_config.sorters; let defaultui = &ui.default_identifier; let forwardui = defaultui @@ -977,11 +949,7 @@ fn draw_logs( app::LogLevel::Warning => ListItem::new(format!( "{} | {} | {}", &time, - &logs_config - .warning - .format - .to_owned() - .unwrap_or_default(), + &logs_config.warning.format.to_owned().unwrap_or_default(), l.message )) .style(logs_config.warning.style.to_owned().into()), @@ -989,11 +957,7 @@ fn draw_logs( app::LogLevel::Success => ListItem::new(format!( "{} | {} | {}", &time, - &logs_config - .success - .format - .to_owned() - .unwrap_or_default(), + &logs_config.success.format.to_owned().unwrap_or_default(), l.message )) .style(logs_config.success.style.to_owned().into()), @@ -1001,11 +965,7 @@ fn draw_logs( app::LogLevel::Error => ListItem::new(format!( "{} | {} | {}", &time, - &logs_config - .error - .format - .to_owned() - .unwrap_or_default(), + &logs_config.error.format.to_owned().unwrap_or_default(), l.message )) .style(logs_config.error.style.to_owned().into()), @@ -1070,8 +1030,7 @@ pub fn draw_custom_content( let render = lua::serialize(lua, &ctx) .map(|arg| { - lua::call(lua, &render, arg) - .unwrap_or_else(|e| format!("{:?}", e)) + lua::call(lua, &render, arg).unwrap_or_else(|e| format!("{:?}", e)) }) .unwrap_or_else(|e| e.to_string()); @@ -1243,12 +1202,8 @@ pub fn draw_layout( Layout::SortAndFilter => { draw_sort_n_filter(f, screen_size, layout_size, app, lua) } - Layout::HelpMenu => { - draw_help_menu(f, screen_size, layout_size, app, lua) - } - Layout::Selection => { - draw_selection(f, screen_size, layout_size, app, lua) - } + Layout::HelpMenu => draw_help_menu(f, screen_size, layout_size, app, lua), + Layout::Selection => draw_selection(f, screen_size, layout_size, app, lua), Layout::InputAndLogs => { if app.input.buffer.is_some() { draw_input_buffer(f, screen_size, layout_size, app, lua); @@ -1256,15 +1211,9 @@ pub fn draw_layout( draw_logs(f, screen_size, layout_size, app, lua); }; } - Layout::CustomContent { title, body } => draw_custom_content( - f, - screen_size, - layout_size, - app, - title, - body, - lua, - ), + Layout::CustomContent { title, body } => { + draw_custom_content(f, screen_size, layout_size, app, title, body, lua) + } Layout::Horizontal { config, splits } => { let chunks = TuiLayout::default() .direction(Direction::Horizontal) @@ -1284,18 +1233,16 @@ pub fn draw_layout( .unwrap_or_default(), ) .vertical_margin( - config - .vertical_margin - .or(config.margin) - .unwrap_or_default(), + config.vertical_margin.or(config.margin).unwrap_or_default(), ) .split(layout_size); - splits.into_iter().zip(chunks.into_iter()).for_each( - |(split, chunk)| { + splits + .into_iter() + .zip(chunks.into_iter()) + .for_each(|(split, chunk)| { draw_layout(split, f, screen_size, chunk, app, lua) - }, - ); + }); } Layout::Vertical { config, splits } => { @@ -1317,18 +1264,16 @@ pub fn draw_layout( .unwrap_or_default(), ) .vertical_margin( - config - .vertical_margin - .or(config.margin) - .unwrap_or_default(), + config.vertical_margin.or(config.margin).unwrap_or_default(), ) .split(layout_size); - splits.into_iter().zip(chunks.into_iter()).for_each( - |(split, chunk)| { + splits + .into_iter() + .zip(chunks.into_iter()) + .for_each(|(split, chunk)| { draw_layout(split, f, screen_size, chunk, app, lua) - }, - ); + }); } } }