Skip to content

Commit

Permalink
Add format option to get-spectreimage
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Jan 4, 2025
1 parent f14a393 commit dc182ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 74 deletions.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
25 changes: 23 additions & 2 deletions PwshSpectreConsole/public/images/Get-SpectreImage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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://")) {
Expand All @@ -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
Expand Down

0 comments on commit dc182ab

Please sign in to comment.