1
0
mirror of https://github.com/nushell/nushell.git synced 2025-07-08 10:27:47 +02:00

fix: fix cursor position when cursor is at the end of the commandline ()

# Description
Fix getting the cursor position, when it's at the end of the
commandline.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting

# After Submitting
This commit is contained in:
Steven Xu
2023-04-28 22:02:45 +10:00
committed by GitHub
parent 3076378373
commit b37662c7e1
2 changed files with 29 additions and 8 deletions
crates/nu-cli/src/commands
src/tests

@ -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,

@ -62,7 +62,27 @@ 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\