Skip to content

Commit

Permalink
Added a ReadLine overload that validates the entered string and retur…
Browse files Browse the repository at this point in the history
…ns it if it's valid.
  • Loading branch information
Zohar Peled committed Dec 25, 2023
1 parent cccb762 commit 6b65195
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
40 changes: 40 additions & 0 deletions ExtendedConsole/ExConsoleRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,46 @@ public static string ReadLine(this ExConsole self, string title)
return Console.ReadLine();
}

/// <summary>
/// Writes the title to the console, reads a line of text,
/// and returns the input only if it passed the validataion.
/// </summary>
/// <param name="self">The current instance of ExConsole.</param>
/// <param name="title">The title to show the user before asking for input.</param>
/// <param name="errorMessage">The error message to show the user if the validation failed.</param>
/// <param name="validator">The <see cref="Predicate{T}"/> to validate the input with.</param>
/// <returns>The string the user entered.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="self"/> or <paramref name="title"/> are null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="title"/> is empty.</exception>
/// <exception cref="System.Xml.XmlException">Thrown when <paramref name="title"/> isn't properly formatted xml.</exception>
/// <example>
/// Write "Please enter your first name:" to the console, where "first" is in green,
/// waits for the user to enter a lime, and returns the user's input.
/// <code>
/// var firstName = exConsole.ReadLine("Please enter your &lt;c f='green'&gt;first&lt;/c&gt; name:", "&lt;c f='green'&gt;first&lt;/c&gt; name must start with a capital letter", s => char.IsUpper(s[0]));
/// </code>
/// </example>
public static string ReadLine(this ExConsole self, string title, string errorMessage, Predicate<string> validator)
{
if (self is null) throw new ArgumentNullException(nameof(self));
if (title is null) throw new ArgumentNullException(nameof(title));
if (title == "") throw new ArgumentException(nameof(title) + " can't be empty.", nameof(title));
if (errorMessage is null) throw new ArgumentNullException(nameof(errorMessage));
if (errorMessage == "") throw new ArgumentException(nameof(errorMessage) + " can't be empty.", nameof(errorMessage));
if (validator is null) throw new ArgumentNullException(nameof(title));

self.WriteLine(title);
while (true)
{
var input = Console.ReadLine();
if (validator(input))
{
return input;
}
self.WriteLine(errorMessage);
}
}

/// <summary>
/// Reads an input line from the user and converts it to T.
/// Repeats until conversion succeeds (even if the user entered ^Z).
Expand Down
12 changes: 5 additions & 7 deletions ExtendedConsole/ExtendedConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
<Copyright />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>7.3</LangVersion>
<PackageReleaseNotes>Changes from previous version:
1. Fixed a bug in multiple select menu that caused flickering with large menus.
2. Added a property to MultipleSelectDisplayArgs to control the forecolor of the selected Item(s)
3. Removed obsolte methods from menus
4. Marked a couple of methos as obsolte
5. Added a ReadKey extension method</PackageReleaseNotes>
<Version>2.0.0</Version>
<PackageReleaseNotes>
Changes from previous version:
1. Added a ReadLine overload that validates the entered string and returns it if it's valud
</PackageReleaseNotes>
<Version>2.1.0</Version>
<PackageIconUrl>https://raw.githubusercontent.com/Peled-Zohar/ExtendedConsole/master/ExtendedConsole.png</PackageIconUrl>
<ApplicationIcon>extendedconsole.ico</ApplicationIcon>
</PropertyGroup>
Expand Down
21 changes: 21 additions & 0 deletions ExtendedConsole/ExtendedConsole.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions UsingExtendedConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ private static int StringsMenu()
"Read DateTime",
"Read int",
"Clear lines",
"Try it yourself"
"Try it yourself",
"Read validated string"
);

switch (result)
Expand All @@ -68,6 +69,9 @@ private static int StringsMenu()
case 6:
TryItYourself();
break;
case 7:
ReadValidatedString();
break;
}
return result;
}
Expand All @@ -80,7 +84,8 @@ private static int ActionsMenu()
("Read DateTime", ReadDateTimeMethods),
("Read int", ReadIntMethods),
("Clear lines", ClearLines),
("Try it yourself", TryItYourself)
("Try it yourself", TryItYourself),
("Read validated string", ReadValidatedString)
);

static void WriteLineMethods()
Expand Down Expand Up @@ -168,6 +173,13 @@ private static void MultipleSelectMenus()

var b = exConsole.MultipleSelectMenu(new MultipleSelectDisplayArgs("Multiple select with args and toString"), s => $"<c f='green'>{s.Id}</c> {s.Name}", arr);
}

private static void ReadValidatedString()
{
var line = exConsole.ReadLine("Enter a string that <c f='green'>starts with a digit</c>", "<c f='red'>invalid string entered</c>", s => char.IsDigit(s[0]));
exConsole.WriteLine($"You've entered: <c f='green'>{line}</c>");
exConsole.Pause();
}
}

public class MID
Expand Down

0 comments on commit 6b65195

Please sign in to comment.