Skip to content

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

@mklement0

Description

@mklement0

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)

Metadata

Metadata

Assignees

Labels

Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifWG-Interactive-Consolethe console experience

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions