SixelSupportDetector.cs 4.6 KB

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