Interface IReadLineProvider
- Namespace
- InteractiveReadLine
- Assembly
- InteractiveReadLine.dll
The underlying contract for any object which is to provide ReadLine functionality to the input handler.
public interface IReadLineProvider : IDisposable
- Inherited Members
- Extension Methods
Remarks
Any object which is to provide a functioning backend on which the ReadLineHandler can act must implement these features: returning a single ConsoleKeyInfo when asked, both synchronously and asynchronously, writing out text and a cursor position on the currently active line (clearing whatever was there before), and inserting text to some output without interfering with the active display line.
Finally, the provider must implement IDisposable; as it will be disposed by the handler when the input of a line has been completed. The provider must then clean up the view in some fashion (on a standard System.Console provider this involves writing a newline to the console)
Methods
InsertText(FormattedText)
Inserts text 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. This is used to interrupt the user with a message or information while the ReadLine is still active
void InsertText(FormattedText text)
Parameters
textFormattedTextThe text to write to the console, a newline char will be added automatically
ReadKey(CancellationToken)
Waits synchronously for a ConsoleKeyInfo to be returned from some underlying input source.
ConsoleKeyInfo ReadKey(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token that cancels the wait. An implementation whose underlying wait cannot be interrupted should poll for input becoming available rather than blocking, so that the token can be observed.
Returns
- ConsoleKeyInfo
Information about the key that was read.
Exceptions
- OperationCanceledException
The token was canceled before a key arrived.
ReadKeyAsync(CancellationToken)
Waits asynchronously for a ConsoleKeyInfo to be returned from some underlying input source.
Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token that cancels the wait.
Returns
- Task<ConsoleKeyInfo>
A task producing information about the key that was read.
Remarks
A backend whose input arrives through an event or an already asynchronous API should implement this natively. A backend whose ReadKey does not block, such as one reading from an already populated queue, may return Task.FromResult(ReadKey(cancellationToken)). Avoid wrapping a blocking read in Task.Run because it occupies a thread until the user presses a key, and the token cannot interrupt it.
Exceptions
- OperationCanceledException
The token was canceled before a key arrived.
SetDisplay(LineDisplayState)
Displays the LineDisplayState to the visual output, including the cursor position
void SetDisplay(LineDisplayState state)
Parameters
stateLineDisplayStateThe line display state (prefix, suffix, text, and cursor position) to be displayed