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

Add ctrl+enter key binding for Linux #3983

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jborean93
Copy link

@jborean93 jborean93 commented Apr 2, 2024

PR Summary

Adds the Ctrl+Enter key binding for Linux that defaults to AcceptLine. This allows PSReadLine to work by default on Linux terminals like Alacritty (in tmux) where a newline in a paste comes across as the Ctrl+Enter key. Not all terminals on Linux will send a newline in a paste as this key but some do. Having this key handler means that pasting a string with newlines is preserved properly rather than as

You can replicate how a newline was handled in a tty with a .NET application by running the following Python code on Linux

import os
import pty
import subprocess
import sys

parent, child = pty.openpty()
p = subprocess.Popen(
    ['pwsh', '-Command', '[Console]::ReadKey($true) | ConvertTo-Json -EnumsAsString'],
    preexec_fn=os.setsid,
    stdin=child,
    stdout=sys.stdout,
    stderr=sys.stderr,
)
stdin = os.fdopen(parent, 'wb', 0)
os.close(child)

stdin.write(b"\n")
p.communicate()

The output on PowerShell 7.4/.NET 8 is

{
  "KeyChar": "\n",
  "Key": "Enter",
  "Modifiers": "Control"
}

.NET introduced a rewrite of Console.ReadKey() in .NET 7 dotnet/runtime#72193 that changed how a \n on a tty was handled. In the past this is what it returned

{
  "KeyChar": "\r",
  "Key": "Enter",
  "Modifiers": 0
}

As there were no modifiers the \r was treated as a newline and so just worked.

I'm unsure how you wish for me to test this, any guidance there would be appreciated.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure you've added one or more new tests
  • Make sure you've tested these changes in terminals that PowerShell is commonly used in (i.e. conhost.exe, Windows Terminal, Visual Studio Code Integrated Terminal, etc.)
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed at PowerShell-Docs
      • Doc Issue filed:
Microsoft Reviewers: Open in CodeFlow

Adds the Ctrl+Enter key binding for Linux that defaults to AcceptLine.
This allows PSReadLine to work by default on Linux terminals like
Alacritty where a newline in a paste comes across as the Ctrl+Enter key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant