PoshCode / Pansies

Powershell ANSI Escape Sequences, functions for colored output, etc.
MIT License
148 stars 14 forks source link

Add New-VtConsoleSequence, to provide embeddable strings and not require Pansies in distributable code #19

Open fsackur opened 3 years ago

fsackur commented 3 years ago

Use case: my team has ~200 powershell modules deployed to an enterprise nuget source. Our dependency trees are a little cumbersome. We don't want to add any dependencies if we can avoid it.

We do use ANSI sequences, some of which I have generated with Pansies - ♥ - but I am currently doing some awkward manipulation to create strings which I can embed in our modules.

I would like to use a function New-VtConsoleSequence in development, to create those strings.

It would have a Reset switch in one parameterset, to output an ANSI reset; the other parameterset would accept any valid input to the ctor for RgbColor, e.g. ForestGreen1, ea0016.

I would also like a EscapeAsExpression switch, which would behave as follows:

> New-VtConsoleSequence ea0016 -EscapeAsExpression
$([char]27)[38;2;234;0;22m

> New-VtConsoleSequence -Reset -EscapeAsExpression
$([char]27)[39m

Then my module functions will have code like this:


$DeepSkyBlue = "$([char]27)[38;2;0;191;255m"
$Reset       = "$([char]27)[0m"
Write-Information "$DeepSkyBlue`Stuff possibly relating to other stuff.$Reset"
fsackur commented 3 years ago

If we can get an acceptable req, I would PR it. I'd love to contribute.

Jaykul commented 3 years ago

All you have to do is run the output through -replace "`e",'`e' (or, for PowerShell 5 compatibility -replace "$([char]27)",'$([char]27)')

(Text ${fg:Aquamarine}Hello) -replace "`e",'`e'
[rgbColor]::Background("ea0016") -replace "`e",'`e'
$fg:Tomato -replace "$([char]27)",'$([char]27)'

Or even:

(Get-Gradient Tomato Aquamarine -Length 10 | % ToVtEscapeSequence $false) -replace "$([char]27)",'$([char]27)'
Jaykul commented 3 years ago

Originally, none of that seemed like a good idea to me. When I started this, there were a lot of places (particularly Windows) where only a subset of colors were supported (e.g. 16 colors, or 256 colors), so one of the best features of PANSIES was the ability to set the mode to whatever your terminal supports. Nowadays that doesn't seem worth worrying about as much, and in many modules, I'd be willing to just hard-code RGB color sequences...

I think it would definitely be interesting to write something that could be run on PowerShell files (.ps1 and .psm1 and .ps1xml format files) at "build" time to use the AST parser and replace references to the drives (i.e. ${fg:...} and ${bg:...}) with in-line escape sequences.

In fact, with that in mind, I wouldn't mind adding a PSProvider drive for $emoji named escape sequences and $nf (nerdfonts) to make it easier to use those too. That would mean you could use "${fg:SkyBlue3}Hello${emoji:smile}${fg:Clear}" in your source code, but have it converted at build-time to a less readable version (optionally, with $([char]27) or `e but -- for maximum speed by just embedding the actual escape sequences in the UTF8 files...

Jaykul commented 1 year ago

Just to update:

I never did the drive provider for those others, but I did add them as entities that are handled by Pansies' New-Text and Write-Host commands, so you can for example:

Write-Host "Hello&smilingfacewithtear;" Hello🥲

I am leaving this open because I do still want to implement something to replace all those &entities; in a file with the actual characters, escape sequences or $([char]###) patterns for shipping