ShaunLawrie / PwshSpectreConsole

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

added Format-SpectreJson and refactor Format-SpectreTable #15

Closed trackd closed 11 months ago

trackd commented 11 months ago

Hey,

was playing around with the spectre lib and thought json would be a cool addition. Format-SpectreJson image note: this does carry a new dependency, Spectre.Console.Json.dll im not sure if you want to add that?

at the moment all the property type colors are hardcoded, could change that to allow for greater control. would probably need to pass that as a hashtable otherwise its gonna be an insane amount of parameters..

i was also playing around with Format-SpectreTable, i wanted to be able to pass normal objects and have them displayed somewhat similar to normal Format-Table but nicer.

added a helper function Get-DefaultDisplayMembers, my initial version based on (TypeGetter.cs) i had some trouble getting a few properties but found a better version ConvertTo-TableFormat which it's now based on.

image But it's not foolproof, i've not managed to find a way to fix Get-ChildItem image

the tablerow end is too short, not sure whats happening there.

This is not ready to merge.. needs more testing and fixing the ls rows.

if you update prerelease, i could push towards that instead ?

trackd commented 11 months ago

seems as soon as you have any sort of colors using escape codes it messes up the format. image

$start = $($PSStyle.Foreground.Blue)
$end = $($PSStyle.Reset)
$data = @(
    [pscustomobject]@{Name = "${start}John${end}"; Age = 25; City = "New York" },
    [pscustomobject]@{Name = "Jane"; Age = 30; City = "${start}Los Angeles${end}" }
)
$data | Format-SpectreTable
trackd commented 11 months ago

got it working now, it's stripping out the ansi and converting it to spectrecolors on the fly. image

Todo: Need to look at background/foreground and bold

It treats it all as fg atm, and discards bold etc.

trackd commented 11 months ago

This works as expected now, it correctly parses the FormatData and creates the objects with the preferred look, after that it converts the ANSI escape to Spectre Colors, with support for Foreground, Background and Decoration.

Note: it currently does not support multiple different colors in the same cell, mostly because I'm not sure how you would do that with Spectre..

[Spectre.Console.Text]::new(<String>,[Spectre.Console.Style]::new(foreground,background,decoration)) that syntax doesnt really allow for multi colors as far i understand?

PwshSpectreConsole.VTCodes.cs We could just generate a dll from this at module build time.

Sample test

# Import-Module ".\PwshSpectreConsole\PwshSpectreConsole\PwshSpectreConsole.psd1"
# dot source private functions to make them available in the current scope
. ".\PwshSpectreConsole\PwshSpectreConsole\private\Add-PwshSpectreConsole.VTCodes.ps1"
. ".\PwshSpectreConsole\PwshSpectreConsole\private\ConvertTo-SpectreDecoration.ps1"
$sample = @(
    # Simple colors
    "$([char]27)[32m$([char]27)[47mString$([char]27)[0m",
    # RGB colors
    "$([char]27)[38;2;255;0;0m$([char]27)[48;2;0;255;0mRGB String$([char]27)[0m",
    # 256 colors
    "$([char]27)[38;5;1m$([char]27)[48;5;2m256 Color String$([char]27)[0m",
    # Bold decoration
    "$([char]27)[1mBold String$([char]27)[0m",
    # Underline decoration
    "$([char]27)[4mUnderline String$([char]27)[0m",
    # RGB colors with bold decoration
    "$([char]27)[38;2;255;0;0m$([char]27)[48;2;0;255;0m$([char]27)[1mRGB Bold String$([char]27)[0m",
    # 256 colors with underline decoration
    "$([char]27)[38;5;1m$([char]27)[48;5;2m$([char]27)[4m256 Underline String$([char]27)[0m",
    # PSStyle
    "$($PSStyle.Foreground.Green)$($PSStyle.Background.White)String$($PSStyle.Reset)"
)
foreach ($item in $sample) {
    'Original'
    $item
    'Parsed'
    [Spectre.Console.AnsiConsole]::Write((ConvertTo-SpectreDecoration $item))
    "`n"
}