crossterm-rs / crossterm

Cross platform terminal library rust
MIT License
3.27k stars 280 forks source link

Crossterm panics when scrolled on the left side #590

Open dsseng opened 3 years ago

dsseng commented 3 years ago

Describe the bug Crash when scrolling happens on the left of the terminal window. See video

Screencast from 08-08-2021 09 36 05 AM

To Reproduce Steps to reproduce the behavior:

  1. Start code from event-read example
  2. Move mouse to the left of the screen (terminal is maximized)
  3. Scroll either up or down
  4. Panic with the following backtrace:
thread 'main' panicked at 'attempt to subtract with overflow', /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/sys/unix/parse.rs:355:14
stack backtrace:
0: rust_begin_unwind at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:515:5
1: core::panicking::panic_fmt at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:92:14
2: core::panicking::panic at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:50:5
3: crossterm::event::sys::unix::parse::parse_csi_sgr_mouse at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/sys/unix/parse.rs:355:14
4: crossterm::event::sys::unix::parse::parse_csi at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/sys/unix/parse.rs:147:24
5: crossterm::event::sys::unix::parse::parse_event at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/sys/unix/parse.rs:55:29
6: crossterm::event::source::unix::Parser::advance at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/source/unix.rs:216:19
7: <crossterm::event::source::unix::UnixInternalEventSource as crossterm::event::source::EventSource>::try_read at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/source/unix.rs:106:41
8: crossterm::event::read::InternalEventReader::poll at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/read.rs:64:37
9: crossterm::event::read::InternalEventReader::read at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event/read.rs:122:21
10: crossterm::event::read_internal at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event.rs:232:5
11: crossterm::event::read at /tmp/cargo/registry/src/github.com-1ecc6299db9ec823/crossterm-0.20.0/src/event.rs:201:11
12: crossterm_test::print_events at /tmp/mozilla_sh7dm0/crossterm-test/src/main.rs:26:21
13: crossterm_test::main at /tmp/mozilla_sh7dm0/crossterm-test/src/main.rs:72:21
14: core::ops::function::FnOnce::call_once at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Expected behavior No panic when cursor is leftmost

OS

Terminal/Console

sigmaSd commented 3 years ago

I can reproduce with gnome terminal, but not with alacrity.

I think this subtraction https://github.com/crossterm-rs/crossterm/blob/master/src/event/sys/unix/parse.rs#L355 is not mentioned by the sgr extension.

wezterm for example doesn't appear to do it

But the spec says http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking

The upper left character position on the terminal is denoted as 1,1.

So maybe gnome-terminal is wrong here by sending 0

sigmaSd commented 3 years ago

This issue has some insight https://gitlab.gnome.org/GNOME/vte/-/issues/71

So from what I can tell 0 is a valid number, but its interpretation its not clear, it can be either scroll 1 or noop.

I think crossterm should just pick one behavior (maybe just consider 0 as a scroll by 1 request)