diff --git a/crates/nu-cli/src/commands/commandline.rs b/crates/nu-cli/src/commands/commandline.rs index 597491917d..c394b62218 100644 --- a/crates/nu-cli/src/commands/commandline.rs +++ b/crates/nu-cli/src/commands/commandline.rs @@ -118,8 +118,9 @@ impl Command for Commandline { .expect("repl cursor pos mutex"); let char_pos = buffer .grapheme_indices(true) + .chain(std::iter::once((*cursor_pos, ""))) .position(|(i, _c)| i == *cursor_pos) - .unwrap_or(buffer.len()); + .expect("Cursor position isn't on a grapheme boundary"); Ok(Value::String { val: char_pos.to_string(), span: call.head, diff --git a/src/tests/test_commandline.rs b/src/tests/test_commandline.rs index 9f094f5b88..03db3fa921 100644 --- a/src/tests/test_commandline.rs +++ b/src/tests/test_commandline.rs @@ -47,14 +47,14 @@ fn commandline_test_replace() -> TestResult { fn commandline_test_cursor() -> TestResult { run_test( "commandline --replace '0👩‍❤️‍👩2'\n\ - commandline --cursor '1' \n\ + commandline --cursor '1'\n\ commandline --insert 'x'\n\ commandline", "0x👩‍❤️‍👩2", )?; run_test( "commandline --replace '0👩‍❤️‍👩2'\n\ - commandline --cursor '2' \n\ + commandline --cursor '2'\n\ commandline --insert 'x'\n\ commandline", "0👩‍❤️‍👩x2", @@ -62,16 +62,36 @@ fn commandline_test_cursor() -> TestResult { } #[test] -fn commandline_test_cursor_show_pos() -> TestResult { +fn commandline_test_cursor_show_pos_begin() -> TestResult { + run_test( + "commandline --replace '0👩‍❤️‍👩'\n\ + commandline --cursor '0'\n\ + commandline --cursor", + "0", + ) +} + +#[test] +fn commandline_test_cursor_show_pos_end() -> TestResult { + run_test( + "commandline --replace '0👩‍❤️‍👩'\n\ + commandline --cursor '2'\n\ + commandline --cursor", + "2", + ) +} + +#[test] +fn commandline_test_cursor_show_pos_mid() -> TestResult { run_test( "commandline --replace '0👩‍❤️‍👩2'\n\ - commandline --cursor '1' \n\ + commandline --cursor '1'\n\ commandline --cursor", "1", )?; run_test( "commandline --replace '0👩‍❤️‍👩2'\n\ - commandline --cursor '2' \n\ + commandline --cursor '2'\n\ commandline --cursor", "2", ) @@ -81,7 +101,7 @@ fn commandline_test_cursor_show_pos() -> TestResult { fn commandline_test_cursor_too_small() -> TestResult { run_test( "commandline --replace '123456'\n\ - commandline --cursor '-1' \n\ + commandline --cursor '-1'\n\ commandline --insert '0'\n\ commandline", "0123456", @@ -92,7 +112,7 @@ fn commandline_test_cursor_too_small() -> TestResult { fn commandline_test_cursor_too_large() -> TestResult { run_test( "commandline --replace '123456'\n\ - commandline --cursor '10' \n\ + commandline --cursor '10'\n\ commandline --insert '0'\n\ commandline", "1234560",