ShaunLawrie / PwshSpectreConsole

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

[Implimented Feature, PR #23] Moved $script:AccentColor and $script:DefaultValueColor to a new 'defaults' folder in 'private'. #26

Closed futuremotiondev closed 6 months ago

futuremotiondev commented 7 months ago

Link to PR#23

Overview


  1. Removed the $script:AccentColor and $script:DefaultValueColor definitions from PwshSpectreConsole.psm1
  2. Created a new folder in private called defaults, and moved the above definitions to a new file called Initialize-DefaultColors.ps1
  3. Updated the psm1 to reflect this change:
using module ".\private\completions\Completers.psm1"

# Dotsource default configuration
Get-ChildItem -Path "$PSScriptRoot\private\defaults\*.ps1" -Recurse | ForEach-Object {
    . $_.FullName
}

# Dotsource each function in Private and Public
foreach ($directory in @('private', 'public')) {
    Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object {
        . $_.FullName
    }
}

However looking at the code now, I think the first statement under # Dotsource default configuration is redundant as the below foreach loop already dot-sources everything in private.

So I'm going to change the .psm1 to something like this:

using module ".\private\completions\Completers.psm1"

# Dotsource each function in Private and Public
foreach ($directory in @('private', 'public')) {
    Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object {
        . $_.FullName
    }
}
  1. I added some additional script-wide default colors in Initialize-DefaultColors.ps1:
$script:AccentColor             =  [Spectre.Console.Color]::Blue
$script:DefaultValueColor       =  [Spectre.Console.Color]::Grey
$script:DefaultTableHeaderColor =  [Spectre.Console.Color]::Grey82
$script:DefaultTableTextColor   =  [Spectre.Console.Color]::Grey39

Note: $script:DefaultTableTextColor does nothing right now.

Future Plans


  1. Implement a -TextColor parameter to Format-SpectreTable.
  2. Implement a -TitleColor parameter to Format-SpectreTable.
  3. Change the parameter name -Color in Format-SpectreTable to -TableBorderColor
  4. These changes would allow full and complete control over the coloring of tables (Title, Header, Text, Table Border). And with the new -AllowMarkup switch, we can also colorize the text of any cell in the table,

Comments / Discussions / Critiques / Proposed Changes are welcome!

futuremotiondev commented 7 months ago

Update: Modified the erroneous dot-source statement block:

Before


using module ".\private\completions\Completers.psm1"

# Dotsource default configuration
Get-ChildItem -Path "$PSScriptRoot\private\defaults\*.ps1" -Recurse | ForEach-Object {
    . $_.FullName
}

# Dotsource each function in Private and Public
foreach ($directory in @('private', 'public')) {
    Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object {
        . $_.FullName
    }
}

After


using module ".\private\completions\Completers.psm1"

# Dotsource each function in Private and Public
foreach ($directory in @('private', 'public')) {
    Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object {
        . $_.FullName
    }
}

I updated the PR to include this change. I have no idea what I was doing with that first dot-source block. The second one already imports Initialize-DefaultColors.ps1 Apologies for the bad code.

ShaunLawrie commented 6 months ago

When I reimplemented the table colors from your PR I just left these where they were for now in the psm1. If we need theming it can be added in another change.

The table colors can be set with Set-SpectreColors and you could pass a splat to it as a theme at the moment.

https://pwshspectreconsole.com/reference/config/set-spectrecolors/

futuremotiondev commented 6 months ago

Really sorry for not being more active on this!

After taking some time away from coding I spent all day/night working on a C# helper library for the module. Right now it's specifically focused on extending the capabilities of formatting tables. You can pass it objects, lists containing objects, scalar arrays, whatever - and it will handle it all gracefully in the same manner that the module works now.

You can also add columns manually, specify the width of each column by percentage or direct values. You can specify colors for any component, etc.

There is also a ColorValue class that handles all conversions between SpectreConsole color names, HEX codes, and RGB ValueTuples.

Lastly I put together all the enums associated with the library. There is also a public facing dictionary that associates all colors with their hex values, RGB values, and names.

Here's some links:

https://github.com/fmotion1/PwshSpectreConsoleHelpers https://github.com/fmotion1/PwshSpectreConsoleHelpers/blob/main/SpectreTableHelpers.cs https://github.com/fmotion1/PwshSpectreConsoleHelpers/blob/main/SpectreColorValue.cs

Took forever to get it to build successfully but finally nailed it with no warnings / errors.

I still have to test it all - really tired after this marathon of coding. Let me know what you think.

ShaunLawrie commented 6 months ago

Really sorry for not being more active on this!

Not a problem. You can do as little or as much as you want it's just a bit of fun. I appreciate all your inputs, I'm learning a lot on this project

ShaunLawrie commented 6 months ago

I'll have to take a better look when I'm not so tired. I'm not sure at the moment what your use case is because there is so much at once 😄

For where you're using enums I'm using a different approach in the library to grab them at runtime:

[Spectre.Console.TableBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name

So they can be used as argument completers like this: Animation

trackd commented 6 months ago

Really sorry for not being more active on this!

After taking some time away from coding I spent all day/night working on a C# helper library for the module. Right now it's specifically focused on extending the capabilities of formatting tables. You can pass it objects, lists containing objects, scalar arrays, whatever - and it will handle it all gracefully in the same manner that the module works now.

You can also add columns manually, specify the width of each column by percentage or direct values. You can specify colors for any component, etc.

There is also a ColorValue class that handles all conversions between SpectreConsole color names, HEX codes, and RGB ValueTuples.

Lastly I put together all the enums associated with the library. There is also a public facing dictionary that associates all colors with their hex values, RGB values, and names.

Here's some links:

https://github.com/fmotion1/PwshSpectreConsoleHelpers https://github.com/fmotion1/PwshSpectreConsoleHelpers/blob/main/SpectreTableHelpers.cs https://github.com/fmotion1/PwshSpectreConsoleHelpers/blob/main/SpectreColorValue.cs

Took forever to get it to build successfully but finally nailed it with no warnings / errors.

I still have to test it all - really tired after this marathon of coding. Let me know what you think.

Nice !

I'm still learning C# but I'm actually working on something very similar. https://github.com/trackd/PwshSpectreConsole.VT

I'm trying to convert the VT to Spectre color logic into pure C# cmdlet.

but it's overly complicated at the moment, i kinda feel like if i structure my objects differently i can make it easier but i havnt really figured out how.

theres too many steps from a VT string to a text / markup object..

multi color strings is really where it gets dicey 😅 kinda tempted to say thats just not supported because of the complexity but i also kind of want to figure it out because its a fun problem.

I'd love some help figuring out the "cheapest" (performance wise) way to convert.

I looked over your code briefly but havnt had a chance to look in detail.

I do think the latest version (1.7) solves a few of the issues that (it looks like) your trying to fix in your C#?

I went a little bit of a different route than you, i load the Spectre library directly in my project so i can access all the methods and properties like colors it does make it a bit easier.

my code is a bit of a mess at the moment with some unused or just me testing things out, it's quite a volatile codebase 🤣 feel free to copy / use / pr