Collection of utilities for writing console-based applications in .NET
using static ConsoleHelpers.Helpers;
class FooBar
{
public string foo { get; set; }
public string bar { get; set; }
}
class Program
{
static void Main(string[] args)
{
Header("This is a header");
string input = PromptRead("This is a prompt");
string input2 = PromptRead("You can also specify default values", "foobar");
IList<string> choices = new List<string>
{
"foo",
"bar",
"baz"
};
string selected = PromptSelect("IEnumerables of strings are selectable by their index", choices);
IList<FooBar> fooBarChoices = new List<FooBar>
{
new FooBar { foo = "foo"; bar = "bar" },
new FooBar { foo = "bar"; bar = "foo" }
};
FooBar selectedFooBar = PromptSelect<FooBar>(
"IEnumerables of objects are selectable too!",
fooBarChoices,
fooBar => $"foo = {foo}, bar = {bar}"
);
}
}Classes
- SelectedIndex - represents either:
- the index of a selected item within a list or
- a cancelled selection
public class SelectedIndex
{
public int Value { get; }
public bool Cancelled { get; } = false;
}- SelectedValue<T> - represents either:
- the value of a selected item (T) within a list (IEnumberable<T>) or
- a cancelled selection
public class SelectedValue<T> where T : class
{
public T Value { get; } = default(T);
public bool Cancelled { get; } = false;
}Constants
- string EOL
- Equivelent to
Environment.NewLine(just simpler to type!)
- Equivelent to
Variables
- ConsoleColor HEADER_COLOR
- The color of text written by the
Headermethod - Default: ConsoleColor.White
- The color of text written by the
- ConsoleColor HEADER_BG
- The color of the background of text written by the
Headermethod - Default: ConsoleColor.DarkBlue
- The color of the background of text written by the
- ConsoleColor PROMPT_COLOR
- Defualt: ConsoleColor.Gray
- ConsoleColor PROMPT_BG
- Default: ConsoleColor.Black
Methods
-
void WriteWithColor(ConsoleColor textColor, ConsoleColor bgColor, string message, bool newLine)- Prints the provided
messageto the console using the provided colors - textColor - the color of the text to print
- bgColor - the color of the background to print
- message - the message to print
- newLine - whether or not to print a newline after the message
- Prints the provided
-
void Blue(string message [, bool newLine = true ])- Prints message with text color of blue
- By default, prints a newline character after the message unless
falseis provided for thenewLineargument
-
void Red(string message [, bool newLine = true ])- Prints message in red
- By default, prints a newline character after the message unless
falseis provided for thenewLineargument
-
void Yellow(string message [, bool newLine = true ])- Prints message in yellow
- By default, prints a newline character after the message unless
falseis provided for thenewLineargument
-
void White(string message [, bool newLine = true ])- Prints message in white
- By default, prints a newline character after the message unless
falseis provided for thenewLineargument
-
void Gray(string message [, bool newLine = true ])- Prints message in gray
- By default, prints a newline character after the message unless
falseis provided for thenewLineargument
-
void Header(string message)- Prints a new line followed by
"## {message} ##" - Text color / background color can be changed by setting
HEADER_COLORandHEADER_BGrespectively
- Prints a new line followed by
-
void Prompt(string message)- Prints a tab followed by
"{message}: " - Text color / background color can be changed by setting
PROMPT_COLORandPROMPT_BGrespectively
- Prints a tab followed by
-
string PromptRead(string prompt)- Prompts the user to enter a value
- Returns
nullif the user presses enter without typing anything - Returns the entered string if the user types something and presses enter
-
string PromptRead(string prompt, object defaultValue)- Prompts the user to enter a value
- Returns
defaultValueif the user presses enter without typing anything- If defaultValue is
null, this method will returnnullwhen nothing is entered. - For any non-
nullvalue ofdefaultValue, this method will returndefaultValue.ToString()when nothing is entered
- If defaultValue is
- Returns the entered string if the user types something and presses enter
-
SelectedIndex PromptSelect(string prompt, IEnumberable<string> choices [, bool allowCancel = true, bool allowNull = false])- Prompts the user to choose one of the provided
choices - Each element of
choiceswill be listed alongside its index withinchoices, and the user can enter the number corresponding to their choice - The return value
SelectedIndexcontains the users's selection - allowCancel - whether or not the user will be allowed to enter
-1to cancel this selection- if user enters
-1, theSelectedIndexthat is returned will haveCancelled = true
- if user enters
- allowNull - whether or not to allow the user to simply press enter, choosing nothing
- if user chooses nothing,
nullwill be returned
- if user chooses nothing,
- Prompts the user to choose one of the provided
-
SelectedIndex PromptSelect(string prompt, IEnumberable<string> choices, object defaultValue [, bool allowCancel = true, bool allowNull = false])- Prompts the user to choose one of the provided
choices - Each element of
choiceswill be listed alongside its index withinchoices, and the user can enter the number corresponding to their choice - Returns the index of
defaultValuewithinchoiceswrapped in aSelectedIndexif the user presses enter without typing anything- If defaultValue is
null, this method will returnnullwhen nothing is entered. - For any non-
nullvalue ofdefaultValue, this method will return the index ofdefaultValue.ToString()wrapped in aSelectedIndexwhen nothing is entered
- If defaultValue is
- The return value
SelectedIndexcontains the users's selection - allowCancel - whether or not the user will be allowed to enter
-1to cancel this selection- if user enters
-1, theSelectedIndexthat is returned will haveCancelled = true
- if user enters
- allowNull - whether or not to allow the user to simply press enter, choosing nothing
- if user chooses nothing,
nullwill be returned
- if user chooses nothing,
- Prompts the user to choose one of the provided
-
SelectedValue<T> PromptSelect<T>(string prompt, IEnumberable<T> choices, Func<T, string> mapping [, bool allowCancel = true, bool allowNull = false])- Prompts the user to choose one of the provided
choices - Each element of
choiceswill be mapped to a string usingmappingand listed alongside its index withinchoices, and the user can enter the number corresponding to their choice - The return value
SelectedValue<T>contains the users's selection - allowCancel - whether or not the user will be allowed to enter
-1to cancel this selection- if user enters
-1, theSelectedValue<T>that is returned will haveCancelled = true
- if user enters
- allowNull - whether or not to allow the user to simply press enter, choosing nothing
- if user chooses nothing,
nullwill be returned
- if user chooses nothing,
- Prompts the user to choose one of the provided
-
SelectedValue<T> PromptSelect<T>(string prompt, IEnumberable<T> choices, Func<T, string> mapping, T defaultValue [, bool allowCancel = true, bool allowNull = false])- Prompts the user to choose one of the provided
choices - Each element of
choiceswill be mapped to a string usingmappingand listed alongside its index withinchoices, and the user can enter the number corresponding to their choice - Returns
defaultValuewrapped in aSelectedValue<T>if the user presses enter without typing anything- If defaultValue is
null, this method will returnnullwhen nothing is entered.
- If defaultValue is
- The return value
SelectedValue<T>contains the users's selection - allowCancel - whether or not the user will be allowed to enter
-1to cancel this selection- if user enters
-1, theSelectedValue<T>that is returned will haveCancelled = true
- if user enters
- allowNull - whether or not to allow the user to simply press enter, choosing nothing
- if user chooses nothing,
nullwill be returned
- if user chooses nothing,
- Prompts the user to choose one of the provided
-
SelectedValue<T> PromptSelect<T>(string prompt, IEnumberable<T> choices, Func<T, string> mapping, int defaultIndex [, bool allowCancel = true, bool allowNull = false])- Prompts the user to choose one of the provided
choices - Each element of
choiceswill be mapped to a string usingmappingand listed alongside its index withinchoices, and the user can enter the number corresponding to their choice - Returns
choices[defaultIndex]wrapped in aSelectedValue<T>if the user presses enter without typing anything- If
choices[defaultIndex]isnull, this method will returnnullwhen nothing is entered.
- If
- The return value
SelectedValue<T>contains the users's selection - allowCancel - whether or not the user will be allowed to enter
-1to cancel this selection- if user enters
-1, theSelectedValue<T>that is returned will haveCancelled = true
- if user enters
- allowNull - whether or not to allow the user to simply press enter, choosing nothing
- if user chooses nothing,
nullwill be returned
- if user chooses nothing,
- Prompts the user to choose one of the provided