From 5bd7300cd5b26ea5e22ba4512edeed578e83ae1b Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 13 Sep 2023 12:44:09 -0500 Subject: [PATCH] add helper switch to move cursor to end of buffer (#10354) # Description This PR adds a helper flag named `--cursor-end`/`-e` that allows you to set the cursor to the end of the buffer. Before this, you'd have to do something like `--cursor 100` where you're guessing that 100 would be longer than the buffer and just put it at the end. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-cli/src/commands/commandline.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/nu-cli/src/commands/commandline.rs b/crates/nu-cli/src/commands/commandline.rs index 17b02fa6cf..1ab4282cb5 100644 --- a/crates/nu-cli/src/commands/commandline.rs +++ b/crates/nu-cli/src/commands/commandline.rs @@ -1,9 +1,9 @@ use nu_engine::CallExt; -use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::Category; -use nu_protocol::IntoPipelineData; -use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Type, Value}; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, +}; use unicode_segmentation::UnicodeSegmentation; #[derive(Clone)] @@ -25,6 +25,11 @@ impl Command for Commandline { "Set or get the current cursor position", Some('c'), ) + .switch( + "cursor-end", + "Set the current cursor position to the end of the buffer", + Some('e'), + ) .switch( "append", "appends the string to the end of the buffer", @@ -104,8 +109,11 @@ impl Command for Commandline { } Ok(Value::nothing(call.head).into_pipeline_data()) } else { - let repl = engine_state.repl_state.lock().expect("repl state mutex"); - if call.has_flag("cursor") { + let mut repl = engine_state.repl_state.lock().expect("repl state mutex"); + if call.has_flag("cursor-end") { + repl.cursor_pos = repl.buffer.graphemes(true).count(); + Ok(Value::nothing(call.head).into_pipeline_data()) + } else if call.has_flag("cursor") { let char_pos = repl .buffer .grapheme_indices(true)