fn parse_input_sequence(bytes: &[u8]) -> (Event, usize) {
    if bytes.len() == 1 {
        return (match bytes[0] {
            27 => Event::Escape,
            9 => Event::Tab,
            32 => Event::Space,
            127 => Event::Backspace,
            b if b < 32 => Event::Press(char::from(b + 96), Modifiers::Ctrl),
            b => {
                let ch = char::from(b);
                if ch.is_ascii_uppercase() {
                    Event::Press(ch.to_ascii_lowercase(), Modifiers::Shift)
                } else {
                    Event::Press(ch, Modifiers::None)
                }
            }
        }, 1);
    }
    // all next patterns start with ESC, just handle it at the beginning
    if bytes[0] != 27 {
        fail!(bytes);
    }
    if bytes.len() == 2 {
        let b = bytes[1];
        if b < 32 {
            return (Event::Press(char::from(b + 96), Modifiers::CtrlAlt), 2);
        }
        let ch = char::from(b);
        return (if ch.is_ascii_uppercase() {
            Event::Press(ch.to_ascii_lowercase(), Modifiers::AltShift)
        } else {
            Event::Press(ch, Modifiers::Alt)
        }, 2);
    }
    // all other ones start with CSI (ESC+[)
    if bytes[1] != 91 {
        fail!(bytes);
    }
    let code = &bytes[2..];
    match code {
        [b @ 65..=68] => (Event::Arrow(parse_arrow(*b).unwrap(), Modifiers::None), 3),

        [70] => (Event::End, 3),
        [72] => (Event::Home, 3),

        [50, 126] => (Event::Insert, 4),
        [51, 126] => (Event::Delete, 4),
        [53, 126] => (Event::PgUp, 4),
        [54, 126] => (Event::PgDown, 4),

        [49, 59, mods @ 50..=56, arrow] => {
            match parse_arrow(*arrow) {
                Some(arrow) => (Event::Arrow(arrow, parse_mods(*mods).unwrap()), 6),
                _ => fail!(bytes)
            }
        }

        _ if code.len() > 1 && code[0] == 60 => {
            let ([b, x, y], read) = read_params(&code[1..], 109, 77);
            if b == 1000 || x == 1000 || y == 1000 || x == 0 || y == 0 {
                fail!(bytes);
            }
            let pos = Position { x: x - 1, y: y - 1 };
            let mods = match (b & 0b11100) >> 2 {
                0b000 => Modifiers::None,
                0b001 => Modifiers::Shift,
                0b010 => Modifiers::Alt,
                0b011 => Modifiers::AltShift,
                0b100 => Modifiers::Ctrl,
                0b101 => Modifiers::CtrlShift,
                0b110 => Modifiers::CtrlAlt,
                0b111 => Modifiers::CtrlShiftAlt,
                _ => unreachable!(),
            };
            if b & 0b1000000 != 0 { // wheel bit
                let dir = if b & 0b1 == 0 { MouseWheelDirection::Up } else { MouseWheelDirection::Down };
                return (Event::MouseWheel(dir, pos, mods), read + 3);
            }
            let action = if code[read] == 109 {
                MouseAction::Release
            } else if b & 0b100000 != 0 { // drag bit
                MouseAction::Drag
            } else {
                MouseAction::Press
            };
            let button = match b & 0b11 {
                0b00 => MouseButton::Left,
                0b01 => MouseButton::Middle,
                0b10 => MouseButton::Right,
                0b11 => return (Event::MouseMotion(pos, mods), read + 3),
                _ => unreachable!(),
            };
            return (Event::Mouse(action, button, pos, mods), read + 3);
        }
        _ => fail!(bytes)
    }
}
/// This method tries to parse a sequence of bytes into an event.
/// It returns an event, and a number of consumed bytes.
/// If it fails to parse the event, it returns `Event::UnknownByteSequence`
/// with all the bytes received, and the length of the given slice
This method tries to parse a sequence of bytes into an event.
It returns an event, and a number of consumed bytes.
If it fails to parse the event, it returns `Event::UnknownByteSequence`
with all the bytes received, and the length of the given slice
This method tries to parse a sequence of bytes into an event . It returns an event and a number of consumed bytes . If it fails to parse the event it returns ` Event :: UnknownByteSequence ` with all the bytes received and the length of the given slice
fn parse_input_sequence(bytes: &[u8]) -> (Event, usize) {
    if bytes.len() == 1 {
        return (match bytes[0] {
            27 => Event::Escape,
            9 => Event::Tab,
            32 => Event::Space,
            127 => Event::Backspace,
            b if b < 32 => Event::Press(char::from(b + 96), Modifiers::Ctrl),
            b => {
                let ch = char::from(b);
                if ch.is_ascii_uppercase() {
                    Event::Press(ch.to_ascii_lowercase(), Modifiers::Shift)
                } else {
                    Event::Press(ch, Modifiers::None)
                }
            }
        }, 1);
    }
    // all next patterns start with ESC, just handle it at the beginning
    if bytes[0] != 27 {
        fail!(bytes);
    }
    if bytes.len() == 2 {
        let b = bytes[1];
        if b < 32 {
            return (Event::Press(char::from(b + 96), Modifiers::CtrlAlt), 2);
        }
        let ch = char::from(b);
        return (if ch.is_ascii_uppercase() {
            Event::Press(ch.to_ascii_lowercase(), Modifiers::AltShift)
        } else {
            Event::Press(ch, Modifiers::Alt)
        }, 2);
    }
    // all other ones start with CSI (ESC+[)
    if bytes[1] != 91 {
        fail!(bytes);
    }
    let code = &bytes[2..];
    match code {
        [b @ 65..=68] => (Event::Arrow(parse_arrow(*b).unwrap(), Modifiers::None), 3),
        [70] => (Event::End, 3),
        [72] => (Event::Home, 3),
        [50, 126] => (Event::Insert, 4),
        [51, 126] => (Event::Delete, 4),
        [53, 126] => (Event::PgUp, 4),
        [54, 126] => (Event::PgDown, 4),
        [49, 59, mods @ 50..=56, arrow] => {
            match parse_arrow(*arrow) {
                Some(arrow) => (Event::Arrow(arrow, parse_mods(*mods).unwrap()), 6),
                _ => fail!(bytes)
            }
        }
        _ if code.len() > 1 && code[0] == 60 => {
            let ([b, x, y], read) = read_params(&code[1..], 109, 77);
            if b == 1000 || x == 1000 || y == 1000 || x == 0 || y == 0 {
                fail!(bytes);
            }
            let pos = Position { x: x - 1, y: y - 1 };
            let mods = match (b & 0b11100) >> 2 {
                0b000 => Modifiers::None,
                0b001 => Modifiers::Shift,
                0b010 => Modifiers::Alt,
                0b011 => Modifiers::AltShift,
                0b100 => Modifiers::Ctrl,
                0b101 => Modifiers::CtrlShift,
                0b110 => Modifiers::CtrlAlt,
                0b111 => Modifiers::CtrlShiftAlt,
                _ => unreachable!(),
            };
            if b & 0b1000000 != 0 { // wheel bit
                let dir = if b & 0b1 == 0 { MouseWheelDirection::Up } else { MouseWheelDirection::Down };
                return (Event::MouseWheel(dir, pos, mods), read + 3);
            }
            let action = if code[read] == 109 {
                MouseAction::Release
            } else if b & 0b100000 != 0 { // drag bit
                MouseAction::Drag
            } else {
                MouseAction::Press
            };
            let button = match b & 0b11 {
                0b00 => MouseButton::Left,
                0b01 => MouseButton::Middle,
                0b10 => MouseButton::Right,
                0b11 => return (Event::MouseMotion(pos, mods), read + 3),
                _ => unreachable!(),
            };
            return (Event::Mouse(action, button, pos, mods), read + 3);
        }
        _ => fail!(bytes)
    }
}
fn parse_input_sequence ( bytes : & [ u8 ] ) -> ( Event , usize ) { if bytes . len ( ) == 1 { return ( match bytes [ 0 ] { 27 => Event :: Escape , 9 => Event :: Tab , 32 => Event :: Space , 127 => Event :: Backspace , b if b < 32 => Event :: Press ( char :: from ( b + 96 ) , Modifiers :: Ctrl ) , b => { let ch = char :: from ( b ) ; if ch . is_ascii_uppercase ( ) { Event :: Press ( ch . to_ascii_lowercase ( ) , Modifiers :: Shift ) } else { Event :: Press ( ch , Modifiers :: None ) } } } , 1 ) ; } // all next patterns start with ESC, just handle it at the beginning if bytes [ 0 ] != 27 { fail ! ( bytes ) ; } if bytes . len ( ) == 2 { let b = bytes [ 1 ] ; if b < 32 { return ( Event :: Press ( char :: from ( b + 96 ) , Modifiers :: CtrlAlt ) , 2 ) ; } let ch = char :: from ( b ) ; return ( if ch . is_ascii_uppercase ( ) { Event :: Press ( ch . to_ascii_lowercase ( ) , Modifiers :: AltShift ) } else { Event :: Press ( ch , Modifiers :: Alt ) } , 2 ) ; } // all other ones start with CSI (ESC+[) if bytes [ 1 ] != 91 { fail ! ( bytes ) ; } let code = & bytes [ 2 .. ] ; match code { [ b @ 65 ..= 68 ] => ( Event :: Arrow ( parse_arrow ( * b ) . unwrap ( ) , Modifiers :: None ) , 3 ) , [ 70 ] => ( Event :: End , 3 ) , [ 72 ] => ( Event :: Home , 3 ) , [ 50 , 126 ] => ( Event :: Insert , 4 ) , [ 51 , 126 ] => ( Event :: Delete , 4 ) , [ 53 , 126 ] => ( Event :: PgUp , 4 ) , [ 54 , 126 ] => ( Event :: PgDown , 4 ) , [ 49 , 59 , mods @ 50 ..= 56 , arrow ] => { match parse_arrow ( * arrow ) { Some ( arrow ) => ( Event :: Arrow ( arrow , parse_mods ( * mods ) . unwrap ( ) ) , 6 ) , _ => fail ! ( bytes ) } } _ if code . len ( ) > 1 && code [ 0 ] == 60 => { let ( [ b , x , y ] , read ) = read_params ( & code [ 1 .. ] , 109 , 77 ) ; if b == 1000 || x == 1000 || y == 1000 || x == 0 || y == 0 { fail ! ( bytes ) ; } let pos = Position { x : x - 1 , y : y - 1 } ; let mods = match ( b & 0b11100 ) >> 2 { 0b000 => Modifiers :: None , 0b001 => Modifiers :: Shift , 0b010 => Modifiers :: Alt , 0b011 => Modifiers :: AltShift , 0b100 => Modifiers :: Ctrl , 0b101 => Modifiers :: CtrlShift , 0b110 => Modifiers :: CtrlAlt , 0b111 => Modifiers :: CtrlShiftAlt , _ => unreachable ! ( ) , } ; if b & 0b1000000 != 0 { // wheel bit let dir = if b & 0b1 == 0 { MouseWheelDirection :: Up } else { MouseWheelDirection :: Down } ; return ( Event :: MouseWheel ( dir , pos , mods ) , read + 3 ) ; } let action = if code [ read ] == 109 { MouseAction :: Release } else if b & 0b100000 != 0 { // drag bit MouseAction :: Drag } else { MouseAction :: Press } ; let button = match b & 0b11 { 0b00 => MouseButton :: Left , 0b01 => MouseButton :: Middle , 0b10 => MouseButton :: Right , 0b11 => return ( Event :: MouseMotion ( pos , mods ) , read + 3 ) , _ => unreachable ! ( ) , } ; return ( Event :: Mouse ( action , button , pos , mods ) , read + 3 ) ; } _ => fail ! ( bytes ) } }
This method tries to parse a sequence of bytes into an event.
This method tries to parse a sequence of bytes into an event .
bytes
&[u8]
bytes
&[u8]