From 4902c0fe0ee6d1bf25505353085ceb57bca8d9c9 Mon Sep 17 00:00:00 2001 From: trackd Date: Sat, 23 Nov 2024 19:46:12 +0100 Subject: [PATCH] fixes --- module/Sixel.format.ps1xml | 8 +++++++- module/Sixel.psd1 | 4 ++-- src/Sixel/Cmdlet/SixelGifcmdlet.cs | 9 +++------ src/Sixel/Protocols/gif.cs | 11 +++++++---- src/Sixel/Terminal/Load.cs | 7 +++++++ 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/module/Sixel.format.ps1xml b/module/Sixel.format.ps1xml index ccb5c46..c74d93d 100644 --- a/module/Sixel.format.ps1xml +++ b/module/Sixel.format.ps1xml @@ -11,7 +11,13 @@ - $_ | 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] + ) + $_ | & $cmdlet diff --git a/module/Sixel.psd1 b/module/Sixel.psd1 index 67dac3f..f57e7a5 100644 --- a/module/Sixel.psd1 +++ b/module/Sixel.psd1 @@ -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 = @{ diff --git a/src/Sixel/Cmdlet/SixelGifcmdlet.cs b/src/Sixel/Cmdlet/SixelGifcmdlet.cs index 687e3ff..bc94552 100644 --- a/src/Sixel/Cmdlet/SixelGifcmdlet.cs +++ b/src/Sixel/Cmdlet/SixelGifcmdlet.cs @@ -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.", @@ -90,7 +90,6 @@ protected override void ProcessRecord() } [Cmdlet(VerbsCommon.Show, "SixelGif", DefaultParameterSetName = "Path")] -[Alias("play")] public sealed class ShowSixelGifCmdlet : PSCmdlet { [Parameter( @@ -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(); }; @@ -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) diff --git a/src/Sixel/Protocols/gif.cs b/src/Sixel/Protocols/gif.cs index da373c0..ff8d61d 100644 --- a/src/Sixel/Protocols/gif.cs +++ b/src/Sixel/Protocols/gif.cs @@ -55,14 +55,18 @@ private static SixelGif ConvertGifToSixel(Image 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 { @@ -70,10 +74,8 @@ public static void PlaySixelGif(SixelGif gif, int LoopCount = 0, CancellationTok { 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); @@ -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; } diff --git a/src/Sixel/Terminal/Load.cs b/src/Sixel/Terminal/Load.cs index 17d615f..225fddf 100644 --- a/src/Sixel/Terminal/Load.cs +++ b/src/Sixel/Terminal/Load.cs @@ -10,6 +10,7 @@ public static class Load /// /// Load an image and convert it to a terminal compatible format. /// + /// implement type to allow for more image types.. public static string? ConsoleImage(ImageProtocol imageProtocol, Stream imageStream, int maxColors, int width, bool Force) { if (imageProtocol == ImageProtocol.Sixel) @@ -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(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)