NetOutput.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. EscSeqUtils.CSI_AppendForegroundColorRGB (
  45. output,
  46. attr.Foreground.R,
  47. attr.Foreground.G,
  48. attr.Foreground.B
  49. );
  50. EscSeqUtils.CSI_AppendBackgroundColorRGB (
  51. output,
  52. attr.Background.R,
  53. attr.Background.G,
  54. attr.Background.B
  55. );
  56. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  57. }
  58. /// <inheritdoc />
  59. protected override void Write (StringBuilder output)
  60. {
  61. Console.Out.Write (output);
  62. }
  63. /// <inheritdoc />
  64. protected override bool SetCursorPositionImpl (int col, int row)
  65. {
  66. if (_lastCursorPosition is { } && _lastCursorPosition.Value.X == col && _lastCursorPosition.Value.Y == row)
  67. {
  68. return true;
  69. }
  70. _lastCursorPosition = new (col, row);
  71. if (_isWinPlatform)
  72. {
  73. // Could happens that the windows is still resizing and the col is bigger than Console.WindowWidth.
  74. try
  75. {
  76. Console.SetCursorPosition (col, row);
  77. return true;
  78. }
  79. catch (Exception)
  80. {
  81. return false;
  82. }
  83. }
  84. // + 1 is needed because non-Windows is based on 1 instead of 0 and
  85. // Console.CursorTop/CursorLeft isn't reliable.
  86. EscSeqUtils.CSI_WriteCursorPosition (Console.Out, row + 1, col + 1);
  87. return true;
  88. }
  89. /// <inheritdoc/>
  90. public void Dispose ()
  91. {
  92. }
  93. /// <inheritdoc cref="IConsoleOutput.SetCursorVisibility"/>
  94. public override void SetCursorVisibility (CursorVisibility visibility)
  95. {
  96. Console.Out.Write (visibility == CursorVisibility.Default ? EscSeqUtils.CSI_ShowCursor : EscSeqUtils.CSI_HideCursor);
  97. }
  98. }