This commit is contained in:
Oleksii Holub
2022-04-22 22:33:55 +03:00
parent 6d33c5cdad
commit ab87225f1f
5 changed files with 56 additions and 60 deletions

View File

@@ -99,10 +99,8 @@ public partial class ConsoleReader : StreamReader
public partial class ConsoleReader public partial class ConsoleReader
{ {
internal static ConsoleReader Create(IConsole console, Stream? stream) => new( internal static ConsoleReader Create(IConsole console, Stream stream) => new(
console, console,
stream is not null Stream.Synchronized(stream)
? Stream.Synchronized(stream)
: Stream.Null
); );
} }

View File

@@ -272,10 +272,8 @@ public partial class ConsoleWriter : StreamWriter
public partial class ConsoleWriter public partial class ConsoleWriter
{ {
internal static ConsoleWriter Create(IConsole console, Stream? stream) => new( internal static ConsoleWriter Create(IConsole console, Stream stream) => new(
console, console,
stream is not null Stream.Synchronized(stream)
? Stream.Synchronized(stream)
: Stream.Null
) {AutoFlush = true}; ) {AutoFlush = true};
} }

View File

@@ -59,9 +59,28 @@ public class FakeConsole : IConsole, IDisposable
/// </summary> /// </summary>
public FakeConsole(Stream? input = null, Stream? output = null, Stream? error = null) public FakeConsole(Stream? input = null, Stream? output = null, Stream? error = null)
{ {
Input = ConsoleReader.Create(this, input); Input = ConsoleReader.Create(this, input ?? Stream.Null);
Output = ConsoleWriter.Create(this, output); Output = ConsoleWriter.Create(this, output ?? Stream.Null);
Error = ConsoleWriter.Create(this, error); Error = ConsoleWriter.Create(this, error ?? Stream.Null);
}
/// <inheritdoc />
public ConsoleKeyInfo ReadKey(bool intercept = false) =>
_keys.TryDequeue(out var key)
? key
: throw new InvalidOperationException(
"Cannot read key because there are no key presses enqueued. " +
$"Use the `{nameof(EnqueueKey)}(...)` method to simulate a key press."
);
/// <summary>
/// Enqueues a simulated key press, which can then be read by calling <see cref="ReadKey"/>.
/// </summary>
public void EnqueueKey(ConsoleKeyInfo key) => _keys.Enqueue(key);
/// <inheritdoc />
public void Clear()
{
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -87,25 +106,6 @@ public class FakeConsole : IConsole, IDisposable
} }
} }
/// <inheritdoc />
public void Clear()
{
}
/// <inheritdoc />
public ConsoleKeyInfo ReadKey(bool intercept = false) =>
_keys.TryDequeue(out var key)
? key
: throw new InvalidOperationException(
"Cannot read key because there are no key presses enqueued. " +
$"Use the `{nameof(EnqueueKey)}(...)` method to simulate a key press."
);
/// <summary>
/// Enqueues a simulated key press, which can then be read by calling <see cref="ReadKey"/>.
/// </summary>
public void EnqueueKey(ConsoleKeyInfo key) => _keys.Enqueue(key);
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() => _cancellationTokenSource.Dispose(); public virtual void Dispose() => _cancellationTokenSource.Dispose();
} }

View File

@@ -64,6 +64,16 @@ public interface IConsole
/// </summary> /// </summary>
int CursorTop { get; set; } int CursorTop { get; set; }
/// <summary>
/// Obtains the next character or function key pressed by the user.
/// </summary>
ConsoleKeyInfo ReadKey(bool intercept = false);
/// <summary>
/// Clears the console buffer and corresponding console window of display information.
/// </summary>
void Clear();
/// <summary> /// <summary>
/// Registers a handler for the interrupt signal (Ctrl+C) on the console and returns /// Registers a handler for the interrupt signal (Ctrl+C) on the console and returns
/// a token representing the cancellation request. /// a token representing the cancellation request.
@@ -82,16 +92,6 @@ public interface IConsole
/// </para> /// </para>
/// </remarks> /// </remarks>
CancellationToken RegisterCancellationHandler(); CancellationToken RegisterCancellationHandler();
/// <summary>
/// Clears the console buffer and corresponding console window of display information.
/// </summary>
void Clear();
/// <summary>
/// Obtains the next character or function key pressed by the user.
/// </summary>
ConsoleKeyInfo ReadKey(bool intercept = false);
} }
/// <summary> /// <summary>

View File

@@ -42,6 +42,20 @@ public class SystemConsole : IConsole, IDisposable
set => Console.BackgroundColor = value; set => Console.BackgroundColor = value;
} }
/// <inheritdoc />
public int CursorLeft
{
get => Console.CursorLeft;
set => Console.CursorLeft = value;
}
/// <inheritdoc />
public int CursorTop
{
get => Console.CursorTop;
set => Console.CursorTop = value;
}
/// <summary> /// <summary>
/// Initializes an instance of <see cref="SystemConsole"/>. /// Initializes an instance of <see cref="SystemConsole"/>.
/// </summary> /// </summary>
@@ -56,18 +70,10 @@ public class SystemConsole : IConsole, IDisposable
public void ResetColor() => Console.ResetColor(); public void ResetColor() => Console.ResetColor();
/// <inheritdoc /> /// <inheritdoc />
public int CursorLeft public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept);
{
get => Console.CursorLeft;
set => Console.CursorLeft = value;
}
/// <inheritdoc /> /// <inheritdoc />
public int CursorTop public void Clear() => Console.Clear();
{
get => Console.CursorTop;
set => Console.CursorTop = value;
}
/// <inheritdoc /> /// <inheritdoc />
public CancellationToken RegisterCancellationHandler() public CancellationToken RegisterCancellationHandler()
@@ -90,12 +96,6 @@ public class SystemConsole : IConsole, IDisposable
return (_cancellationTokenSource = cts).Token; return (_cancellationTokenSource = cts).Token;
} }
/// <inheritdoc />
public void Clear() => Console.Clear();
/// <inheritdoc />
public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept);
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {