ShaunLawrie / PwshSpectreConsole

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

Bug Format-SpectreTable enumerating [ICollection] #24

Closed trackd closed 8 months ago

trackd commented 9 months ago

opening an issue just incase someone else also runs into it.

i found a bug in my code for Format-SpectreTable and there doesnt seem to be an obvious solution that would cover all scenarios.

the first command runs with the latest preview, the second runs my local test with Format-Table instead of Select-Object for formatting. image

The problem seem to be that some properties are unrolled and that doesn't seem to be apart of the formatting file but some internal db in Format-Table or something.

BaseFormattingCommand

GetExpansionState

I have not found a good way to detect this, [ICollection] seems to be the type but there are different propertynames for different types..

While it certainly is possible to add the ones we find and unroll them.. i thought it might be easier to just rip out the values from Format-Table and let that worry about enumeration.

I'm still testing so it's not really ready, but i might push a few changes.

the new method is still very untested so need a little time.

Current approach uses something like this, Get-TableData

trackd commented 9 months ago

So i've been tinkering with this for a little bit.

i've tried to detect collections and enumerate them properly.. but it's kinda hard to test so not sure if all works, there is like a manual propertymap we can add stuff to for exceptions:

        $propertyMap = @{
            'ServiceController' = 'Name'
            'ProcessThread'     = 'Id'
            'FileInfo'          = 'Name'
            'DirectoryInfo'     = 'Name'
            # Add more mappings as needed
        }

after that it tries, known values MshObjectUtil.cs it tries its best to deduce what property to show but it's not foolproof.

the updated code is here: trackd/PwshSpectreConsole/prerelease

i've also been playing around with a version that uses format-table instead of select-object to format the data.. trackd/PwshSpectreConsole/FormatTableVersion

it does simplify alot of things but it breaks a few tests and we loose the actual objects as its formatdata.

there are benefits and drawbacks to both, not really sure which way to go..

ShaunLawrie commented 9 months ago

I think it's better to use format-table rather than try keep in sync with the manual mapping inside powershell. Does it matter so much if the raw data is lost if this only happens internally in format-spectretable? I can't see a situation where you would even be able to use the data after it's been piped to that command e.g. in the current state $data | Format-SpectreTable | Select-Object Anything doesn't work because it gets rendered and no data is returned.

trackd commented 9 months ago

yea it simplifies alot to go with format-table.

i'll go over that branch and merge a few other fixes i think i may have added to prerelease and push during the weekend.

trackd commented 9 months ago

The biggest change i think is that we loose psobject.properties.value which makes it harder to test the helper functions as we cant pass normal objects to them.

Iirc it looks for .propertyvalue now instead.

But instead we get some fun parameters from format-table like property that supports name/expression hashtables to make custom formatting on the fly and a few others