From d405ed6e49bd1a2dcd3639a5c469d59feeb34609 Mon Sep 17 00:00:00 2001 From: teramako Date: Thu, 15 May 2025 15:16:11 +0000 Subject: [PATCH] Fix `Sixel.GetCtrlSeqResponse()` #26 - Wait until key is input - If no input is received, time out and return an empty string --- src/Sixel.Supported.cs | 55 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Sixel.Supported.cs b/src/Sixel.Supported.cs index cdeb32d..cedee8b 100644 --- a/src/Sixel.Supported.cs +++ b/src/Sixel.Supported.cs @@ -41,7 +41,7 @@ public static bool IsSyncSupported() // Should get something like: ^[[?2026;1$yc // The "1" indicates synchronized output support (may receive 0 in its place, or nothing at all) DebugPrint($"IsSyncSupported: ^[{CSI_SYNC_OUTPUT} => ", ConsoleColor.DarkGray); - var response = GetCtrlSeqResponse(CSI_SYNC_OUTPUT); + var response = GetCtrlSeqResponse(CSI_SYNC_OUTPUT, 'y'); return !(response == default || response.Contains("2026;0$", StringComparison.Ordinal)); } @@ -191,27 +191,42 @@ public static ReadOnlySpan GetCtrlSeqResponse(string ctrlSeq, params char[ var ends = endChars.Length > 0 ? endChars : [ctrlSeq[^1]]; var response = new StringBuilder(); - Task.WaitAll([ - Task.Run(() => +#if DEBUG + var start = DateTime.Now; +#endif + + async Task ReadKeys(CancellationToken ct) + { + while (!Console.KeyAvailable) { - do - { - char c = Console.ReadKey(true).KeyChar; - DebugPrint($"{(char.IsControl(c) ? $"\\{(char)(Convert.ToByte(c) + 0x40)}" : c)}", - char.IsControl(c) ? ConsoleColor.Magenta : ConsoleColor.Red); - if (ends.Contains(c)) - break; - if (!char.IsControl(c)) - response.Append(c); - } - while (Console.KeyAvailable); - DebugPrint(string.Empty, lf: true); - }), - Task.Run(() => + ct.ThrowIfCancellationRequested(); + await Task.Delay(1, ct); + } + do { - Console.Out.Write($"{ESC}{ctrlSeq}"); - }) - ]); + char c = Console.ReadKey(true).KeyChar; + DebugPrint($"{(char.IsControl(c) ? $"\\{(char)(Convert.ToByte(c) + 0x40)}" : c)}", + char.IsControl(c) ? ConsoleColor.Magenta : ConsoleColor.Red); + if (ends.Contains(c)) + break; + if (!char.IsControl(c)) + response.Append(c); + } + while (Console.KeyAvailable); + DebugPrint($" ({(DateTime.Now - start).TotalMilliseconds}ms)", ConsoleColor.DarkGray, true); + } + + try + { + // Timeout: 100 ms + var cts = new CancellationTokenSource(100); + + using var readTask = ReadKeys(cts.Token); + Console.Out.Write($"{ESC}{ctrlSeq}"); + readTask.Wait(cts.Token); + } + catch (OperationCanceledException) { } + return response.ToString(); } }