ShaunLawrie / PwshSpectreConsole

👻 PwshSpectreConsole is a PowerShell wrapper for the awesome Spectre.Console library
https://pwshspectreconsole.com/
MIT License
114 stars 7 forks source link

Function or parameter in cmdlets to escape markup #5

Closed ruckii closed 1 year ago

ruckii commented 1 year ago

According to Spectre.Console Best Practices:

Do escape data when outputting any user input or any external data via Markup using the EscapeMarkup method on the data. Any user input containing [ or ] will likely cause a runtime error while rendering otherwise.

There are at least 2 options:

$data = "][[][]]][[][][][" $escapedData = [Spectre.Console.Markup]::Escape($data) Format-SpectrePanel -Title "Escaped data" -Data $escapedData Format-SpectrePanel -Title "Unescaped data" -Data $data # Exception

ShaunLawrie commented 1 year ago

I added a function for escaping Get-SpectreEscapedText. I wonder if that's enough for most cases? It just pipes the text through that escape function you have there so you can use it in a pipeline.

Cases like your one above:
image

And receiving untrusted input like the best practice doc talks about:
image

And mixtures of escaped and unescaped data:
image

I've pushed this and your other PR to psgallery in https://github.com/ShaunLawrie/PwshSpectreConsole/releases/tag/v0.2.3 Thanks for the feedback.

ruckii commented 1 year ago

Escaping function is what was needed! Thanks a lot!