From dc182abcff0e3c89093f398e82d40500d6510222 Mon Sep 17 00:00:00 2001 From: Shaun Lawrie Date: Sun, 5 Jan 2025 00:13:58 +1300 Subject: [PATCH] Add format option to get-spectreimage --- .../Config/Test-SpectreSixelSupport.mdx | 62 ------------------- .../Config/_Test-SpectreSixelSupport.sha256 | 1 - PwshSpectreConsole/PwshSpectreConsole.psd1 | 2 +- .../Test-SpectreSixelSupport.ps1 | 8 --- .../public/images/Get-SpectreImage.ps1 | 25 +++++++- 5 files changed, 24 insertions(+), 74 deletions(-) delete mode 100644 PwshSpectreConsole.Docs/src/content/docs/reference/Config/Test-SpectreSixelSupport.mdx delete mode 100644 PwshSpectreConsole.Docs/src/content/docs/reference/Config/_Test-SpectreSixelSupport.sha256 rename PwshSpectreConsole/{public/config => private}/Test-SpectreSixelSupport.ps1 (64%) diff --git a/PwshSpectreConsole.Docs/src/content/docs/reference/Config/Test-SpectreSixelSupport.mdx b/PwshSpectreConsole.Docs/src/content/docs/reference/Config/Test-SpectreSixelSupport.mdx deleted file mode 100644 index 7e810286..00000000 --- a/PwshSpectreConsole.Docs/src/content/docs/reference/Config/Test-SpectreSixelSupport.mdx +++ /dev/null @@ -1,62 +0,0 @@ ---- -sidebar: - badge: - text: Updated - variant: note -title: Test-SpectreSixelSupport ---- - - - - - - - -import Asciinema from '../../../../components/Asciinema.astro' -import testspectresixelsupportExample1 from '../../../../assets/examples/testspectresixelsupportExample1.cast?url' - -### Description - -Tests if the terminal supports Sixel graphics. Sixel allows the terminal to display images. -Windows Terminal Preview and other terminals support sixel, see https://www.arewesixelyet.com/ for more. -Returns $true if the terminal supports Sixel graphics, otherwise $false. - - - ---- - - -### Examples - - -**Example 1** -This example demonstrates how to set the accent color and default value color for Spectre Console. - - - -```powershell -if (Test-SpectreSixelSupport) { - Write-SpectreHost "Sixel graphics are supported :)" -} else { - Write-SpectreHost "Sixel graphics are not supported because this ran in Github Actions :(" -} -``` - - - ---- - - -### Syntax -```powershell -Test-SpectreSixelSupport [] -``` diff --git a/PwshSpectreConsole.Docs/src/content/docs/reference/Config/_Test-SpectreSixelSupport.sha256 b/PwshSpectreConsole.Docs/src/content/docs/reference/Config/_Test-SpectreSixelSupport.sha256 deleted file mode 100644 index d37ccb1b..00000000 --- a/PwshSpectreConsole.Docs/src/content/docs/reference/Config/_Test-SpectreSixelSupport.sha256 +++ /dev/null @@ -1 +0,0 @@ -6C0478CA7A0A02D5A590EFC2CC6ABEC690D0630A8E3DEEC52D2CCC00D4E5F222 \ No newline at end of file diff --git a/PwshSpectreConsole/PwshSpectreConsole.psd1 b/PwshSpectreConsole/PwshSpectreConsole.psd1 index 162afaba..9db18ab8 100644 --- a/PwshSpectreConsole/PwshSpectreConsole.psd1 +++ b/PwshSpectreConsole/PwshSpectreConsole.psd1 @@ -92,7 +92,7 @@ FunctionsToExport = 'Add-SpectreJob', 'Format-SpectreBarChart', 'Format-SpectreAligned', 'Out-SpectreHost', 'Add-SpectreTableRow', 'Invoke-SpectreLive', 'Format-SpectreException', 'Get-SpectreDemoFeatures', 'Get-SpectreRenderableSize', - 'Read-SpectreSelectionGrouped', 'Test-SpectreSixelSupport' + 'Read-SpectreSelectionGrouped' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/PwshSpectreConsole/public/config/Test-SpectreSixelSupport.ps1 b/PwshSpectreConsole/private/Test-SpectreSixelSupport.ps1 similarity index 64% rename from PwshSpectreConsole/public/config/Test-SpectreSixelSupport.ps1 rename to PwshSpectreConsole/private/Test-SpectreSixelSupport.ps1 index 2069d8d7..59b61a90 100644 --- a/PwshSpectreConsole/public/config/Test-SpectreSixelSupport.ps1 +++ b/PwshSpectreConsole/private/Test-SpectreSixelSupport.ps1 @@ -6,14 +6,6 @@ function Test-SpectreSixelSupport { Tests if the terminal supports Sixel graphics. Sixel allows the terminal to display images. Windows Terminal Preview and other terminals support sixel, see https://www.arewesixelyet.com/ for more. Returns $true if the terminal supports Sixel graphics, otherwise $false. - .EXAMPLE - # **Example 1** - # This example demonstrates how to set the accent color and default value color for Spectre Console. - if (Test-SpectreSixelSupport) { - Write-SpectreHost "Sixel graphics are supported :)" - } else { - Write-SpectreHost "Sixel graphics are not supported because this ran in Github Actions :(" - } #> [Reflection.AssemblyMetadata("title", "Test-SpectreSixelSupport")] param () diff --git a/PwshSpectreConsole/public/images/Get-SpectreImage.ps1 b/PwshSpectreConsole/public/images/Get-SpectreImage.ps1 index 494233e0..56588ccc 100644 --- a/PwshSpectreConsole/public/images/Get-SpectreImage.ps1 +++ b/PwshSpectreConsole/public/images/Get-SpectreImage.ps1 @@ -18,6 +18,10 @@ function Get-SpectreImage { .PARAMETER MaxWidth The maximum width of the image. If not specified, the image will be displayed at its original size. + .PARAMETER Format + The preferred format to use when rendering the image. + If not specified, the image will be rendered using Sixel if the terminal supports it, otherwise it will use Canvas. + .EXAMPLE # **Example 1** # When Sixel is not supported the image will use the standard Canvas renderer which draws the image using character cells to represent the image. @@ -44,7 +48,9 @@ function Get-SpectreImage { [Reflection.AssemblyMetadata("title", "Get-SpectreImage")] param ( [string] $ImagePath, - [int] $MaxWidth + [int] $MaxWidth, + [ValidateSet("Auto", "Sixel", "Canvas")] + [string] $Format = "Auto" ) if ($ImagePath.StartsWith("http://") -or $ImagePath.StartsWith("https://")) { @@ -61,7 +67,22 @@ function Get-SpectreImage { throw "The specified image path '$resolvedImagePath' does not exist." } - $image = (Test-SpectreSixelSupport) ? [Spectre.Console.SixelImage]::new($imagePathResolved) : [Spectre.Console.CanvasImage]::new($imagePathResolved) + $image = $null + if ($Format -eq "Auto") { + if (Test-SpectreSixelSupport) { + $image = [Spectre.Console.SixelImage]::new($imagePathResolved) + } else { + $image = [Spectre.Console.CanvasImage]::new($imagePathResolved) + } + } elseif ($Format -eq "Sixel") { + if (Test-SpectreSixelSupport) { + $image = [Spectre.Console.SixelImage]::new($imagePathResolved) + } else { + throw "Sixel format is not supported in this terminal." + } + } elseif ($Format -eq "Canvas") { + $image = [Spectre.Console.CanvasImage]::new($imagePathResolved) + } if ($MaxWidth) { $image.MaxWidth = $MaxWidth