SixelSupportDetector.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Text.RegularExpressions;
  2. namespace Terminal.Gui;
  3. /* TODO : Depends on https://github.com/gui-cs/Terminal.Gui/pull/3768
  4. /// <summary>
  5. /// Uses Ansi escape sequences to detect whether sixel is supported
  6. /// by the terminal.
  7. /// </summary>
  8. public class SixelSupportDetector : ISixelSupportDetector
  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 = IsSixelSupportedByDar ();
  21. if (result.IsSupported)
  22. {
  23. if (TryGetResolutionDirectly (out var res))
  24. {
  25. result.Resolution = res;
  26. }
  27. else if(TryComputeResolution(out res))
  28. {
  29. result.Resolution = res;
  30. }
  31. result.SupportsTransparency = IsWindowsTerminal () || IsXtermWithTransparency ();
  32. }
  33. return result;
  34. }
  35. private bool TryGetResolutionDirectly (out Size resolution)
  36. {
  37. // Expect something like:
  38. //<esc>[6;20;10t
  39. if (AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_RequestSixelResolution, out var response))
  40. {
  41. // Terminal supports directly responding with resolution
  42. var match = Regex.Match (response.Response, @"\[\d+;(\d+);(\d+)t$");
  43. if (match.Success)
  44. {
  45. if (int.TryParse (match.Groups [1].Value, out var ry) &&
  46. int.TryParse (match.Groups [2].Value, out var rx))
  47. {
  48. resolution = new Size (rx, ry);
  49. return true;
  50. }
  51. }
  52. }
  53. resolution = default;
  54. return false;
  55. }
  56. private bool TryComputeResolution (out Size resolution)
  57. {
  58. // Fallback to window size in pixels and characters
  59. if (AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_RequestWindowSizeInPixels, out var pixelSizeResponse)
  60. && AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_ReportTerminalSizeInChars, out var charSizeResponse))
  61. {
  62. // Example [4;600;1200t
  63. var pixelMatch = Regex.Match (pixelSizeResponse.Response, @"\[\d+;(\d+);(\d+)t$");
  64. // Example [8;30;120t
  65. var charMatch = Regex.Match (charSizeResponse.Response, @"\[\d+;(\d+);(\d+)t$");
  66. if (pixelMatch.Success && charMatch.Success)
  67. {
  68. // Extract pixel dimensions
  69. if (int.TryParse (pixelMatch.Groups [1].Value, out var pixelHeight)
  70. && int.TryParse (pixelMatch.Groups [2].Value, out var pixelWidth)
  71. &&
  72. // Extract character dimensions
  73. int.TryParse (charMatch.Groups [1].Value, out var charHeight)
  74. && int.TryParse (charMatch.Groups [2].Value, out var charWidth)
  75. && charWidth != 0
  76. && charHeight != 0) // Avoid divide by zero
  77. {
  78. // Calculate the character cell size in pixels
  79. var cellWidth = (int)Math.Round ((double)pixelWidth / charWidth);
  80. var cellHeight = (int)Math.Round ((double)pixelHeight / charHeight);
  81. // Set the resolution based on the character cell size
  82. resolution = new Size (cellWidth, cellHeight);
  83. return true;
  84. }
  85. }
  86. }
  87. resolution = default;
  88. return false;
  89. }
  90. private bool IsSixelSupportedByDar ()
  91. {
  92. return AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_SendDeviceAttributes, out AnsiEscapeSequenceResponse darResponse)
  93. ? darResponse.Response.Split (';').Contains ("4")
  94. : false;
  95. }
  96. private bool IsWindowsTerminal ()
  97. {
  98. return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable ("WT_SESSION"));;
  99. }
  100. private bool IsXtermWithTransparency ()
  101. {
  102. // Check if running in real xterm (XTERM_VERSION is more reliable than TERM)
  103. var xtermVersionStr = Environment.GetEnvironmentVariable ("XTERM_VERSION");
  104. // If XTERM_VERSION exists, we are in a real xterm
  105. if (!string.IsNullOrWhiteSpace (xtermVersionStr) && int.TryParse (xtermVersionStr, out var xtermVersion) && xtermVersion >= 370)
  106. {
  107. return true;
  108. }
  109. return false;
  110. }
  111. }*/