2
0

NetOutput.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Microsoft.Extensions.Logging;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Implementation of <see cref="IConsoleOutput"/> that uses native dotnet
  5. /// methods e.g. <see cref="System.Console"/>
  6. /// </summary>
  7. public class NetOutput : OutputBase, IConsoleOutput
  8. {
  9. private readonly bool _isWinPlatform;
  10. /// <summary>
  11. /// Creates a new instance of the <see cref="NetOutput"/> class.
  12. /// </summary>
  13. public NetOutput ()
  14. {
  15. Logging.Logger.LogInformation ($"Creating {nameof (NetOutput)}");
  16. Console.OutputEncoding = Encoding.UTF8;
  17. PlatformID p = Environment.OSVersion.Platform;
  18. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  19. {
  20. _isWinPlatform = true;
  21. }
  22. }
  23. /// <inheritdoc/>
  24. public void Write (ReadOnlySpan<char> text)
  25. {
  26. Console.Out.Write (text);
  27. }
  28. /// <inheritdoc/>
  29. public Size GetWindowSize ()
  30. {
  31. if (ConsoleDriver.RunningUnitTests)
  32. {
  33. // For unit tests, we return a default size.
  34. return Size.Empty;
  35. }
  36. return new (Console.WindowWidth, Console.WindowHeight);
  37. }
  38. /// <inheritdoc/>
  39. public void SetCursorPosition (int col, int row) { SetCursorPositionImpl (col, row); }
  40. private Point? _lastCursorPosition;
  41. /// <inheritdoc/>
  42. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  43. {
  44. if (Application.Force16Colors)
  45. {
  46. output.Append (EscSeqUtils.CSI_SetForegroundColor (attr.Foreground.GetAnsiColorCode ()));
  47. output.Append (EscSeqUtils.CSI_SetBackgroundColor (attr.Background.GetAnsiColorCode ()));
  48. }
  49. else
  50. {
  51. EscSeqUtils.CSI_AppendForegroundColorRGB (
  52. output,
  53. attr.Foreground.R,
  54. attr.Foreground.G,
  55. attr.Foreground.B
  56. );
  57. EscSeqUtils.CSI_AppendBackgroundColorRGB (
  58. output,
  59. attr.Background.R,
  60. attr.Background.G,
  61. attr.Background.B
  62. );
  63. }
  64. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  65. }
  66. /// <inheritdoc />
  67. protected override void Write (StringBuilder output)
  68. {
  69. Console.Out.Write (output);
  70. }
  71. /// <inheritdoc />
  72. protected override bool SetCursorPositionImpl (int col, int row)
  73. {
  74. if (_lastCursorPosition is { } && _lastCursorPosition.Value.X == col && _lastCursorPosition.Value.Y == row)
  75. {
  76. return true;
  77. }
  78. _lastCursorPosition = new (col, row);
  79. if (_isWinPlatform)
  80. {
  81. // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth.
  82. try
  83. {
  84. Console.SetCursorPosition (col, row);
  85. return true;
  86. }
  87. catch (Exception)
  88. {
  89. return false;
  90. }
  91. }
  92. // + 1 is needed because non-Windows is based on 1 instead of 0 and
  93. // Console.CursorTop/CursorLeft isn't reliable.
  94. EscSeqUtils.CSI_WriteCursorPosition (Console.Out, row + 1, col + 1);
  95. return true;
  96. }
  97. /// <inheritdoc/>
  98. public void Dispose ()
  99. {
  100. }
  101. private EscSeqUtils.DECSCUSR_Style? _currentDecscusrStyle;
  102. /// <inheritdoc cref="IConsoleOutput.SetCursorVisibility"/>
  103. public override void SetCursorVisibility (CursorVisibility visibility)
  104. {
  105. if (visibility != CursorVisibility.Invisible)
  106. {
  107. if (_currentDecscusrStyle is null || _currentDecscusrStyle != (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF))
  108. {
  109. _currentDecscusrStyle = (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF);
  110. Write (EscSeqUtils.CSI_SetCursorStyle ((EscSeqUtils.DECSCUSR_Style)_currentDecscusrStyle));
  111. }
  112. Write (EscSeqUtils.CSI_ShowCursor);
  113. }
  114. else
  115. {
  116. Write (EscSeqUtils.CSI_HideCursor);
  117. }
  118. }
  119. }