Skip to content

Fixed #58 - Newlines in commands render incorrectly #60

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

Merged
merged 5 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ Get-Process | Out-ConsoleGridView
> NOTE: If you change the code and rebuild the project, you'll need to launch a
> _new_ PowerShell process since the dll is already loaded and can't be unloaded.

### Debugging in Visual Studio Code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this!!



```powershell
PS C:\path\to\GraphicalTools> code .
```

Build by hitting `Ctrl-Shift-b` in VS Code.

To debug:

In a Powershell session in the `c:\path\to\GraphicalTools` directory, run `pwsh` (thus nesting powershell).

Then do the folowing:

```powershell
Import-Module .\module\Microsoft.PowerShell.ConsoleGuiTools
$pid
```

This will import the latest built DLL and output the process ID you'll need for debugging. Copy this ID to the clipboard.

In VScode, set your breakpoints, etc... Then hit `F5`. In the VScode search box, paste the value printed by `$pid`. You'll see something like `pwsh.exe 18328`. Click that and the debug session will start.

In the Powershell session run your commands; breakpoints will be hit, etc...

When done, run `exit` to exit the nested PowerShell and run `pwsh` again. This unloads the DLL. Repeat.


## Contributions Welcome!

We would love to incorporate community contributions into this project. If you would like to
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ private static string GetPaddedString(List<string> strings, int[] colWidths, int
builder.Append(' ');
}

// Replace any newlines with encoded newline/linefeed (`n or `r)
// Note we can't use Environment.Newline because we don't know that the
// Command honors that.
strings[i] = strings[i].Replace("\r", "`r");
strings[i] = strings[i].Replace("\n", "`n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So thinking about this some more, it may be possible that the string has a carriage return and a linefeed. So in addition to what you're doing here, we should also handle that so:

Suggested change
strings[i] = strings[i].Replace("\n", "`n");
strings[i] = strings[i].Replace("\r\n", "`r`n");
strings[i] = strings[i].Replace("\n", "`n");

This makes sure there are no lingering \r.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be just as good to do a replace for \r and another for \n and thus catch all of them by default?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's true. I guess that would catch the off chance that there's a \r by itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur. I've changed it to:

                // Replace any newlines with encoded newline/linefeed (`n or `r)
                // Note we can't use Environment.Newline because we don't know that the
                // Command honors that.
                strings[i] = strings[i].Replace("\r", "`r");
                strings[i] = strings[i].Replace("\n", "`n");

I don't do many PRs, so I'm not sure what I'm supposed to do to push this through other than pushing it to my master branch... I think it's automatic?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create new branch for every new PR.


// If the string won't fit in the column, append an ellipsis.
if (strings[i].Length > colWidths[i])
{
Expand Down