Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-124096: Enable REPL virtual terminal support on Windows #124119

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

y5c4l3
Copy link
Contributor

@y5c4l3 y5c4l3 commented Sep 16, 2024

Copy link

cpython-cla-bot bot commented Sep 16, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@y5c4l3 y5c4l3 force-pushed the windows-vt branch 2 times, most recently from 42a48dc to 13bb26f Compare September 19, 2024 12:31
@pablogsal
Copy link
Member

@ambv @zooba can you test this on a windows machine?

@zooba
Copy link
Member

zooba commented Sep 20, 2024

I'm just testing that indentation works when I paste code? That seems fine.

As a side note, my Ctrl+Left and Ctrl+Right shortcuts (skip word) are broken now (possibly ever since the repl went in), and there's way too much ctypes for my liking in here - we should prioritise moving those to _winapi for 3.13.1.

@y5c4l3
Copy link
Contributor Author

y5c4l3 commented Sep 20, 2024

As a side note, my Ctrl+Left and Ctrl+Right shortcuts (skip word) are broken now

It can be fixed by adding two entries in the keymap. I'll later check whether there are other sequences that I missed before and commit as soon.

@y5c4l3 y5c4l3 force-pushed the windows-vt branch 5 times, most recently from 3c1502a to 718110f Compare September 20, 2024 18:07
To support virtual terminal mode in Windows PYREPL, we need a scanner
to read over the supported escaped VT sequences.

Signed-off-by: y5c4l3 <[email protected]>
Windows REPL input was using virtual key mode, which does not support
terminal escape sequences. This patch calls `SetConsoleMode` properly
when initializing and send sequences to enable bracketed-paste modes
to support verbatim copy-and-paste.

Signed-off-by: y5c4l3 <[email protected]>
@y5c4l3
Copy link
Contributor Author

y5c4l3 commented Sep 28, 2024

@pablogsal @ambv @zooba
Consider it's little bit hard to integrate Windows console tests, I recorded a GIF with screenkey for reference, it at least covers:

  • Ctrl + R
  • PgUp / PgDown
  • Home / End / Ctrl + A / Ctrl + E / Ctrl + ← / Ctrl + →
  • bracketed pastes

Peek 2024-09-28 17-10

@cbrnr
Copy link
Contributor

cbrnr commented Jan 14, 2025

I just wanted to ask if there is something that I could do to help get this PR merged? It is currently not possible to paste a code snippet containing indentation into the REPL on Windows. I think this is a pretty serious limitation, because many tools (e.g. Visual Studio Code) rely on this mechanism to run selected code portions. I was hoping that this fix would make it into 3.13.1, but unfortunately this was not the case. Currently, this forces people on 3.13 to either disable the new REPL experience or switch to 3.12, both of which are very suboptimal workarounds (because the new REPL is great!).

@pavlovc2
Copy link

I second the need to get this merged as I am also impacted by the issue.

@wheeheee
Copy link

Would this PR also work for VSCode's integrated terminal? There's a comment here: # Currently enable bracketed-paste only if it's Windows Terminal.

@pablogsal
Copy link
Member

I just wanted to ask if there is something that I could do to help get this PR merged? It is currently not possible to paste a code snippet containing indentation into the REPL on Windows. I think this is a pretty serious limitation, because many tools (e.g. Visual Studio Code) rely on this mechanism to run selected code portions. I was hoping that this fix would make it into 3.13.1, but unfortunately this was not the case. Currently, this forces people on 3.13 to either disable the new REPL experience or switch to 3.12, both of which are very suboptimal workarounds (because the new REPL is great!).

We need someone with windows knowledge to validate the PR. Unfortunately I don't have a windows machine or enough knowledge of the platform to validate the correctness.

So if you can review the PR or get some windows expert to do we probably can continue landing it

@pablogsal
Copy link
Member

CC @ambv @zooba


# Should make educated guess to determine the terminal type.
# Currently enable bracketed-paste only if it's Windows Terminal.
if 'WT_SESSION' in os.environ:
Copy link

@DHowett DHowett Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Maintainer of Windows Terminal (and/or Windows Console) here 🙂

In the general case, this is a fragile way to detect support. When Windows Terminal is used as the "default console host", it doesn't have a chance to inject its environment variables; this means that any sessions spawned outside of Terminal but which get launched into it will not try to enable bracketed paste.

It looks like the only thing this PR does with __vt_bracketed_paste is check whether it should enable it, and that it doesn't gate any other behavior.

Fortunately, bracketed paste is safe to enable on all console hosts on Windows that support VIRTUAL_TERMINAL_INPUT--even if they do not appear to support it. It'll just no-op when it's not available.

If you really want to check (later!) whether it got enabled, you can request a private mode report with DECRQM (\e[?2004$p); it will return \e[?2004;X$y where X is 1 if it is set and 2 if it is cleared. If it doesn't reply, the console host is too old to support mode queries.

You could also wait to see the first \e[200~ to determine whether it had been turned on for a given paste.

@DHowett
Copy link

DHowett commented Jan 22, 2025

We need someone with windows knowledge to validate the PR. Unfortunately I don't have a windows machine or enough knowledge of the platform to validate the correctness.

So if you can review the PR or get some windows expert to do we probably can continue landing it

I've tapped in somebody from my team (Windows Terminal/Console) to have a look at this. Thanks!

Copy link

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I don't have any notes beyond the WT_SESSION thing that Dustin called out. That environment variable sucks. Terminal feature detection generally sucks, but that variable especially so.

If you blindly enable bracketed paste when VT input is supported, the worst thing that will happen is it works the same as it already does 😅

@encukou
Copy link
Member

encukou commented Jan 23, 2025

@y5c4l3, I hope you don't mind if I take over this PR, resolve the conflict and do the requested changes. (It looks like you're not active on GitHub right now.)

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the change to enable bracketed mode (if VT is supported).

I've tested on Windows 10 on Command Prompt (PS & cmd), VS Code embedded terminal (PS & cmd), and conhost.exe. Bracketed paste works on the first two (there is no confirmation dialog when pasting in multiple lines and the text is not auto-indented; pasting ends the F3 paste mode). On conhost.exe, bracketed paste works as before (dialog appears, text is auto-indented; F3 needs to be ended with another F3).
Ctrl+arrows, PgUp/PgDn, Alt+. still work.

(I found other behaviour that's surprising to me, including a bug, but no unintended changes to the status quo.)

@cbrnr
Copy link
Contributor

cbrnr commented Jan 23, 2025

I just wanted to thank everyone for helping get this PR merged! You are awesome! ❤️

Comment on lines +35 to +41
b'\x1b[17~]': 'f6',
b'\x1b[18~]': 'f7',
b'\x1b[19~]': 'f8',
b'\x1b[20~]': 'f9',
b'\x1b[21~]': 'f10',
b'\x1b[23~]': 'f11',
b'\x1b[24~]': 'f12',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may not fully understand what's going on here, but I believe these encodings are incorrect due to the stray ].

Suggested change
b'\x1b[17~]': 'f6',
b'\x1b[18~]': 'f7',
b'\x1b[19~]': 'f8',
b'\x1b[20~]': 'f9',
b'\x1b[21~]': 'f10',
b'\x1b[23~]': 'f11',
b'\x1b[24~]': 'f12',
b'\x1b[17~': 'f6',
b'\x1b[18~': 'f7',
b'\x1b[19~': 'f8',
b'\x1b[20~': 'f9',
b'\x1b[21~': 'f10',
b'\x1b[23~': 'f11',
b'\x1b[24~': 'f12',

Verified empirically with showkey -a; F6 and F12 pictured:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants