diff --git a/Cargo.toml b/Cargo.toml index 9f1cdd6f6..1a00ffbe0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,5 @@ shadow-rs = "0.26" [features] devicons = ["phf"] file_mimetype = [] -mouse = [] syntax_highlight = ["ansi-to-tui"] -default = ["devicons", "mouse", "syntax_highlight"] +default = ["devicons", "syntax_highlight"] diff --git a/config/joshuto.toml b/config/joshuto.toml index 4ab9cbc8f..d23da09cc 100644 --- a/config/joshuto.toml +++ b/config/joshuto.toml @@ -1,5 +1,6 @@ numbered_command = false +mouse_support = true focus_on_create = true use_trash = true watch_files = true diff --git a/docs/configuration/joshuto.toml.md b/docs/configuration/joshuto.toml.md index 7c90fc299..acec5bf65 100644 --- a/docs/configuration/joshuto.toml.md +++ b/docs/configuration/joshuto.toml.md @@ -5,6 +5,8 @@ This file is for general configurations. All options available and their default values: ```toml +# Enables mouse support (true by default) +mouse_support = true # This is for configuring how many items to reach before 'scrolling' the view scroll_offset = 6 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..9c60c54b1 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,15 @@ 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 +68,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 +82,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/config/clean/app/config.rs b/src/config/clean/app/config.rs index 60b088d8a..ee3a24b50 100644 --- a/src/config/clean/app/config.rs +++ b/src/config/clean/app/config.rs @@ -21,6 +21,7 @@ pub struct AppConfig { pub watch_files: bool, pub custom_commands: Vec, pub focus_on_create: bool, + pub mouse_support: bool, pub cmd_aliases: HashMap, pub _display_options: DisplayOption, pub _preview_options: PreviewOption, @@ -86,6 +87,7 @@ impl From for AppConfig { watch_files: raw.watch_files, cmd_aliases: raw.cmd_aliases, focus_on_create: raw.focus_on_create, + mouse_support: raw.mouse_support, _display_options: DisplayOption::from(raw.display_options), _preview_options: PreviewOption::from(raw.preview_options), _search_options: SearchOption::from(raw.search_options), diff --git a/src/config/raw/app/config.rs b/src/config/raw/app/config.rs index cf2b7586f..4ded404fc 100644 --- a/src/config/raw/app/config.rs +++ b/src/config/raw/app/config.rs @@ -34,6 +34,8 @@ pub struct AppConfigRaw { pub watch_files: bool, #[serde(default = "default_true")] pub focus_on_create: bool, + #[serde(default = "default_true")] + pub mouse_support: bool, #[serde(default)] pub cmd_aliases: HashMap, #[serde(default, rename = "display")] 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/ui/backend.rs b/src/ui/backend.rs index b5b9833d4..4b5b12448 100644 --- a/src/ui/backend.rs +++ b/src/ui/backend.rs @@ -6,7 +6,6 @@ use termion::raw::{IntoRawMode, RawTerminal}; use termion::screen::AlternateScreen; use termion::screen::IntoAlternateScreen; -#[cfg(feature = "mouse")] use termion::input::MouseTerminal; trait New { @@ -15,24 +14,38 @@ trait New { Self: Sized; } -#[cfg(feature = "mouse")] -type Screen = MouseTerminal>>; -#[cfg(feature = "mouse")] -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())) + } } } -#[cfg(not(feature = "mouse"))] -type Screen = AlternateScreen>; -#[cfg(not(feature = "mouse"))] -impl New for Screen { - // Returns alternate screen - fn new() -> io::Result { - let stdout = std::io::stdout().into_raw_mode()?; - Ok(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(), + } } } @@ -44,8 +57,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)?; @@ -80,8 +93,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(()) }