Skip to content
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

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

Open
fsackur opened this issue Apr 23, 2021 · 4 comments

Comments

@fsackur
Copy link

fsackur commented Apr 23, 2021

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
Copy link
Author

fsackur commented Apr 23, 2021

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

@fsackur fsackur changed the title Add New-VtConsoleSequence, to provide embeddable strings and not require Pansies in distirbutable code Add New-VtConsoleSequence, to provide embeddable strings and not require Pansies in distributable code Apr 23, 2021
@Jaykul
Copy link
Member

Jaykul commented Jul 18, 2021

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
Copy link
Member

Jaykul commented Jul 18, 2021

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
Copy link
Member

Jaykul commented Jul 15, 2023

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

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

No branches or pull requests

2 participants