From e48815d1d3b015f361ff1399e1c2be6f0785cb6f Mon Sep 17 00:00:00 2001 From: Mroik Date: Wed, 21 Feb 2024 00:00:08 +0100 Subject: [PATCH] Don't capture mouse on mouse_support = false --- src/commands/bulk_rename.rs | 2 +- src/commands/custom_search.rs | 2 +- src/commands/fzf.rs | 10 ++++----- src/commands/open_file.rs | 6 +++--- src/commands/sub_process.rs | 2 +- src/commands/zoxide.rs | 2 +- src/main.rs | 2 +- src/run.rs | 4 ---- src/ui/backend.rs | 40 ++++++++++++++++++++++++++++------- 9 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index f7009aee3..9cc7f265b 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -128,7 +128,7 @@ pub fn bulk_rename(context: &mut AppContext, backend: &mut AppBackend) -> AppRes context.remove_external_preview(); backend.terminal_drop(); let res = _bulk_rename(context); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; reload::soft_reload_curr_tab(context)?; res } diff --git a/src/commands/custom_search.rs b/src/commands/custom_search.rs index 422143e21..a9d440588 100644 --- a/src/commands/custom_search.rs +++ b/src/commands/custom_search.rs @@ -57,7 +57,7 @@ pub fn custom_search( .stdout(Stdio::piped()) .spawn()? .wait_with_output()?; - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; cmd_result } else { cmd.output()? diff --git a/src/commands/fzf.rs b/src/commands/fzf.rs index 8aeaa1ba6..451bdca93 100644 --- a/src/commands/fzf.rs +++ b/src/commands/fzf.rs @@ -25,7 +25,7 @@ pub fn fzf( CaseSensitivity::Smart => {} } - fzf_impl(backend, items, args) + fzf_impl(context, backend, items, args) } pub fn fzf_multi( @@ -47,10 +47,10 @@ pub fn fzf_multi( } args.push("-m".to_owned()); - fzf_impl(backend, items, args) + fzf_impl(context, backend, items, args) } -fn fzf_impl(backend: &mut AppBackend, items: Vec, args: Vec) -> AppResult { +fn fzf_impl(context: &mut AppContext, backend: &mut AppBackend, items: Vec, args: Vec) -> AppResult { backend.terminal_drop(); let mut cmd = Command::new("fzf"); @@ -63,7 +63,7 @@ fn fzf_impl(backend: &mut AppBackend, items: Vec, args: Vec) -> let mut fzf = match cmd.spawn() { Ok(child) => child, Err(e) => { - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; return Err(AppError::from(e)); } }; @@ -77,7 +77,7 @@ fn fzf_impl(backend: &mut AppBackend, items: Vec, args: Vec) -> } let fzf_output = fzf.wait_with_output(); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; if let Ok(output) = fzf_output { if output.status.success() { diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 0d248e914..d206f3565 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -60,7 +60,7 @@ where } else { backend.terminal_drop(); let res = execute_and_wait(option, files); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; res?; } Ok(()) @@ -79,7 +79,7 @@ fn _open_with_xdg( backend.terminal_drop(); let handle = open::that_in_background(path); let result = handle.join(); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; if let Ok(result) = result { result?; } @@ -136,7 +136,7 @@ where let mut option = ProgramEntry::new(String::from(cmd)); option.args(args_iter); let res = execute_and_wait(&option, files); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; res? } } diff --git a/src/commands/sub_process.rs b/src/commands/sub_process.rs index 437112eb3..58bfdc1bb 100644 --- a/src/commands/sub_process.rs +++ b/src/commands/sub_process.rs @@ -81,7 +81,7 @@ pub fn sub_process( ) -> AppResult { backend.terminal_drop(); let res = execute_sub_process(context, words, spawn); - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; let _ = reload::soft_reload_curr_tab(context); context.message_queue_mut().push_info(format!( "{}: {}", diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs index 222bb9d1c..a0651177a 100644 --- a/src/commands/zoxide.rs +++ b/src/commands/zoxide.rs @@ -49,7 +49,7 @@ pub fn zoxide_query_interactive(context: &mut AppContext, backend: &mut AppBacke .spawn()?; let zoxide_output = zoxide_process.wait_with_output()?; - backend.terminal_restore()?; + backend.terminal_restore(context.config_ref().mouse_support)?; if zoxide_output.status.success() { if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { diff --git a/src/main.rs b/src/main.rs index 7a5d44d68..02ea567a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -169,7 +169,7 @@ fn run_main(args: Args) -> Result { let mut context = AppContext::new(config, args.clone()); { - let mut backend: ui::AppBackend = ui::AppBackend::new()?; + let mut backend: ui::AppBackend = ui::AppBackend::new(context.config_ref().mouse_support)?; run::run_loop(&mut backend, &mut context, keymap)?; } run_quit(&args, &context)?; diff --git a/src/run.rs b/src/run.rs index 1ad2f5add..958fe804a 100644 --- a/src/run.rs +++ b/src/run.rs @@ -83,10 +83,6 @@ fn process_input( // handle the event match event { AppEvent::Termion(Event::Mouse(event)) => { - if !context.config_ref().mouse_support { - context.flush_event(); - return; - } process_event::process_mouse(event, context, backend, keymap_t); preview_default::load_preview(context, backend); } diff --git a/src/ui/backend.rs b/src/ui/backend.rs index ee8dc20e0..8806bcbae 100644 --- a/src/ui/backend.rs +++ b/src/ui/backend.rs @@ -14,12 +14,36 @@ trait New { Self: Sized; } -type Screen = MouseTerminal>>; -impl New for Screen { +pub enum Screen { + WithMouse(MouseTerminal>>), + WithoutMouse(AlternateScreen>), +} + +impl Screen { // Returns alternate screen - fn new() -> io::Result { + fn new(mouse_support: bool) -> io::Result { let stdout = io::stdout().into_raw_mode()?; - Ok(MouseTerminal::from(stdout.into_alternate_screen().unwrap())) + if mouse_support { + Ok(Self::WithMouse(MouseTerminal::from(stdout.into_alternate_screen().unwrap()))) + } else { + Ok(Self::WithoutMouse(stdout.into_alternate_screen().unwrap())) + } + } +} + +impl Write for Screen { + fn write(&mut self, buf: &[u8]) -> io::Result { + match self { + Screen::WithMouse(t) => t.write(buf), + Screen::WithoutMouse(t) => t.write(buf), + } + } + + fn flush(&mut self) -> io::Result<()> { + match self { + Screen::WithMouse(t) => t.flush(), + Screen::WithoutMouse(t) => t.flush(), + } } } @@ -31,8 +55,8 @@ pub struct AppBackend { } impl AppBackend { - pub fn new() -> io::Result { - let mut alt_screen = Screen::new()?; + pub fn new(mouse_support: bool) -> io::Result { + let mut alt_screen = Screen::new(mouse_support)?; // clears the screen of artifacts write!(alt_screen, "{}", termion::clear::All)?; @@ -67,8 +91,8 @@ impl AppBackend { let _ = stdout().flush(); } - pub fn terminal_restore(&mut self) -> io::Result<()> { - let mut new_backend = Self::new()?; + pub fn terminal_restore(&mut self, mouse_support: bool) -> io::Result<()> { + let mut new_backend = Self::new(mouse_support)?; std::mem::swap(&mut self.terminal, &mut new_backend.terminal); Ok(()) }