|
| 1 | +using Sixel.Terminal; |
| 2 | +using Sixel.Terminal.Models; |
| 3 | +using Sixel.Protocols; |
| 4 | +using System.Management.Automation; |
| 5 | + |
| 6 | +namespace Sixel.Cmdlet; |
| 7 | + |
| 8 | +[Cmdlet(VerbsCommon.New, "SixelGif", DefaultParameterSetName = "Path")] |
| 9 | +[Alias("gif")] |
| 10 | +[OutputType(typeof(SixelGif))] |
| 11 | +public sealed class NewSixelGifCmdlet : PSCmdlet |
| 12 | +{ |
| 13 | + [Parameter( |
| 14 | + HelpMessage = "A path to a local image to convert to sixel.", |
| 15 | + Mandatory = true, |
| 16 | + ValueFromPipelineByPropertyName = true, |
| 17 | + Position = 0, |
| 18 | + ParameterSetName = "Path" |
| 19 | + )] |
| 20 | + [ValidateNotNullOrEmpty] |
| 21 | + [Alias("FullName")] |
| 22 | + public string Path { get; set; } = null!; |
| 23 | + |
| 24 | + [Parameter( |
| 25 | + HelpMessage = "A URL of the image to download and convert to sixel.", |
| 26 | + Mandatory = true, |
| 27 | + ValueFromPipeline = true, |
| 28 | + ParameterSetName = "Url" |
| 29 | + )] |
| 30 | + [ValidateNotNullOrEmpty] |
| 31 | + [Alias("Uri")] |
| 32 | + public string Url { get; set; } = null!; |
| 33 | + |
| 34 | + [Parameter( |
| 35 | + HelpMessage = "The maximum number of colors to use in the image." |
| 36 | + )] |
| 37 | + [ValidateRange(1, 256)] |
| 38 | + public int MaxColors { get; set; } = 256; |
| 39 | + |
| 40 | + [Parameter( |
| 41 | + HelpMessage = "Width of the image in character cells, the height will be scaled to maintain aspect ratio." |
| 42 | + )] |
| 43 | + [ValidateTerminalWidth()] |
| 44 | + public int Width { get; set; } |
| 45 | + |
| 46 | + [Parameter( |
| 47 | + HelpMessage = "Force the command to attempt to output sixel data even if the terminal does not support sixel." |
| 48 | + )] |
| 49 | + public SwitchParameter Force { get; set; } |
| 50 | + |
| 51 | + [Parameter( |
| 52 | + HelpMessage = "The number of times to loop the gif." |
| 53 | +)] |
| 54 | + [ValidateRange(1, 256)] |
| 55 | + public int LoopCount { get; set; } = 3; |
| 56 | + protected override void ProcessRecord() |
| 57 | + { |
| 58 | + try |
| 59 | + { |
| 60 | + Stream? imageStream = null; |
| 61 | + switch (ParameterSetName) |
| 62 | + { |
| 63 | + case "Path": |
| 64 | + { |
| 65 | + var resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(Path)[0].Path; |
| 66 | + imageStream = new FileStream(resolvedPath, FileMode.Open, FileAccess.Read); |
| 67 | + break; |
| 68 | + } |
| 69 | + case "Url": |
| 70 | + { |
| 71 | + using var client = new HttpClient(); |
| 72 | + var response = client.GetAsync(Url).Result; |
| 73 | + response.EnsureSuccessStatusCode(); |
| 74 | + imageStream = response.Content.ReadAsStream(); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + if (imageStream is null) return; |
| 79 | + using (imageStream) |
| 80 | + { |
| 81 | + WriteObject(GifToSixel.LoadGif(imageStream, MaxColors, Width, LoopCount)); |
| 82 | + } |
| 83 | + } |
| 84 | + catch (Exception ex) |
| 85 | + { |
| 86 | + WriteError(new ErrorRecord(ex, "SixelError", ErrorCategory.NotSpecified, null)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +[Cmdlet(VerbsCommon.Show, "SixelGif", DefaultParameterSetName = "Path")] |
| 92 | +[Alias("play")] |
| 93 | +public sealed class ShowSixelGifCmdlet : PSCmdlet |
| 94 | +{ |
| 95 | + [Parameter( |
| 96 | + HelpMessage = "SixelGif object to play.", |
| 97 | + Mandatory = true, |
| 98 | + ValueFromPipeline = true, |
| 99 | + Position = 0 |
| 100 | + )] |
| 101 | + [ValidateNotNullOrEmpty] |
| 102 | + public SixelGif? Gif { get; set; } |
| 103 | + |
| 104 | + [Parameter( |
| 105 | + HelpMessage = "The number of times to loop the gif." |
| 106 | + )] |
| 107 | + [ValidateRange(1, 256)] |
| 108 | + public int LoopCount { get; set; } = 0; |
| 109 | + protected override void ProcessRecord() |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + if (Gif is null) return; |
| 114 | + if (LoopCount > 0) |
| 115 | + { |
| 116 | + GifToSixel.PlaySixelGif(Gif, LoopCount); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + GifToSixel.PlaySixelGif(Gif); |
| 121 | + } |
| 122 | + } |
| 123 | + catch (Exception ex) |
| 124 | + { |
| 125 | + WriteError(new ErrorRecord(ex, "SixelError", ErrorCategory.NotSpecified, null)); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments