rexpect icon indicating copy to clipboard operation
rexpect copied to clipboard

Disabling bracketed paste mode

Open zmitchell opened this issue 1 year ago • 8 comments

When integrating rexpect into some of our tests I ran into an issue where the escape codes for bracketed paste mode were present in the output. This naturally broke the tests until I figured out the issue. I found that adding bind 'set enable-bracketed-paste off' to the prompt-setting command here solves the problem:

https://github.com/rust-cli/rexpect/blob/be7f1ab8147c3c17e0600e59d1f98d1963d2d880/src/session.rs#L414

(I'm not sure it's relevant, but in my fork that whole command is actually PS1="~~~~" && unset PROMPT_COMMAND && bind 'set enable-bracketed-paste off').

Is this something you'd like to see upstreamed, or are there legitimate cases where disabling this is undesired?

zmitchell avatar Feb 18 '25 18:02 zmitchell

Have you looked into why bracketed paste is present?

epage avatar Feb 18 '25 18:02 epage

I haven't looked into this too deeply, but I believe it is on by default in readline (see here https://github.com/python/cpython/issues/86985).

zmitchell avatar Feb 18 '25 19:02 zmitchell

What I'm asking is why is it considered pasted, rather than typed?

epage avatar Feb 18 '25 19:02 epage

Ah, misunderstood. That's a good question. This was just text that I had typed out and sent to the shell via send_line, so I don't see an obvious reason why it would be considered pasted.

zmitchell avatar Feb 19 '25 07:02 zmitchell

I'll be spending a little more time on this project in the next few days. Let me see if I can get you a reproducer and I'll get back to you.

zmitchell avatar Feb 19 '25 07:02 zmitchell

I see test the following test failure from a fresh checkout of rexpect:

thread 'session::tests::test_bash' panicked at src/session.rs:542:9:
assertion `left == right` failed
  left: "/tmp\r\n"
 right: "\u{1b}[?2004l\r\r\n/tmp\r\n\u{1b}[?2004h"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

zmitchell avatar Feb 20 '25 21:02 zmitchell

  • That was in Zellij in Wezterm.
  • Same issue outside of Zellij in Wezterm.
  • Same issue outside of Zellij in Ghostty.
  • Same issue outside of Zellij in Terminal.app

So either all of my terminal emulators are misconfigured in the same way, or it's not the terminal emulator.

All of my shells are installed and configured via Nix/nix-darwin/home-manager, for reference.

  • I don't see anything related to this in ~/.bashrc, /etc/bashrc, or /etc/bash.bashrc.

zmitchell avatar Feb 20 '25 21:02 zmitchell

I'm reproducing the same thing but CI is not.

We write to bash's file handle. My best guess is that readline is reading through an alternative API for interactive input and then assumes all text written to the file handle is pasted.

Whats interesting is that I'm not seeing this in completest-pty which implements something like rexpect to test shell completion scripts. In that, we load an isolated bash environment https://github.com/assert-rs/completest/blob/b466abeda28a0393019768478d0353a5d9533271/crates/completest-pty/src/lib.rs#L172-L233

I would be fine with a hacky solution for now. I wonder if we should create an isolated environment like completest-pty.

epage avatar Feb 20 '25 22:02 epage