-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Convert paste line endings #1094
Conversation
Windows -> Unix line-ending conversion is pretty much unavoidable for Terminal to interact happily with WSL. TextBuffer::GetTextForClipboard inserts CRLF pairs into copied text, and while this could be changed to (optionally) generate LFs instead of CRLFs, this doesn't seem to be sufficient since many/most Windows text editors (at a minimum Notepad, VS2019, and vscode) generate CRLF pairs. I think fixing this at the paste level is the ideal location for the fix, as I'm not aware of another way for CRLFs to reasonably get into Unix-space terminal sessions that don't want them. Fixing it as a profile-level option seems appropriate (as opposed to having separate key bindings for "raw paste" and "CRLF fixed paste"), as key bindings are app-wide and a profile-level setting allows different behavior for, say, bash vs. powershell. |
Generate LFs on paste instead of CRLFs? I think this is incorrect. If this is a terminal and we're trying to emulate typing - I believe we're likely after CRs instead of CRLFs. Some shells (like bash) look for LF endings, and then use Unfortunately, if you start using a (typically interactive) program (e.g. If you test pasting to both (I've just re-tested all 3 CRLF/LF/CR line-ending pasting scenarios into WSL using 880272c, and CR line-endings was the only that worked as "expected".) |
Updated to sending the correct character. |
Follow-up: Is there any reason to have more-granular settings? That is, is there a point to having an option which specifies which line-ending to use, or is \x0d sufficient? Clearly my knowledge here is lacking, because I thought bash/vim's handling of \x0a was universal... |
…gham/terminal into convert-paste-line-endings
Consumer scenarios I can think of: Producer scenarios: I'm just ignoring bracketed paste (http://www.xfree86.org/current/ctlseqs.html#Bracketed%20Paste%20Mode) at this point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems mostly reasonable to me.
This won't really work for profiles that have the setting set to false (like cmd.exe) and then run wsl though, correct? Hopefully in the future we can work on a solution to that scenario too.
@@ -85,6 +86,7 @@ Profile::Profile(const winrt::guid& guid) : | |||
_useAcrylic{ false }, | |||
_scrollbarState{}, | |||
_closeOnExit{ true }, | |||
_convertPasteLineEndings{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a tangential change, do we want the WSL profiles that are auto-generated to default to having this setting set to true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that sounds like a good idea.
|
||
std::wstring::size_type pos = 0; | ||
|
||
// Lament that the stl string does not have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::regex_replace
allows replacing all matches in a single op:
if (_settings.ConvertPasteLineEndings()) {
const static std::wregex rgx{ L"\n\r" };
std::wstring stripped = std::regex_replace(wstr, rgx, L"\n");
_SendInputToConnection(stripped);
}
...
/azp run |
Pull request contains merge conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this needs to be a setting. conpty should be turning <CR>
into the right format on the other end. Maybe this is important for other types of connections?
Dustin's comment about conpty has me thinking maybe conpty is the place this should be fixed, not the terminal
It looks like this was outmoded by #2390. Thanks again for the original inspiration! |
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request <!-- Other than the issue solved, is this relevant to any other issues/existing PRs? --> ## References #1091 #1094 #2390 #3314 <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist * [x] Closes #1091 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Requires documentation to be updated * [x] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx <!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments Combination of the PRs #1094, #2390, and #3314, especially as discussed in #3314. In short, this changes line endings from Windows-space \r\n to the more universal \r. <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Copied and pasted text into the terminal without the patch, line endings were doubled. With the patch, line endings weren't doubled. -------------------- * Replacing \r\n line endings with \r line endings * Fixing Formatting
Summary of the Pull Request
Implements line-ending conversion for pasting. Specifically by introducing a new option -- convertPasteLineEndings (optional, defaults to false) -- which, when set, causes pastes from the clipboard to have CRLF pairs converted to LFs.
See here: (Top copy/paste is prior behavior or convertPasteLineEndings set to false; bottom is convertPasteLineEndings set to true; this is Terminal running WSL bash):
References
PR Checklist
Detailed Description of the Pull Request / Additional comments
The new option, convertPasteLineEndings does what it says -- replacing Windows-space CRLF pairs with Unix-space LFs in text pasted to the console. This option applies to both the keyboard-shortcut paste and the right-click paste. Without this most multiline text pasted into Terminal will be "double-spaced" due to the Windows-style CRLF pairs. Furthermore, any multiline text copied from Terminal (in trim whitespace mode) generates CRLF pairs, which guarantees double-spacing when copying from a WSL Terminal session.
Changes:
Notes: