Skip to content

Commit

Permalink
Don't capture mouse on mouse_support = false
Browse files Browse the repository at this point in the history
  • Loading branch information
Mroik committed Feb 20, 2024
1 parent 2fda08c commit e48815d
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/commands/bulk_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/commands/custom_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down
10 changes: 5 additions & 5 deletions src/commands/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn fzf(
CaseSensitivity::Smart => {}
}

fzf_impl(backend, items, args)
fzf_impl(context, backend, items, args)
}

pub fn fzf_multi(
Expand All @@ -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<String>, args: Vec<String>) -> AppResult<String> {
fn fzf_impl(context: &mut AppContext, backend: &mut AppBackend, items: Vec<String>, args: Vec<String>) -> AppResult<String> {
backend.terminal_drop();

let mut cmd = Command::new("fzf");
Expand All @@ -63,7 +63,7 @@ fn fzf_impl(backend: &mut AppBackend, items: Vec<String>, args: Vec<String>) ->
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));
}
};
Expand All @@ -77,7 +77,7 @@ fn fzf_impl(backend: &mut AppBackend, items: Vec<String>, args: Vec<String>) ->
}

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() {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/open_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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?;
}
Expand Down Expand Up @@ -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?
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/sub_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
"{}: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/zoxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn run_main(args: Args) -> Result<i32, AppError> {

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)?;
Expand Down
4 changes: 0 additions & 4 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
40 changes: 32 additions & 8 deletions src/ui/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,36 @@ trait New {
Self: Sized;
}

type Screen = MouseTerminal<AlternateScreen<RawTerminal<std::io::Stdout>>>;
impl New for Screen {
pub enum Screen {
WithMouse(MouseTerminal<AlternateScreen<RawTerminal<std::io::Stdout>>>),
WithoutMouse(AlternateScreen<RawTerminal<std::io::Stdout>>),
}

impl Screen {
// Returns alternate screen
fn new() -> io::Result<Self> {
fn new(mouse_support: bool) -> io::Result<Self> {
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<usize> {
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(),
}
}
}

Expand All @@ -31,8 +55,8 @@ pub struct AppBackend {
}

impl AppBackend {
pub fn new() -> io::Result<Self> {
let mut alt_screen = Screen::new()?;
pub fn new(mouse_support: bool) -> io::Result<Self> {
let mut alt_screen = Screen::new(mouse_support)?;
// clears the screen of artifacts
write!(alt_screen, "{}", termion::clear::All)?;

Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit e48815d

Please sign in to comment.