diff --git a/CliFx/Infrastructure/ConsoleReader.cs b/CliFx/Infrastructure/ConsoleReader.cs index 88112cb..4f30dde 100644 --- a/CliFx/Infrastructure/ConsoleReader.cs +++ b/CliFx/Infrastructure/ConsoleReader.cs @@ -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) ); } \ No newline at end of file diff --git a/CliFx/Infrastructure/ConsoleWriter.cs b/CliFx/Infrastructure/ConsoleWriter.cs index 1873ea7..86f074f 100644 --- a/CliFx/Infrastructure/ConsoleWriter.cs +++ b/CliFx/Infrastructure/ConsoleWriter.cs @@ -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}; } \ No newline at end of file diff --git a/CliFx/Infrastructure/FakeConsole.cs b/CliFx/Infrastructure/FakeConsole.cs index 7e9a6aa..600e6c6 100644 --- a/CliFx/Infrastructure/FakeConsole.cs +++ b/CliFx/Infrastructure/FakeConsole.cs @@ -59,14 +59,33 @@ public class FakeConsole : IConsole, IDisposable /// 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); + } + + /// + 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." + ); + + /// + /// Enqueues a simulated key press, which can then be read by calling . + /// + public void EnqueueKey(ConsoleKeyInfo key) => _keys.Enqueue(key); + + /// + public void Clear() + { } /// public CancellationToken RegisterCancellationHandler() => _cancellationTokenSource.Token; - + /// /// Sends a cancellation signal to the currently executing command. /// @@ -87,25 +106,6 @@ public class FakeConsole : IConsole, IDisposable } } - /// - public void Clear() - { - } - - /// - 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." - ); - - /// - /// Enqueues a simulated key press, which can then be read by calling . - /// - public void EnqueueKey(ConsoleKeyInfo key) => _keys.Enqueue(key); - /// public virtual void Dispose() => _cancellationTokenSource.Dispose(); } \ No newline at end of file diff --git a/CliFx/Infrastructure/IConsole.cs b/CliFx/Infrastructure/IConsole.cs index 694af6e..c674fe6 100644 --- a/CliFx/Infrastructure/IConsole.cs +++ b/CliFx/Infrastructure/IConsole.cs @@ -64,6 +64,16 @@ public interface IConsole /// int CursorTop { get; set; } + /// + /// Obtains the next character or function key pressed by the user. + /// + ConsoleKeyInfo ReadKey(bool intercept = false); + + /// + /// Clears the console buffer and corresponding console window of display information. + /// + void Clear(); + /// /// 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 /// /// CancellationToken RegisterCancellationHandler(); - - /// - /// Clears the console buffer and corresponding console window of display information. - /// - void Clear(); - - /// - /// Obtains the next character or function key pressed by the user. - /// - ConsoleKeyInfo ReadKey(bool intercept = false); } /// diff --git a/CliFx/Infrastructure/SystemConsole.cs b/CliFx/Infrastructure/SystemConsole.cs index 385d20a..b9444e7 100644 --- a/CliFx/Infrastructure/SystemConsole.cs +++ b/CliFx/Infrastructure/SystemConsole.cs @@ -41,6 +41,20 @@ public class SystemConsole : IConsole, IDisposable get => Console.BackgroundColor; set => Console.BackgroundColor = value; } + + /// + public int CursorLeft + { + get => Console.CursorLeft; + set => Console.CursorLeft = value; + } + + /// + public int CursorTop + { + get => Console.CursorTop; + set => Console.CursorTop = value; + } /// /// Initializes an instance of . @@ -56,19 +70,11 @@ public class SystemConsole : IConsole, IDisposable public void ResetColor() => Console.ResetColor(); /// - public int CursorLeft - { - get => Console.CursorLeft; - set => Console.CursorLeft = value; - } - + public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept); + /// - public int CursorTop - { - get => Console.CursorTop; - set => Console.CursorTop = value; - } - + public void Clear() => Console.Clear(); + /// public CancellationToken RegisterCancellationHandler() { @@ -89,13 +95,7 @@ public class SystemConsole : IConsole, IDisposable return (_cancellationTokenSource = cts).Token; } - - /// - public void Clear() => Console.Clear(); - - /// - public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept); - + /// public void Dispose() {