Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vtparse] SS3 sequence #4463

Closed
gwenn opened this issue Oct 19, 2023 · 7 comments
Closed

[vtparse] SS3 sequence #4463

gwenn opened this issue Oct 19, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@gwenn
Copy link

gwenn commented Oct 19, 2023

On MacOS, F1 key is reported as ESC-O-P ([27, 79, 80]) sequence.
But vtparse reports two events:

use vtparse::{CsiParam, VTActor, VTParser};

struct Actor;

impl VTActor for Actor {
    fn print(&mut self, b: char) {
        println!("[print] {:?}", b);
    }
    fn execute_c0_or_c1(&mut self, control: u8) {
        println!("[execute_c0_or_c1] {:02x}", control);
    }

    fn dcs_hook(
        &mut self,
        mode: u8,
        params: &[i64],
        intermediates: &[u8],
        ignored_excess_intermediates: bool,
    ) {
        println!(
            "[dcs_hook] mode={:?}, params={:?}, intermediates={:?}, ignored_excess_intermediates={:?}",
            mode, params, intermediates, ignored_excess_intermediates
        );
    }
    fn dcs_put(&mut self, byte: u8) {
        println!("[dcs_put] {:02x}", byte);
    }
    fn dcs_unhook(&mut self) {
        println!("[dcs_unhook]");
    }

    fn esc_dispatch(
        &mut self,
        params: &[i64],
        intermediates: &[u8],
        ignored_excess_intermediates: bool,
        byte: u8,
    ) {
        println!(
            "[esc_dispatch] params={:?}, intermediates={:?}, ignored_excess_intermediates={:?}, byte={:02x}",
            params, intermediates, ignored_excess_intermediates, byte
        );
    }

    fn csi_dispatch(
        &mut self,
        params: &[CsiParam],
        parameters_truncated: bool,
        byte: u8,
    ) {
        println!(
            "[csi_dispatch] params={:#?}, parameters_truncated={:?}, byte={:?}",
            params, parameters_truncated, byte
        );
    }

    fn osc_dispatch(&mut self, params: &[&[u8]]) {
        println!("[osc_dispatch] params={:?}", params);
    }

    fn apc_dispatch(&mut self, data: Vec<u8>) {
        println!("[apc_dispatch] data={:?}", data);
    }
}

fn main() {
    let mut parser = VTParser::new();
    let mut actor = Actor {};
    let bytes = &[27u8, 79u8, 80u8]; // ESC-O-P
    parser.parse(&bytes[..], &mut actor);
}
$ cargo run 
[esc_dispatch] params=[], intermediates=[], ignored_excess_intermediates=false, byte=4f
[print] 'P'

Could vtparse be extended to report a single event ?
Thanks.

https://en.wikipedia.org/wiki/C0_and_C1_control_codes#C1_control_codes_for_general_use

Next character invokes a graphic character from the G2 or G3 graphic sets respectively.

See alacritty/vte#101

@gwenn gwenn added the enhancement New feature or request label Oct 19, 2023
@wez
Copy link
Owner

wez commented Nov 4, 2023

I'm not sure about this; it feels ambiguous and tricky to handle correctly at the vtparse layer.
Have you tried the higher level termwiz InputParser with this sequence?

@wez wez added the waiting-on-op Waiting for more information from the original poster label Nov 4, 2023
@gwenn
Copy link
Author

gwenn commented Nov 4, 2023

Have you tried the higher level termwiz InputParser with this sequence?

I would like a library with minimal dependencies.
So not termiz.
And not crossterm event which depends on mio.

Would you mind explaining why termwiz doesn't use vtparse but InputParser ?

@github-actions github-actions bot removed the waiting-on-op Waiting for more information from the original poster label Nov 4, 2023
@wez
Copy link
Owner

wez commented Nov 4, 2023

vtparse is targeted at a low level basic parse/categorization of escape sequences.
termwiz has higher level parsers that ascribe semantic meaning to those escape sequences.

Your use case sounds as though it is higher level than vtparse.

@gwenn
Copy link
Author

gwenn commented Nov 4, 2023

In termwiz, when I run cargo run --example line_editor, it seems that vtparse is not used (no debug print with patch below).
Am I wrong ?

diff --git a/termwiz/src/escape/parser/mod.rs b/termwiz/src/escape/parser/mod.rs
index 22047668d..98e455765 100644
--- a/termwiz/src/escape/parser/mod.rs
+++ b/termwiz/src/escape/parser/mod.rs
@@ -200,10 +200,12 @@ fn is_short_dcs(intermediates: &[u8], byte: u8) -> bool {

 impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {
     fn print(&mut self, c: char) {
+        println!("print({:?})",c);
         (self.callback)(Action::Print(c));
     }

     fn execute_c0_or_c1(&mut self, byte: u8) {
+        println!("execute_c0_or_c1({:?})", byte as char);
         match FromPrimitive::from_u8(byte) {
             Some(code) => (self.callback)(Action::Control(code)),
             None => error!(

@wez
Copy link
Owner

wez commented Nov 4, 2023

it is used. However, mixing debug prints with other tui stuff can be challenging: the line editor may erase and re-print over the top of that stuff, for example.

@gwenn
Copy link
Author

gwenn commented Nov 5, 2023

vtparse is targeted at a low level basic parse/categorization of escape sequences.

Ok, I've just found the logic here:

wezterm/termwiz/src/input.rs

Lines 1353 to 1356 in d680953

// This feels a bit gross because we have two different parsers at play
// here. We want to re-use the escape sequence parser to crack the
// parameters out from things like mouse reports. The keymap tree doesn't
// know how to grok this.

For SS3, the vtparse result is ignored and self.key_map is used directly.
Thanks.

@gwenn gwenn closed this as completed Nov 5, 2023
Copy link
Contributor

github-actions bot commented Dec 6, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants