Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trackd committed Nov 23, 2024
1 parent 41c8cbb commit 4902c0f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
8 changes: 7 additions & 1 deletion module/Sixel.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
$_ | Show-SixelGif
# this is a hack to not have to expose the orchestrating cmdlet and just have it play automatically.
# probably not worth it, but it's a fun experiment.
$cmdlet = [System.Management.Automation.CmdletInfo]::new(
'Show-SixelGif',
[Sixel.Cmdlet.ShowSixelGifCmdlet]
)
$_ | &amp; $cmdlet
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
Expand Down
4 changes: 2 additions & 2 deletions module/Sixel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
Author = 'trackd, ShaunLawrie'
Copyright = '(c) trackd. All rights reserved.'
Description = 'Convert images to Sixel format and display them in the terminal, Requires a terminal with Sixel support. (Windows Terminal v1.22.2912.0 or later), Inline Image Protocol or Kitty Graphics support.'
CmdletsToExport = @('ConvertTo-Sixel', 'New-SixelGif','Show-SixelGif')
AliasesToExport = @('cts', 'ConvertTo-InlineImage')
CmdletsToExport = @('ConvertTo-Sixel', 'ConvertTo-SixelGif')
AliasesToExport = @('cts', 'ConvertTo-InlineImage','gif')
FormatsToProcess = @('Sixel.format.ps1xml')
TypesToProcess = @()
PrivateData = @{
Expand Down
9 changes: 3 additions & 6 deletions src/Sixel/Cmdlet/SixelGifcmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace Sixel.Cmdlet;

[Cmdlet(VerbsCommon.New, "SixelGif", DefaultParameterSetName = "Path")]
[Cmdlet(VerbsData.ConvertTo, "SixelGif", DefaultParameterSetName = "Path")]
[Alias("gif")]
[OutputType(typeof(SixelGif))]
public sealed class NewSixelGifCmdlet : PSCmdlet
public sealed class ConvertSixelGifCmdlet : PSCmdlet
{
[Parameter(
HelpMessage = "A path to a local image to convert to sixel.",
Expand Down Expand Up @@ -90,7 +90,6 @@ protected override void ProcessRecord()
}

[Cmdlet(VerbsCommon.Show, "SixelGif", DefaultParameterSetName = "Path")]
[Alias("play")]
public sealed class ShowSixelGifCmdlet : PSCmdlet
{
[Parameter(
Expand All @@ -116,7 +115,6 @@ protected override void ProcessRecord()
// Handle Ctrl+C
Console.CancelKeyPress += (sender, args) =>
{
// Prevent the process from terminating
args.Cancel = true;
cts.Cancel();
};
Expand All @@ -126,8 +124,7 @@ protected override void ProcessRecord()
}
else
{
// GifToSixel.PlaySixelGif(Gif);
GifToSixel.PlaySixelGif(Gif, cancellationToken: cts.Token);
GifToSixel.PlaySixelGif(Gif, CT: cts.Token);
}
}
catch (Exception ex)
Expand Down
11 changes: 7 additions & 4 deletions src/Sixel/Protocols/gif.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,27 @@ private static SixelGif ConvertGifToSixel(Image<Rgba32> image, int maxColors, in
}
return gif;
}
public static void PlaySixelGif(SixelGif gif, int LoopCount = 0, CancellationToken cancellationToken = default)
public static void PlaySixelGif(SixelGif gif, int LoopCount = 0, CancellationToken CT = default)
{
if (LoopCount > 0)
{
// override the loop count on the object.
gif.LoopCount = LoopCount;
}
Console.CursorVisible = false;
// custom padding before gif.
Console.Write($"{Constants.ESC}[1B");
// hack to remove the padding from the formatter
// the formatter adds 2 lines of padding at the end.
int height = gif.Height - 2;
try
{
for (int i = 0; i < gif.LoopCount; i++)
{
foreach (var sixel in gif.Sixel)
{
if (cancellationToken.IsCancellationRequested)
if (CT.IsCancellationRequested)
{
Console.Write($"{Constants.ESC}[{height}B");
Console.CursorVisible = true;
return;
}
Thread.Sleep(gif.Delay);
Expand All @@ -83,6 +85,7 @@ public static void PlaySixelGif(SixelGif gif, int LoopCount = 0, CancellationTok
}
finally
{
// move the cursor below the gif.
Console.Write($"{Constants.ESC}[{height}B");
Console.CursorVisible = true;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Sixel/Terminal/Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Load
/// <summary>
/// Load an image and convert it to a terminal compatible format.
/// </summary>
/// implement type <T> to allow for more image types..
public static string? ConsoleImage(ImageProtocol imageProtocol, Stream imageStream, int maxColors, int width, bool Force)
{
if (imageProtocol == ImageProtocol.Sixel)
Expand All @@ -19,6 +20,12 @@ public static class Load
throw new InvalidOperationException("Terminal does not support sixel, override with -Force (Windows Terminal needs Preview release for Sixel Support)");
}
using var _image = Image.Load<Rgba32>(imageStream);
// move it here? decisions decisions.. we could yeet the gif cmdlet and just make it a param.
// check if the image is a gif
// if (_image.Frames.Count > 1)
// {
// return Protocols.GifToSixel.LoadGif(imageStream, maxColors, width, 3);
// }
return Protocols.Sixel.ImageToSixel(_image, maxColors, width);
}
if (imageProtocol == ImageProtocol.InlineImageProtocol)
Expand Down

0 comments on commit 4902c0f

Please sign in to comment.