Skip to content

When copying output to the clipboard on macOS, output is padded to 80 columns wide with whitespace #5532

Closed

Description

@kai-h

Steps to reproduce

Install PowerShell on macOS
Open a new PowerShell instance
Do something that causes text to be output to the terminal
Copy and paste this text into somewhere else, e.g. a text editor or an email.
There is extraneous whitespace at the end of every line to pad it out to the width of my Terminal window, which is by default 80 columns.

Expected behavior

It is expected that the text output in the Terminal will not have extra whitespace at the end of each and every line.

Actual behavior

Output is padded to the width of the Terminal window.
e.g. if you look at the output for $PSVersionTable below, copy and paste it into a text editor and you will notice that every line is 150 characters long, padded out with the space character. My Terminal window was 150 characters wide so that it could fit in the output for the OS line without truncating it.

Environment data

PS /Users/kai> $PSVersionTable                                                                                                                        

Name                           Value                                                                                                                 
----                           -----                                                                                                                 
PSVersion                      6.0.0-rc                                                                                                              
PSEdition                      Core                                                                                                                  
GitCommitId                    v6.0.0-rc                                                                                                             
OS                             Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/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. iSazonov commented on Nov 23, 2017

    @iSazonov
    Collaborator

    I can not repo on Windows.

  2. kai-h commented on Nov 23, 2017

    @kai-h
    Author

    Yes, it works correctly on Windows 10. I haven't tested it on Linux.

  3. thezim commented on Nov 24, 2017

    @thezim
    Contributor

    Repros for me. Using Trailing Spaces extension for vscode. Note that it is padding to terminal width, mine is higher than 80.

    issue

  4. iSazonov commented on Nov 24, 2017

    @iSazonov
    Collaborator

    Greg Zimmerman (@thezim) What is a platform/version you repo on?

  5. thezim commented on Nov 24, 2017

    @thezim
    Contributor

    Ilya (@iSazonov) High Sierra.

    PS /Users/greg> $PSVersionTable                                                                                                     
    
    Name                           Value                                                                                               
    ----                           -----                                                                                               
    PSVersion                      6.0.0-rc                                                                                            
    PSEdition                      Core                                                                                                
    GitCommitId                    v6.0.0-rc                                                                                           
    OS                             Darwin 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/R...
    Platform                       Unix                                                                                                
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                             
    PSRemotingProtocolVersion      2.3                                                                                                 
    SerializationVersion           1.1.0.1                                                                                             
    WSManStackVersion              3.0                                                                                                 
    PS /Users/greg> sw_vers                                                                                                             
    ProductName:	Mac OS X
    ProductVersion:	10.13.1
    BuildVersion:	17B48

    Running a native command within PowerShell does not exhibit the same output behavior..

    issue

  6. mklement0 commented on Nov 25, 2017

    @mklement0
    Contributor

    This behavior is by (perhaps unfortunate) design:

    PowerShell's formatting system always produces lines right-padded to the (implied or explicitly specified) output width with spaces.

    It is only when you copy directly from a Windows console window that it extends you the (debatable) courtesy of trimming the trailing spaces (the macOS terminal program doesn't do that, and neither does Ubuntu's default terminal program, for instance).

    You can verify the behavior by piping to clip on Windows as opposed to manually copying in the console window:

    Get-Item / | clip  # text on clipboard will have trailing spaces
  7. thezim commented on Nov 25, 2017

    @thezim
    Contributor

    Is this something that can be changed without breaking something? Seems inefficient for a console to waste time adding and displaying right padded characters.

  8. iSazonov commented on Nov 25, 2017

    @iSazonov
    Collaborator

    It would be interesting to know the history of that console padding.

  9. kai-h commented on Nov 26, 2017

    @kai-h
    Author

    I'm having most output padded to, say, 80 columns, but not all. e.g. the output from $host.UI.RawUI.ReadKey isn't padded:
    screen shot 2017-11-26 at 6 35 49 pm

  10. mklement0 commented on Nov 26, 2017

    @mklement0
    Contributor

    Kai Howells (@kai-h):

    The padding seems to happen only for types for which type-specific output formatting was explicitly defined, as described in Get-Help about_Format.ps1xml, and (as Steve Lee (@SteveL-MSFT) points out below) when a table view is being rendered (whether implicitly or via Format-Table).

    To illustrate the difference (assume an 80-column terminal):

    # The type of $PSVersionTable derives from [hashtable], which has explicit formatting
    # data associated with it and defaults to table view:
    # -> PADDING 
    # All nonempty lines are padded to the output width, which defaults to the terminal width.
    > $PSVersionTable | Out-String -Stream | % Length
    0
    80
    80
    80
    80
    80
    80
    80
    80
    80
    80
    80
    0
    0
    
    
    # [System.Management.Automation.Host.KeyInfo], as returned by `$host.ui.RawUI.ReadKey()`,
    # has NO formatting definitions associated with it.
    # -> NO padding.
    > [System.Management.Automation.Host.KeyInfo]::new(65, 'A', 0, $True) | Out-String -Stream | % Length
    0
    48
    48
    48
    0
    0

    Normally you could use Get-FormatData <type-name> to check if a given type has associated formatting data, but that is currently broken - see #4237 and #4235.

  11. SteveL-MSFT commented on Nov 27, 2017

    @SteveL-MSFT
    Member

    This appears to be by design. Any table view has padding. Any non-table view, I made a PR (merged awhile back) to remove padding and linebreaks.

  12. thezim commented on Nov 27, 2017

    @thezim
    Contributor

    What about the the PS > line itself? I see it is being padded as well.

  13. mklement0 commented on Nov 28, 2017

    @mklement0
    Contributor

    Steve Lee (@SteveL-MSFT): Thanks for the clarification re table view.

    We now know that the behavior is by design, but perhaps it's time to revisit that, as Greg Zimmerman (@thezim) suggests - especially given that it is behavior related to for-display-only output that no one can expect to rely on for long-term stability and machine parsing, as @lzybkr has previously noted.

    Doing away with the padding might also help with #4767 and the fix that @lzybkr had contemplated.

  14. iSazonov commented on Nov 28, 2017

    @iSazonov
    Collaborator

    These trailing spaces may be necessary (on display only) if we want to recolor the background of the table.

  15. kai-h commented on Nov 28, 2017

    @kai-h
    Author

    Following "The law of least surprise" for how things generally should work, if you're getting plain text output, it should not be padded to some arbitrary width that depends on the size of your Terminal window. If it's padded (and it probably shouldn't be) it would possibly be better to be padded to a fixed width, dependent on the width of the output table.
    Or, if you're not recolouring the output, it shouldn't be padded at all.
    Or somehow have the trailing spaces trimmed when copying to the clipboard, however this may be out of your control and depend more on the Terminal app in use.

  16. iSazonov commented on Nov 28, 2017

    @iSazonov
    Collaborator

    Users may want to copy including color.

  17. lzybkr commented on Nov 28, 2017

    @lzybkr
    Contributor

    Copying w/ color works fine w/o padding, CaptureScreen in PSReadLine handles it cleanly.

  18. mklement0 commented on Nov 28, 2017

    @mklement0
    Contributor

    Re color and output width: also note that adjusting the output behavior based on whether a terminal is being targeted or not is sensible, as many Unix utilities (e.g., ls) do (which is again what #4767 would benefit fromtoo).

  19. SteveL-MSFT commented on Nov 28, 2017

    @SteveL-MSFT
    Member

    For Tables, it seems that there should be no reason for the trailing padding. A new line should be sufficient. This isn't macOS specific, it repros on Linux as well (the code is the same on all platforms). I think Windows clipboard may be doing something to trim trailing whitespace...

    I'll look into this as I've made changes to this code previously and think it should be a small change (although not entirely sure of the side effects...)

  20. iSazonov commented on Nov 28, 2017

    @iSazonov
    Collaborator

    If console background is black and we output a table with green background then we can not trim trailing whitespace - we lost green background.

  21. mklement0 commented on Nov 28, 2017

    @mklement0
    Contributor

    I think Windows clipboard may be doing something

    It's actually the console window that does it (copying directly out of a window) - if you pipe to clip.exe in order to copy to the clipboard, you do get the padding.

  22. SteveL-MSFT commented on Nov 28, 2017

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) I think we should support cells in the table having a background, but not the entire table. Alternatively, if we want to support colored background, then we can special case that to preserve padding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifOS-LinuxOS-macOSResolution-FixedThe issue is fixed.WG-Interactive-Consolethe console experience

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions