Skip to content

OSC sequences count into wrapping/truncation limits #7744

Open

Description

ESC ] aka OSC (operation system control) sequences are used in terminals to control things like the terminal window, or insert hyperlinks.

From wikipedia:

Starts a control string for the operating system to use, terminated by ST.[14]:8.3.89 In xterm, they may also be terminated by BEL.[15] In xterm, the window title can be set by OSC 0;this is the window title BEL.

The sequence is followed by plain characters and terminated by ST/BEL. The plain characters are not shown to the user, they are interpreted by the terminal, therefor they should not be considered for line wrapping or truncation, but PowerShell does so.

This affects values in table format, group headers, etc.

Steps to reproduce

Run this a PowerShell terminal:

@{ test = "`e]8;;https://github.com/PowerShell/PowerShell#this_is_a_very_long_link_to_show_the_problem_with_osc_sequences`aThis is the link text which should not be truncated`e]8;;`a" }

The OSC sequence used here as an example creates a hyperlink (works in iTerm2 and Gnome terminal, and will only show the link text in terminals without support).

Expected behavior

Output (make sure the resize the terminal so that the text should fit on the screen):

Name                           Value
----                           -----
test                           This is the link text which should not be truncated

Actual behavior

Text gets truncated:

Name                           Value
----                           -----
test                           This is the link text wh...

The amount of text truncated depends on the terminal size of course, but the point is it should not get truncated at all if it fits on the screen.

If you use a string without an OSC sequence, it will not get wrapped:

@{ test = "This is the link text which should not be truncated" }

What's worst is that through truncation PowerShell can cut off the escape sequence terminator, making the sequence apply to all following terminal output (e.g. everything becomes the hyperlink).

This affects values in tables as shown, but also group headers:

github.com/sourcegraph/sourcegraph-classic > app/node_modules/jest-cli/node_modu
les/optimist/example/usage-options.js


Line Preview
---- -------
  18 console.log('\n\nInspecting options');


github.com/sourcegraph/sourcegraph-classic > app/node_modules/jsfmt/node_m
odules/docopt/examples/any_options_example.coffee


