Class ConsoleReadLine
- Namespace
- InteractiveReadLine
- Assembly
- InteractiveReadLine.dll
Exposes a IReadLineProvider that's wrapping a IConsole object, which by default is a wrapper around the System.Console. This class is the standard implementation of a ReadLine provider, intended for use with console applications.
public class ConsoleReadLine : IReadLineProvider, IDisposable
- Inheritance
-
ConsoleReadLine
- Implements
- Inherited Members
- Extension Methods
Constructors
ConsoleReadLine(IConsole?)
Creates a read-line provider over the specified console, or System.Console when no console is given.
public ConsoleReadLine(IConsole? console = null)
Parameters
consoleIConsoleThe console to use, or null to use System.Console.
Exceptions
- InvalidOperationException
The selected console reports redirected input.
Methods
Dispose()
Finishes the read line operation and moves the console cursor to the following row. Additional calls have no effect.
public void Dispose()
InsertText(FormattedText)
Writes a message out to the console out in the spot where the current read line input is, then immediately re-displays the line input on the next row.
public void InsertText(FormattedText text)
Parameters
textFormattedTextThe text to write to the console, a newline char will be added automatically
Read(ReadLineConfig?, CancellationToken)
Reads a line from System.Console and returns a result that describes the text and how the user ended the interaction.
public static ReadLineResult Read(ReadLineConfig? config = null, CancellationToken cancellationToken = default)
Parameters
configReadLineConfigThe configuration to use, or null to use ReadLineConfig.Basic.
cancellationTokenCancellationTokenA token that cancels the read.
Returns
- ReadLineResult
The completed interaction result.
Exceptions
- OperationCanceledException
The token was canceled before input completed.
- InvalidOperationException
Standard input is redirected.
ReadAsync(ReadLineConfig?, CancellationToken)
Reads a line from System.Console without blocking a thread while waiting for keys, returning a result that describes the text and how the user ended the interaction.
public static Task<ReadLineResult> ReadAsync(ReadLineConfig? config = null, CancellationToken cancellationToken = default)
Parameters
configReadLineConfigThe configuration to use, or null to use ReadLineConfig.Basic.
cancellationTokenCancellationTokenA token that cancels the read.
Returns
- Task<ReadLineResult>
A task producing the completed interaction result.
Exceptions
- OperationCanceledException
The token was canceled before input completed.
- InvalidOperationException
Standard input is redirected.
ReadKey(CancellationToken)
Reads a console key from the underlying provider, waiting until one is received.
public ConsoleKeyInfo ReadKey(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token that cancels the wait.
Returns
- ConsoleKeyInfo
The next keypress.
Exceptions
- OperationCanceledException
The token was canceled before a key arrived.
ReadKeyAsync(CancellationToken)
Reads a console key from the underlying provider, waiting asynchronously until one is received.
public Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token that cancels the wait.
Returns
- Task<ConsoleKeyInfo>
A task producing the next keypress.
Remarks
No asynchronous console input API is available. This method therefore polls until a key is available and then reads it without blocking. It polls even when the token cannot be canceled because an asynchronous read must not block. No thread is occupied between polls.
Exceptions
- OperationCanceledException
The token was canceled before a key arrived.
ReadLine(ReadLineConfig?, CancellationToken)
Provides a convenient static method of calling the ReadLine method on the System.Console
public static string? ReadLine(ReadLineConfig? config = null, CancellationToken cancellationToken = default)
Parameters
configReadLineConfigThe configuration to use, or null to use ReadLineConfig.Basic.
cancellationTokenCancellationTokenA token that cancels the read.
Returns
- string
The finished text; null if the user signaled the end of input with Ctrl+D; or an empty string if the user abandoned the line with Ctrl+C. Use Read to distinguish these outcomes.
Exceptions
- OperationCanceledException
The token was canceled before input completed.
- InvalidOperationException
Standard input is redirected.
ReadLineAsync(ReadLineConfig?, CancellationToken)
Reads a line from System.Console without blocking a thread while waiting for keys.
public static Task<string?> ReadLineAsync(ReadLineConfig? config = null, CancellationToken cancellationToken = default)
Parameters
configReadLineConfigThe configuration to use, or null to use ReadLineConfig.Basic.
cancellationTokenCancellationTokenA token that cancels the read.
Returns
- Task<string>
The finished text; null if the user signaled the end of input with Ctrl+D; or an empty string if the user abandoned the line with Ctrl+C. Use ReadAsync to distinguish these outcomes.
Exceptions
- OperationCanceledException
The token was canceled before input completed.
- InvalidOperationException
Standard input is redirected.
SetDisplay(LineDisplayState)
Sets the display state on the underlying console, consisting of a prefix, body, suffix, and cursor position.
public void SetDisplay(LineDisplayState state)
Parameters
stateLineDisplayState