diff --git a/termwiz/src/escape/csi.rs b/termwiz/src/escape/csi.rs index a411742f4..36dd19846 100644 --- a/termwiz/src/escape/csi.rs +++ b/termwiz/src/escape/csi.rs @@ -1265,8 +1265,8 @@ impl ParseParams for OneBased { impl ParseParams for (OneBased, OneBased) { fn parse_params(params: &[CsiParam]) -> Result<(OneBased, OneBased), ()> { match params { - [] => Ok((OneBased::new(1), OneBased::new(1))), - [p] => Ok((OneBased::from_esc_param(p)?, OneBased::new(1))), + [] | [CsiParam::P(b';')] => Ok((OneBased::new(1), OneBased::new(1))), + [a] | [a, CsiParam::P(b';')] => Ok((OneBased::from_esc_param(a)?, OneBased::new(1))), [a, CsiParam::P(b';'), b] => { Ok((OneBased::from_esc_param(a)?, OneBased::from_esc_param(b)?)) } @@ -3010,8 +3010,23 @@ mod test { vec![CSI::Cursor(Cursor::Right(4))] ); - // Check that we default the second parameter of two - // when only one is provided + // Check permutations of optional parameters + assert_eq!( + parse('H', &[], "\x1b[1;1H"), + vec![CSI::Cursor(Cursor::Position { + line: OneBased::new(1), + col: OneBased::new(1) + })] + ); + let res: Vec<_> = CSI::parse(&[CsiParam::P(b';')], false, 'H').collect(); + assert_eq!(encode(&res), "\x1b[1;1H"); + assert_eq!( + res, + vec![CSI::Cursor(Cursor::Position { + line: OneBased::new(1), + col: OneBased::new(1) + })] + ); assert_eq!( parse('H', &[2], "\x1b[2;1H"), vec![CSI::Cursor(Cursor::Position { @@ -3019,6 +3034,33 @@ mod test { col: OneBased::new(1) })] ); + let res: Vec<_> = + CSI::parse(&[CsiParam::Integer(2), CsiParam::P(b';')], false, 'H').collect(); + assert_eq!(encode(&res), "\x1b[2;1H"); + assert_eq!( + res, + vec![CSI::Cursor(Cursor::Position { + line: OneBased::new(2), + col: OneBased::new(1) + })] + ); + let res: Vec<_> = + CSI::parse(&[CsiParam::P(b';'), CsiParam::Integer(2)], false, 'H').collect(); + assert_eq!(encode(&res), "\x1b[1;2H"); + assert_eq!( + res, + vec![CSI::Cursor(Cursor::Position { + line: OneBased::new(1), + col: OneBased::new(2) + })] + ); + assert_eq!( + parse('H', &[2, 3], "\x1b[2;3H"), + vec![CSI::Cursor(Cursor::Position { + line: OneBased::new(2), + col: OneBased::new(3) + })] + ); } #[test]