crossterm-rs / terminal

Unified API for different terminal manipulation libraries.
59 stars 2 forks source link

Unable to Read Mouse Events on iTerm2 #13

Closed bhgomes closed 3 years ago

bhgomes commented 4 years ago

I've been trying to read mouse events through the terminal library but have been unable to do so on iTerm2, but have been able to on the native macOS Terminal application. Other than the following snippet, the mouse works correctly in NeoVim and in the shell for scrolling.

// rust version 1.45.2
// terminal version 0.2.1 with crossterm backend

use terminal::{error, Action, Event, Retrieved, Value};

pub fn main() -> error::Result<()> {
    let term = terminal::stdout();
    term.act(Action::EnableMouseCapture)?;
    term.act(Action::EnableRawMode)?;
    loop {
        match term.get(Value::Event(None)) {
            Ok(Retrieved::Event(Some(event))) => match event {
                Event::Key(_) => return Ok(()),
                Event::Mouse(_) => return Ok(()),
                _ => {}
            },
            _ => {}
        }
    }
}

The mouse events never get registered, and so a keypress is required to exit the program. (This fails for DisableRawMode and DisableMouseCapture as well).

In searching for a solution, I looked at the source which seems to incorrectly map the mouse features:

https://github.com/crossterm-rs/terminal/blob/8ee61631d01da0d1028fb2c88c5977652e88548d/src/backend/crossterm/mapping.rs#L86-L106

I don't think this is the source of the problem, but these seem to be flipped.

TimonPost commented 4 years ago

And if you try another backend? Maybe this issue can be forwarded to crossterm.

bhgomes commented 4 years ago

I can't get any other backends to work, the program won't compile:

error[E0252]: the name `BackendImpl` is defined multiple times
 --> /Users/brandongomes/.local/src/cargo/registry/src/github.com-1ecc6299db9ec823/terminal-0.2.1/src/backend/mod.rs:8:16
  |
6 | pub(crate) use self::crosscurses::BackendImpl;
  |                ------------------------------ previous import of the type `BackendImpl` here
7 | #[cfg(feature = "crossterm-backend")]
8 | pub(crate) use self::crossterm::BackendImpl;
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `BackendImpl` reimported here
  |
  = note: `BackendImpl` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
  |
8 | pub(crate) use self::crossterm::BackendImpl as OtherBackendImpl;
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimonPost commented 4 years ago

If you switch from backend make sure to disable all feature flags and then only run the crosscurses feature flag.

bhgomes commented 4 years ago

All the feature flags are disabled except for crosscurses.

bhgomes commented 4 years ago

It looks like despite crosscurses-backend being the only feature flag in my Cargo.toml

[dependencies]
terminal = { version = "0.2.1", features = ["crosscurses-backend"] }

it is still compiling with both features enabled. This is the command that cargo build --verbose says its running:

rustc
--crate-name terminal
--edition=2018
/Users/brandongomes/.local/src/cargo/registry/src/github.com-1ecc6299db9ec823/terminal-0.2.1/src/lib.rs
--error-format=json
--json=diagnostic-rendered-ansi,artifacts
--crate-type lib
--emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2
--cfg 'feature="crosscurses"'
--cfg 'feature="crosscurses-backend"'
--cfg 'feature="crossterm"'
--cfg 'feature="crossterm-backend"'
--cfg 'feature="default"'
--cfg 'feature="libc"'
-C metadata=f155b762e226b833
-C extra-filename=-f155b762e226b833
--out-dir ***/target/debug/deps
-L dependency=***/target/debug/deps
--extern bitflags=***/target/debug/deps/libbitflags-1501d714e84ec914.rmeta
--extern crosscurses=***/target/debug/deps/libcrosscurses-b0c57750b2c585e9.rmeta
--extern crossterm=***/target/debug/deps/libcrossterm-4e3cfc2cc70486af.rmeta
--extern libc=***/target/debug/deps/liblibc-d0de0fb04cdda23e.rmeta
--cap-lints allow
-L native=/usr/lib

@TimonPost Maybe this has something to do with the automatic enabling of the crossterm-backend feature.

TimonPost commented 4 years ago

When I run

cargo run --example event --no-default-features --features crossterm-backend

It seems to work, can you try this like:

cargo run --example event --no-default-features --features crosscurses-backend
stoat1 commented 3 years ago

I was able to disable crossterm-backend feature

[dependencies.terminal]
version = "0.2"
default-features = false
features = ["termion-backend"]