From faf3b356f2443e850116f69e206608214c16d790 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 23 Jun 2024 23:34:44 -0500 Subject: [PATCH] Add `commandline --showing-suggestion` Returns 0 (true) in case an autosuggestion is currently being displayed. This was first requested in #5000 then again in #10580 after the existing workaround for this missing functionality was broken as part of a change to the overall behavior of `commandline` (for the better). --- src/builtins/commandline.rs | 12 +++++++++++- src/reader.rs | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/builtins/commandline.rs b/src/builtins/commandline.rs index cc3821922..8a57d3dd9 100644 --- a/src/builtins/commandline.rs +++ b/src/builtins/commandline.rs @@ -13,7 +13,7 @@ use crate::parse_util::{ use crate::proc::is_interactive_session; use crate::reader::{ commandline_get_state, commandline_set_buffer, commandline_set_search_field, - reader_execute_readline_cmd, + reader_execute_readline_cmd, reader_showing_suggestion, }; use crate::tokenizer::TOK_ACCEPT_UNFINISHED; use crate::tokenizer::{TokenType, Tokenizer}; @@ -201,6 +201,7 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) let mut paging_full_mode = false; let mut search_field_mode = false; let mut is_valid = false; + let mut showing_suggestion = false; let mut range = 0..0; let mut override_buffer = None; @@ -231,6 +232,7 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) wopt(L!("paging-full-mode"), ArgType::NoArgument, 'F'), wopt(L!("search-field"), ArgType::NoArgument, '\x03'), wopt(L!("is-valid"), ArgType::NoArgument, '\x01'), + wopt(L!("showing-suggestion"), ArgType::NoArgument, '\x04'), ]; let mut w = WGetopter::new(short_options, long_options, args); @@ -277,6 +279,7 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) 'F' => paging_full_mode = true, '\x03' => search_field_mode = true, '\x01' => is_valid = true, + '\x04' => showing_suggestion = true, 'h' => { builtin_print_help(parser, streams, cmd); return STATUS_CMD_OK; @@ -510,6 +513,13 @@ pub fn commandline(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) }; } + if showing_suggestion { + if reader_showing_suggestion(parser) { + return Some(0); + } + return Some(1); + } + if search_field_mode { range = 0..current_buffer.len(); } else { diff --git a/src/reader.rs b/src/reader.rs index e2d3a26bd..1d4b9934a 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -942,6 +942,18 @@ pub fn reader_execute_readline_cmd(parser: &Parser, ch: CharEvent) { } } +pub fn reader_showing_suggestion(parser: &Parser) -> bool { + if !is_interactive_session() { + return false; + } + if let Some(data) = current_data() { + let reader = Reader { parser, data }; + !reader.autosuggestion.is_empty() + } else { + false + } +} + /// Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it /// was set. If the current reader is interruptible, mark the reader as exit_loop_requested. pub fn reader_reading_interrupted(data: &mut ReaderData) -> i32 {