Skip to content

2. Basic Inputs

David Shnayder edited this page Mar 2, 2024 · 1 revision

Basic Inputs

The main methods for input are ReadLine and ReadLine<T>, to reduce the amount of code need to be written, those method directly accept ColoredOutput.

Some of the overloads also allow customization of the input color, and the generic overloads are supported for all types that implement IParsable<T> which are most of the basic types in C#, as this is an interface, you can also implement it in your own types and that will make them supported as well. There is are also overloads for TEnum which enable parsing enums, and they also provide an option to ignoreCase.

Using ReadLine is the most comparable way to System.Console, however, all of the variants also have a corresponding TryReadLine which both will return whether the type was even able to be parsed, and by using an out parameter, enable better type inference from the compiler, which may result in smaller, more readable code.

Input Requests

RequestAnyInput(string message = "Press any key to continue");
RequestAnyInput(ColoredOutput output);

this is similar to how you would use ReadKey() from System.Console. it will show the message and wait for the user to break by pressing Enter.

Confirm(ColoredOutput message);
Confirm(ColoredOutput message, ReadOnlySpan<string> trueValues, bool emptyIsTrue = true)

This is used to confirm a boolean choice from the user, such as "Save output"... it will handle the input with the message.

  • The default overloads uses the values y, yes and "" (enter key without input) as true.
  • This input method ignores case for the trueValues.
Clone this wiki locally