qwandor / anes-rs

ANSI Escape Sequence provider & parser
https://crates.io/crates/anes
Apache License 2.0
15 stars 2 forks source link

Emitting mouse sequences? #21

Open eaglgenes101 opened 4 years ago

eaglgenes101 commented 4 years ago

Understandably, emitting escape sequences to generate terminal mouse sequences is rather niche, but is it within the scope of this project? I'm thinking of using it as part of a terminal multiplexer for forwarding mouse events to pseudoterminals.

zrzka commented 4 years ago

Thanks for the issue. To answer your question, it really depends on how we're looking at the anes crate.

I wrote this crate for the first use case and it doesn't make much sense to add support for this. It's also a bit complicated, see below.


There's no single ANSI escape sequence type for mouse events. We have:

Let's say we will implement Display for the Mouse enum:

https://github.com/zrzka/anes-rs/blob/378ac7461ecac29fde9e10df7b08359d1315a9ad/anes/src/parser/types.rs#L52-L65

Which variant should we emit? X10 compatibility mode? Xterm mode? Rxvt mode? Add special method just for the mouse to emit correct variant? We can introduce another enum like:

enum Mouse {
  Xterm(...),
  X10(...),
  Rxvt(...),
}

Then we will know what to emit, but we will complicate it for the Parser users - they want to know which mouse button was used & the location. They don't care about ANSI escape sequence type.


There's one thing you can do to forward mouse events. Skip the Parser and use Engine & Provide. Unfortunately, both these types are private, but I can make them public. Then you can implement your own provide_csi_sequence in this way:

impl Provider for MyType {
    fn provide_csi_sequence(&mut self, parameters: &[u64], ignored_count: usize, ch: char) {
        if ch == 'm' || ch == 'M' {
            // Mouse event - reconstruct ANSI sequence & forward it
        } else {
            // Not a mouse event
        }
    }
}

Not sure if I helped you. Thoughts?

eaglgenes101 commented 4 years ago

I think this makes sense. Thanks. The changes you floated will probably be helpful if they do come in a release, but I can make do without as well.

zrzka commented 4 years ago

@eaglgenes101 so for now, I'd recommend to copy & paste Engine & Provide to your code base. I'm still thinking how to expose these two types (directly via anes crate, move just these two types to a separate crate like anes-parser, ...). No decision made yet and I do not want to block you while thinking what would be the best option.