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

Strings with Virtual Terminal (VT) / ANSI escape sequences that are truncated for display make effects linger #7767

Closed
mklement0 opened this issue Sep 12, 2018 · 9 comments · Fixed by #8326
Assignees
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif WG-Interactive-Console the console experience

Comments

@mklement0
Copy link
Contributor

mklement0 commented Sep 12, 2018

Related: #7744

Virtual Terminal escape sequences allow, among other things, formatting of console (terminal) output, notably to produce colored output.

An escape sequence that changes the output color - e.g., "`e[32m" to switch to green - stays in effect until the color is reset - e.g., with "`e[m"

Example:

"before `e[32mgreen`e[m after"  # prints only the word "green" in green.

Therefore, if PowerShell truncates strings for output formatting, a string may get cut off so that the reset sequence is not output, which means that the color unexpectedly stays in effect.

#7744 shows the problem in the context of using an OSC escape sequence to construct a hyperlink (which only some terminals support; others render just the link label).

This is a nontrivial problem to solve, because PowerShell would have to be aware of all potential escape sequences and know how to reset their effects so that the appropriate escape sequence can be appended to the truncated string.

Steps to reproduce

[pscustomobject] @{ test1="before `e[32mgreen`e[m after"; test2='next col' } | Format-Table @{ e='test1'; width=17 }, test2

Expected behavior

test1              test2
-----              -----
before gr...       next col

gr... should render green, but next col should render with the default color.

Actual behavior

test1             test2
-----             -----
before gr...  next col

That is, next col too is printed in green, and green output stays in effect (the prompt rendered after the command will be green too).

Additionally, note that the column alignment is off.

Environment data

PowerShell Core v6.1.0-rc.1 on macOS 10.13.6
PowerShell Core v6.1.0-rc.1 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-rc.1 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.165)
@iSazonov iSazonov added WG-Interactive-Console the console experience Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif labels Sep 12, 2018
@iSazonov
Copy link
Collaborator

I would like to know that there is a better practice for working with VT.

@mklement0
Copy link
Contributor Author

What do you mean by that, @iSazonov?

@SteveL-MSFT
Copy link
Member

It seems we could go with a simple approach and always reset the console color anytime after we truncate.

@mklement0
Copy link
Contributor Author

mklement0 commented Sep 12, 2018

That's promising, @SteveL-MSFT; it will definitely work with colors, but is there an equivalent generic reset mechanism for other types of escape sequences, such as the OSC sequences mentioned in #7744? (There may be, I haven't checked).

"`e[m", which works for colors, does not work for OSCs.

@iSazonov
Copy link
Collaborator

We don't want to see escapes in trace log. So we need a rendering subsystem.

@SeeminglyScience
Copy link
Collaborator

SeeminglyScience commented Sep 13, 2018

Worth noting that `e[m won't reset underline (at least on Windows), so `e[24m may be needed as well.

@mklement0
Copy link
Contributor Author

That smells like a bug, @SeeminglyScience, and I actually can't reproduce it on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.285)

Does the following really not work for you?

"before `e[4munderlined`e[m after"

@SeeminglyScience
Copy link
Collaborator

@mklement0 huh, it does now. I wonder if a Windows update fixed that or if I'm just thinking of something else...

Either way nevermind :)

@jazzdelightsme
Copy link
Contributor

I made the same comment on #7744, but: DbgShell's approach is not to try to reset/undo escape sequences; rather just leave them all in. In other words, when truncating, it only truncates content, but preserves all escape sequences (code here).

That said, resetting does seem like a good idea in general for a formatting engine. I came searching for Issues after running Get-Variable where I had a bunch of variables like ${RED}, ${YELLOW}, ${BLACK}, etc. defined, which totally horked my console. Fortunately I also had a Reset-Colors command.

@SteveL-MSFT SteveL-MSFT self-assigned this Nov 20, 2018
TravisEz13 pushed a commit that referenced this issue Dec 11, 2018
…le`; Replace `...` with unicode ellipsis (#8326)

change ellipsis when truncating to single unicode character
reset console output if previous column contains ESC
update existing format-table tests

If content included a VT100 ESC sequence (like changing color), this affected all output after that cell in the table.  Fix is to detect that a cell contained ESC and reset the console after it.  Also, change the 3 character ellipsis `...` to use the single unicode character `…` so that more text is available.

Fix #7767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif WG-Interactive-Console the console experience
Projects
None yet
5 participants