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
{
internal static ConsoleReader Create(IConsole console, Stream? stream) => new(
internal static ConsoleReader Create(IConsole console, Stream stream) => new(
console,
stream is not null
? Stream.Synchronized(stream)
: Stream.Null
Stream.Synchronized(stream)
);
}

View File

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

View File

@@ -59,9 +59,28 @@ public class FakeConsole : IConsole, IDisposable
/// </summary>
public FakeConsole(Stream? input = null, Stream? output = null, Stream? error = null)
{
Input = ConsoleReader.Create(this, input);
Output = ConsoleWriter.Create(this, output);
Error = ConsoleWriter.Create(this, error);
Input = ConsoleReader.Create(this, input ?? Stream.Null);
Output = ConsoleWriter.Create(this, output ?? Stream.Null);
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 />
@@ -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 />
public virtual void Dispose() => _cancellationTokenSource.Dispose();
}

View File

@@ -64,6 +64,16 @@ public interface IConsole
/// </summary>
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>
/// Registers a handler for the interrupt signal (Ctrl+C) on the console and returns
/// a token representing the cancellation request.
@@ -82,16 +92,6 @@ public interface IConsole
/// </para>
/// </remarks>
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>

View File

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