From 7d10a72bb3eb11fa6f7ec6ae0cfc9de6dbefb664 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 30 Dec 2023 16:49:42 +0100 Subject: [PATCH] builtin read: enable bracketed paste We run __fish_enable_bracketed_paste on every shell prompt, and inside edit_command_buffer. This protects from accidents when pasting control characters, and makes sure the paste results in a single undo group. Let's do the same for builtin read. Found while doing the research for #10101 --- fish-rust/src/builtins/read.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fish-rust/src/builtins/read.rs b/fish-rust/src/builtins/read.rs index 903ab2de2..dfd346f0e 100644 --- a/fish-rust/src/builtins/read.rs +++ b/fish-rust/src/builtins/read.rs @@ -7,6 +7,7 @@ use crate::common::scoped_push_replacer; use crate::common::str2wcstring; use crate::common::unescape_string; use crate::common::valid_var_name; +use crate::common::ScopeGuard; use crate::common::UnescapeStringStyle; use crate::compat::MB_CUR_MAX; use crate::env::EnvMode; @@ -25,7 +26,9 @@ use crate::wutil; use crate::wutil::encoding::mbrtowc; use crate::wutil::encoding::zero_mbstate; use crate::wutil::perror; +use crate::wutil::write_to_fd; use libc::SEEK_CUR; +use libc::STDOUT_FILENO; use std::os::fd::RawFd; use std::sync::atomic::Ordering; @@ -584,13 +587,21 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt } }; + let stream_stdin_is_a_tty = isatty(streams.stdin_fd); + + let _maybe_disable_bracketed_paste = stream_stdin_is_a_tty.then(|| { + let _ = write_to_fd(b"\x1b[?2004h", STDOUT_FILENO); + ScopeGuard::new((), |()| { + let _ = write_to_fd(b"\x1b[?2004l", STDOUT_FILENO); + }) + }); + // Normally, we either consume a line of input or all available input. But if we are reading a // line at a time, we need a middle ground where we only consume as many lines as we need to // fill the given vars. loop { buff.clear(); - let stream_stdin_is_a_tty = isatty(streams.stdin_fd); if stream_stdin_is_a_tty && !opts.split_null { // Read interactively using reader_readline(). This does not support splitting on null. exit_res = read_interactive(