Skip to content

Commit

Permalink
works, but needs some cleanup and perhaps rename the gif cmdlet and r…
Browse files Browse the repository at this point in the history
…emove the show one..
  • Loading branch information
trackd committed Nov 22, 2024
1 parent 58cc0e0 commit 5c5ae0e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
14 changes: 14 additions & 0 deletions module/Sixel.Types.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Types>
<Type>
<Name>Sixel.Terminal.Models.SixelGif</Name>
<Members>
<ScriptMethod>
<Name>ToString</Name>
<Script>
$cts = [System.Threading.CancellationTokenSource]::new()
[Sixel.Protocols.GifToSixel]::PlaySixelGif($this, 0, $cts.Token)
</Script>
</ScriptMethod>
</Members>
</Type>
</Types>
23 changes: 23 additions & 0 deletions module/Sixel.format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Configuration>
<ViewDefinitions>
<View>
<Name>Sixel.Terminal.Models.SixelGif</Name>
<ViewSelectedBy>
<TypeName>Sixel.Terminal.Models.SixelGif</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
$_.ToString()
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
4 changes: 2 additions & 2 deletions module/Sixel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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')
FormatsToProcess = @('Sixel.format.ps1xml')
TypesToProcess = @('Sixel.Types.ps1xml')
PrivateData = @{
PSData = @{
Tags = @(
Expand All @@ -36,6 +38,4 @@
# A missing or $null entry is equivalent to specifying the wildcard *. declare unused with @() for better perf.
FunctionsToExport = @()
VariablesToExport = @()
FormatsToProcess = @()
TypesToProcess = @()
}
24 changes: 6 additions & 18 deletions src/Sixel/Protocols/gif.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static SixelGif ConvertGifToSixel(Image<Rgba32> image, int maxColors, in
for (int i = 0; i < frameCount; i++)
{
var targetFrame = image.Frames[i];
gif.Sixel.Add(Sixel.FrameToSixelString(targetFrame, false));
gif.Sixel.Add(Sixel.FrameToSixelString(targetFrame, true));
}
return gif;
}
Expand All @@ -62,41 +62,29 @@ public static void PlaySixelGif(SixelGif gif, int LoopCount = 0, CancellationTok
{
gif.LoopCount = LoopCount;
}
(int positionX, int positionY) = Console.GetCursorPosition();
Console.CursorVisible = false;
int endPositionY = positionY + gif.Height + 2;
// check if endPositionY is greater than the console buffer height, set it at the bottom then
if (endPositionY > Console.BufferHeight)
{
endPositionY = Console.BufferHeight - 1;
// make room for the gif to render scroll to make room for the gif
// need a crossplatform way for this..
// Console.MoveBufferArea(0, positionY, Console.BufferWidth, endPositionY - positionY, 0, positionY + 1);
}
// hack to remove the padding from the formatter
int height = gif.Height - 2;
try
{
for (int i = 0; i < gif.LoopCount; i++)
{
foreach (var sixel in gif.Sixel)
{
// Check for cancellation
if (cancellationToken.IsCancellationRequested)
{
Console.SetCursorPosition(positionX, endPositionY);
Console.Write($"{Constants.ESC}[{height}B");
Console.CursorVisible = true;
return;
}
Console.SetCursorPosition(positionX, positionY);
Console.Write(sixel);
Thread.Sleep(gif.Delay);
Console.Write(sixel);
}
}
}
finally
{
// Move the cursor to avoid overlapping output
Console.SetCursorPosition(positionX, endPositionY);
// Ensure the cursor visibility is restored
Console.Write($"{Constants.ESC}[{height}B");
Console.CursorVisible = true;
}
}
Expand Down
21 changes: 15 additions & 6 deletions src/Sixel/Terminal/Models/SixelGif.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ namespace Sixel.Terminal.Models;
/// </summary>
public class SixelGif
{
/// <summary>
/// The sixel data for each frame of the gif.
/// </summary>
public List<string> Sixel { get; set; } = new List<string>();

/// <summary>
/// The delay in milliseconds between each frame.
/// </summary>
Expand All @@ -18,14 +15,26 @@ public class SixelGif
/// The number of times the gif should loop.
/// </summary>
public int LoopCount { get; set; }

/// <summary>
/// The height of the gif, in characters.
/// </summary>
public int Height { get; set; }

/// <summary>
/// The audio data for the gif.
/// </summary>
// public string? Audio { get; set; }
// public static string ToString()
/// <summary>
/// The sixel data for each frame of the gif.
/// </summary>
public List<string> Sixel { get; set; } = new List<string>();
/// <summary>
/// this is just a hacky way to autoplay when outputting the object to the console.
/// </summary>
/// <returns>Console Write :|</returns>
// public override string ToString()
// {
// GifToSixel.PlaySixelGif(this);
// return string.Empty;
// }
}

0 comments on commit 5c5ae0e

Please sign in to comment.