From a20b24a7120be4628a8531162bc30d19c7177113 Mon Sep 17 00:00:00 2001 From: KITAGAWA Yasutaka Date: Thu, 15 Feb 2024 23:17:38 +0900 Subject: [PATCH] Fix commandline --cursor to return int (#11864) # Description Fix #11825 # User-Facing Changes `commandline --cursor` returns int. # Tests + Formatting # After Submitting --- crates/nu-cli/src/commands/commandline.rs | 12 +++++++++++- src/tests/test_commandline.rs | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/commands/commandline.rs b/crates/nu-cli/src/commands/commandline.rs index 52b8020632..e160b68dce 100644 --- a/crates/nu-cli/src/commands/commandline.rs +++ b/crates/nu-cli/src/commands/commandline.rs @@ -19,6 +19,7 @@ impl Command for Commandline { .input_output_types(vec![ (Type::Nothing, Type::Nothing), (Type::String, Type::String), + (Type::String, Type::Int), ]) .switch( "cursor", @@ -120,7 +121,16 @@ impl Command for Commandline { .chain(std::iter::once((repl.buffer.len(), ""))) .position(|(i, _c)| i == repl.cursor_pos) .expect("Cursor position isn't on a grapheme boundary"); - Ok(Value::string(char_pos.to_string(), call.head).into_pipeline_data()) + match i64::try_from(char_pos) { + Ok(pos) => Ok(Value::int(pos, call.head).into_pipeline_data()), + Err(e) => Err(ShellError::GenericError { + error: "Failed to convert cursor position to int".to_string(), + msg: e.to_string(), + span: None, + help: None, + inner: vec![], + }), + } } else { Ok(Value::string(repl.buffer.to_string(), call.head).into_pipeline_data()) } diff --git a/src/tests/test_commandline.rs b/src/tests/test_commandline.rs index 9f3eb07ed7..d618ba68b5 100644 --- a/src/tests/test_commandline.rs +++ b/src/tests/test_commandline.rs @@ -135,3 +135,8 @@ fn commandline_test_cursor_end() -> TestResult { "2", // 2 graphemes ) } + +#[test] +fn commandline_test_cursor_type() -> TestResult { + run_test("commandline --cursor | describe", "int") +}