SixelSupportDetector.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Text.RegularExpressions;
  2. using Microsoft.CodeAnalysis;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Uses Ansi escape sequences to detect whether sixel is supported
  6. /// by the terminal.
  7. /// </summary>
  8. public class SixelSupportDetector
  9. {
  10. /// <summary>
  11. /// Sends Ansi escape sequences to the console to determine whether
  12. /// sixel is supported (and <see cref="SixelSupportResult.Resolution"/>
  13. /// etc).
  14. /// </summary>
  15. /// <returns>Description of sixel support, may include assumptions where
  16. /// expected response codes are not returned by console.</returns>
  17. public SixelSupportResult Detect ()
  18. {
  19. var result = new SixelSupportResult ();
  20. result.IsSupported =
  21. AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_SendDeviceAttributes, out AnsiEscapeSequenceResponse darResponse)
  22. ? darResponse.Response.Split (';').Contains ("4")
  23. : false;
  24. if (result.IsSupported)
  25. {
  26. // Expect something like:
  27. //<esc>[6;20;10t
  28. bool gotResolutionDirectly = false;
  29. if (AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_RequestSixelResolution, out var resolution))
  30. {
  31. // Terminal supports directly responding with resolution
  32. var match = Regex.Match (resolution.Response, @"\[\d+;(\d+);(\d+)t$");
  33. if (match.Success)
  34. {
  35. if (int.TryParse (match.Groups [1].Value, out var ry) &&
  36. int.TryParse (match.Groups [2].Value, out var rx))
  37. {
  38. result.Resolution = new Size (rx, ry);
  39. gotResolutionDirectly = true;
  40. }
  41. }
  42. }
  43. if (!gotResolutionDirectly)
  44. {
  45. // Fallback to window size in pixels and characters
  46. if (AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_RequestWindowSizeInPixels, out var pixelSizeResponse) &&
  47. AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_ReportTerminalSizeInChars, out var charSizeResponse))
  48. {
  49. // Example [4;600;1200t
  50. var pixelMatch = Regex.Match (pixelSizeResponse.Response, @"\[\d+;(\d+);(\d+)t$");
  51. // Example [8;30;120t
  52. var charMatch = Regex.Match (charSizeResponse.Response, @"\[\d+;(\d+);(\d+)t$");
  53. if (pixelMatch.Success && charMatch.Success)
  54. {
  55. // Extract pixel dimensions
  56. if (int.TryParse (pixelMatch.Groups [1].Value, out var pixelHeight) &&
  57. int.TryParse (pixelMatch.Groups [2].Value, out var pixelWidth) &&
  58. // Extract character dimensions
  59. int.TryParse (charMatch.Groups [1].Value, out var charHeight) &&
  60. int.TryParse (charMatch.Groups [2].Value, out var charWidth) &&
  61. charWidth != 0 && charHeight != 0) // Avoid divide by zero
  62. {
  63. // Calculate the character cell size in pixels
  64. var cellWidth = pixelWidth / charWidth;
  65. var cellHeight = pixelHeight / charHeight;
  66. // Set the resolution based on the character cell size
  67. result.Resolution = new Size (cellWidth, cellHeight);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return result;
  74. }
  75. }