Revert GetWindowSize and add SetWindowSize

This commit is contained in:
BDisp
2025-07-22 18:29:53 +01:00
parent 600eba4667
commit 0cc4c60802
6 changed files with 43 additions and 19 deletions

View File

@@ -23,9 +23,15 @@ public interface IConsoleOutput : IDisposable
/// Returns the current size of the console window in rows/columns (i.e.
/// of characters not pixels).
/// </summary>
/// <param name="lastSize"></param>
/// <returns></returns>
public Size GetWindowSize (Size? lastSize = null);
public Size GetWindowSize ();
/// <summary>
/// Sets the current size of the console window in rows/columns
/// </summary>
/// <param name="newSize"></param>
/// /// <returns></returns>
public Size SetWindowSize (Size newSize);
/// <summary>
/// Updates the console cursor (the blinking underscore) to be hidden,

View File

@@ -202,7 +202,7 @@ public class NetOutput : IConsoleOutput
}
/// <inheritdoc/>
public Size GetWindowSize (Size? lastSize = null)
public Size GetWindowSize ()
{
if (ConsoleDriver.RunningUnitTests)
{
@@ -213,6 +213,12 @@ public class NetOutput : IConsoleOutput
return new (Console.WindowWidth, Console.WindowHeight);
}
/// <inheritdoc />
public Size SetWindowSize (Size newSize)
{
return newSize;
}
private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
{
SetCursorPositionImpl (lastCol, row);

View File

@@ -25,14 +25,21 @@ internal class WindowSizeMonitor : IWindowSizeMonitor
return false;
}
Size size = _consoleOut.GetWindowSize (_lastSize);
Size size = _consoleOut.GetWindowSize ();
if (size != _lastSize)
{
Logging.Logger.LogInformation ($"Console size changes from '{_lastSize}' to {size}");
_outputBuffer.SetWindowSize (size.Width, size.Height);
_lastSize = size;
SizeChanging?.Invoke (this, new (size));
Size newSize = size;
if (_consoleOut.GetType().Name == "WindowsOutput")
{
newSize = _consoleOut.SetWindowSize (size);
}
_outputBuffer.SetWindowSize (newSize.Width, newSize.Height);
_lastSize = newSize;
SizeChanging?.Invoke (this, new (newSize));
return true;
}

View File

@@ -466,7 +466,7 @@ internal partial class WindowsOutput : IConsoleOutput
return result;
}
public Size GetWindowSize (Size? lastSize = null)
public Size GetWindowSize ()
{
var csbi = new WindowsConsole.CONSOLE_SCREEN_BUFFER_INFOEX ();
csbi.cbSize = (uint)Marshal.SizeOf (csbi);
@@ -481,17 +481,19 @@ internal partial class WindowsOutput : IConsoleOutput
csbi.srWindow.Right - csbi.srWindow.Left + 1,
csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
if (lastSize is { } && sz != lastSize)
{
Size newSize = SetConsoleWindow ((short)sz.Width, (short)sz.Height);
return sz;
}
if (sz != newSize)
{
return newSize;
}
public Size SetWindowSize (Size newSize)
{
Size resSize = SetConsoleWindow ((short)newSize.Width, (short)newSize.Height);
if (resSize != newSize)
{
return resSize;
}
return sz;
return newSize;
}
private Size SetConsoleWindow (short cols, short rows)

View File

@@ -17,7 +17,10 @@ internal class FakeOutput : IConsoleOutput
public void Write (IOutputBuffer buffer) { LastBuffer = buffer; }
/// <inheritdoc/>
public Size GetWindowSize (Size? lastSize = null) { return Size; }
public Size GetWindowSize () { return Size; }
/// <inheritdoc />
public Size SetWindowSize (Size newSize) { return newSize; }
/// <inheritdoc/>
public void SetCursorVisibility (CursorVisibility visibility) { }

View File

@@ -19,7 +19,7 @@ public class WindowSizeMonitorTests
});
consoleOutput.Setup (m => m.GetWindowSize (null))
consoleOutput.Setup (m => m.GetWindowSize ())
.Returns (queue.Dequeue);
var outputBuffer = Mock.Of<IOutputBuffer> ();
@@ -52,7 +52,7 @@ public class WindowSizeMonitorTests
new Size (30, 20),
});
consoleOutput.Setup (m => m.GetWindowSize (null))
consoleOutput.Setup (m => m.GetWindowSize ())
.Returns (queue.Dequeue);
var outputBuffer = Mock.Of<IOutputBuffer> ();