Sixel icon indicating copy to clipboard operation
Sixel copied to clipboard

ConvertTo-Sixel fails when stdin is redirected

Open adamedx opened this issue 1 year ago • 5 comments

So if you redirect stdin, it looks ConvertTo-Sixel throws an exception:

# Using value of '-' with File parameter as that causes stdin redirection
'convertto-sixel -Force -Path \pictures\mypicture.png' | pwsh -noprofile -File -
ConvertTo-Sixel: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.

Looks like the use of Console.ReadKey is the culprit in GetControlSequenceResponse which is used in a few places to test for various terminal capabilities:

https://github.com/trackd/Sixel/blob/main/src/Sixel/Terminal/Compatibility.cs#L42

 public static string GetControlSequenceResponse(string controlSequence)
  {
    char? c;
    var response = string.Empty;

    Console.Write($"{Constants.ESC}{controlSequence}");
    do
    {
      c = Console.ReadKey(true).KeyChar;
      response += c;
    } while (c != 'c' && Console.KeyAvailable);
    return response;
  }

This fails even if you specify -Force; based on the docs for ConvertTo-Sixel, I would expect it to ignore the error and just emit whatever sequences are specified by -Protocol.

I tested a workaround where I just handled the exception and things seemed to work (though strangely it worked even without -Force :) ). I looked for ways to replace the use of ReadKey, but couldn't find anything in .net. I assume using specific terminal APIs would do it, but pretty sure we don't want to take dependencies on particular terminals, even as a fallback. IMO handling the exception and, at least in the case of -Force, trusting that the user knows what their terminal can do is sufficient.

If you're ok with the exception handling fix, I can submit a PR with the unit test (repro is trivial, so easy to write the test) -- let me know!

Thanks for sharing this module.

adamedx avatar Jul 09 '25 03:07 adamedx

Hey!

Thanks for the very detailed issue!

Yea -Force is only wired up to override the supported image protocol check.

I know theres spectreconsole/radline but havnt tested it.

I kind of want to redo GetControlSequenceResponse a bit it has a tendency to sometimes miss catching the response and mess up user input buffer.

(I think this is when powershell automatically loads the module while your typing out one of the cmdlets.. But need to test more)

Errors in that method shouldnt stop everything though..

If you know a better solution im always happy to get some help 😃

ill probably look at it soon, need to fix the sizing issue as well.

A bit curious on the use-case?

trackd avatar Jul 09 '25 15:07 trackd

was testing a bit in trackd/Sixel/tree/feature/elaborate-moon

'& .\tools\build.ps1; ipmo .\output\sixel.psd1; cts .\assets\cog.png -protocol Sixel -Force' | pwsh -noprofile -file -

basically just ignore any checks in GetControlSequenceResponse if Input or Output is redirected.

need to test a bit more before release but if you want you could try it out.

Oh and

and things seemed to work (though strangely it worked even without -Force :)

Thats probably just because it detected terminal support from env vars. Need both because not all Terminals respond correctly 🙃

trackd avatar Jul 09 '25 23:07 trackd

I did try that branch -- it worked great! Definitely makes sense to detect redirection and not bother with checks that can't work.

The env vars thing makes sense, and as you say, that mechanism isn't very robust.

I can't say my workaround was very serious -- I was testing / demoing a PowerShell-based project I'm working on for fun (https://github.com/adamedx/chatgps) and I like generating graphics (especially fractals), and this Sixel module is exactly what I was looking for because I could generate graphics inside the terminal just like I used to do in the 80's on 8-bit computers. :) My project dynamically generates the code and runs it in a separate process through redirection, so that's how I originally encountered this.

I'll definitely use your branch / main on my local box, so thanks very much for sharing that so quickly! On other systems I think start-job is probably a reasonable workaround (honestly I would have used start-job for my own out of process powershell but I am doing some pretty terrible things and pretty sure I ran into deadlocks within powershell itself, the redirection trick is cumbersome but effective).

This is really a great project btw -- not sure what inspired you to do it, but a) terminal / character stuff is pretty cool in general and b) making it native to PowerShell is the best.

adamedx avatar Jul 10 '25 05:07 adamedx

Thank you, tried out the latest from PowerShellGallery and it's working great!

adamedx avatar Sep 23 '25 02:09 adamedx

Thank you, tried out the latest from PowerShellGallery and it's working great!

Thats great! Sorry for the delay, got a bit sidetracked along the way.

Thanks for the report!

trackd avatar Sep 23 '25 17:09 trackd