Line Preview
---- -------
   1 doc = """Example of program which uses [options] shortcut in pattern.
   4   any_options_example.coffee [options
   6 Options:

Note the wrongly wrapped the group headers (the links are also broken because of this).

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.4
PSEdition                      Core
GitCommitId                    v6.0.4
OS                             Darwin 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    WG-Interactive-Console
    Issue-Discussion
    on Sep 10, 2018
  2. felixfbecker commented on Sep 10, 2018

    @felixfbecker
    ContributorAuthor

    Ilya (@iSazonov) why is this Issue-Discussion and not Issue-Bug?

  3. iSazonov commented on Sep 10, 2018

    @iSazonov
    Collaborator

    Felix Becker (@felixfbecker) I don't see the problem on Windows. On WSL a console is frozen. I haven't MacOs to test.
    We need more info. I don't know if the issue related with escapes.

  4. felixfbecker commented on Sep 10, 2018

    @felixfbecker
    ContributorAuthor

    I would bet the issue also happens in Linux, did you try a VM? I am almost certain it's caused by the escapes, because the point where truncation occurs is in direct relation to how long the link text inside the escape sequence is.
    Builds are run on macOS/Linux, right? If there are unit tests for the truncation behaviour, maybe it would be possible to write a failing test?

  5. iSazonov commented on Sep 10, 2018

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) Have you time to look the Issue on Linux/MacOs? Does escapes really affect?

  6. mklement0 commented on Sep 12, 2018

    @mklement0
    Contributor

    While I haven't dug deep, there is definitely something going on with respect to escape sequences, and it affects all platforms:

    [pscustomobject] @{ test = "`e[32mgreen`e[m" } | ft -property @{ e='test'; width = 5 }

    The above the string green in green, as expected, on Windows, macOS, Linux (Ubuntu).
    That is, the length of the string is correctly recognized as 5, despite the presence of the escape sequences, which are seemingly ignored in this case.

    Things go awry if you force truncation, however, simply by lowering the width to 4:

    [pscustomobject] @{ test = "`e[32mgreen`e[m" } | ft -property @{ e='test'; width = 4 }

    This should render "g...", and that's what it does without escape sequences, but with them present what is rendered is the equivalent of "`e..." - i.e., the very first character in the string is blindly used, without regard to whether it is part of an escape sequence or not.

    How that renders depends on the platform / terminal application, but it's definitely broken.

    Because the partial escape sequence doesn't succeed in changing the color in this case, there are no aftereffects, but, as in Felix Becker (@felixfbecker)'s example, if a complete start escape sequence happens to be rendered, but not its complementary end sequence, effects, such as color changes, linger - and solving that problem generically sounds nontrivial.

    For instance, the following will print gr... in green and persistently switch to green output, because the sequence that turns green off, e[m, is not sent to the terminal:

    [pscustomobject] @{ test = "`e[32mgreen, green grass of home`e[m" } | ft -property @{ e='test'; width = 10 }
  7. rkeithhill commented on Sep 12, 2018

    @rkeithhill
    Collaborator

    I don't think the following is a valid terminating seq:

     "`e[m"
    

    Try using:

    "`e[0m"
    
  8. mklement0 commented on Sep 12, 2018

    @mklement0
    Contributor

    Keith Hill (@rkeithhill): The 0 is optional; try the following (on any supported platform):

    "before `e[32mgreen`e[m after"
  9. rkeithhill commented on Sep 12, 2018

    @rkeithhill
    Collaborator

    Yeah, now I see the line in the docs that I missed:

    When no parameters are specified, it is treated the same as a single 0 parameter.

    Good to know.

  10. felixfbecker commented on Sep 12, 2018

    @felixfbecker
    ContributorAuthor

    Yeah, truncated text including opening escapes but possibly truncating the closing escapes is another problem, but I would say only loosely related to this issue.

    Your experiment above seems to show that length is not a problem for color escape codes, but the issue description shows that it is a problem for OSC sequences. Perhaps PowerShell recognizes and filters ESC[ sequences (which are terminated with m), but not ESC]8 sequences (which are terminated with ST/BEL).

  11. mklement0 commented on Sep 12, 2018

    @mklement0
    Contributor

    Good point about the distinction between color codes and OSC sequences, Felix Becker (@felixfbecker), and I think your guess about PowerShell not recognizing the latter is correct:

    [pscustomobject] @{ test = "`e]8;;http://example.org`aexample.org`e]8;;`a" } | ft @{ e='test'; width = 11 }

    The above breaks, even though 11 is exactly the length of example.org, and it only renders correctly with a minimum width of 41, which is the raw character count of the string including escape sequences.

    I've opened a separate issue for the end-escape-sequence-not-getting-output problem: #7767

  12. felixfbecker commented on Sep 20, 2018

    @felixfbecker
    ContributorAuthor
  13. added
    Issue-Enhancement
    on Sep 20, 2018
  14. 53 remaining items

  15. microsoft-github-policy-service commented on Apr 15, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  16. BalassaMarton commented on Apr 15, 2025

    @BalassaMarton

    Issue is still relevant.

  17. removed
    Resolution-No Activity
    on Apr 15, 2025
  18. microsoft-github-policy-service commented on Oct 12, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  19. added
    Resolution-No Activity
    on Oct 12, 2025
  20. iSazonov commented on Oct 13, 2025

    @iSazonov
    Collaborator

    For reference #23861 (where we have idea for better Unicode and Terminal supprt.)

  21. removed
    Resolution-No Activity
    on Oct 13, 2025
  22. added
    Resolution-No Activity
    on Apr 11, 2026
  23. microsoft-github-policy-service commented on Apr 11, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  24. zaneduffield commented on Apr 13, 2026

    @zaneduffield

    Bump

  25. removed
    Resolution-No Activity
    on Apr 13, 2026
  26. added
    Resolution-No Activity
    on Oct 10, 2026
  27. microsoft-github-policy-service commented on Oct 10, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  28. oising commented on Oct 13, 2026

    @oising
    Contributor

    Hey hey

  29. removed
    Resolution-No Activity
    on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugIssue-EnhancementWG-Interactive-Console

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions