diff --git a/ExtendedConsole/ExConsoleRead.cs b/ExtendedConsole/ExConsoleRead.cs
index cf55878..5d2cd2c 100644
--- a/ExtendedConsole/ExConsoleRead.cs
+++ b/ExtendedConsole/ExConsoleRead.cs
@@ -99,6 +99,46 @@ public static string ReadLine(this ExConsole self, string title)
return Console.ReadLine();
}
+ ///
+ /// Writes the title to the console, reads a line of text,
+ /// and returns the input only if it passed the validataion.
+ ///
+ /// The current instance of ExConsole.
+ /// The title to show the user before asking for input.
+ /// The error message to show the user if the validation failed.
+ /// The to validate the input with.
+ /// The string the user entered.
+ /// Thrown when or are null.
+ /// Thrown when is empty.
+ /// Thrown when isn't properly formatted xml.
+ ///
+ /// 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.
+ ///
+ /// var firstName = exConsole.ReadLine("Please enter your <c f='green'>first</c> name:", "<c f='green'>first</c> name must start with a capital letter", s => char.IsUpper(s[0]));
+ ///
+ ///
+ public static string ReadLine(this ExConsole self, string title, string errorMessage, Predicate 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);
+ }
+ }
+
///
/// Reads an input line from the user and converts it to T.
/// Repeats until conversion succeeds (even if the user entered ^Z).
diff --git a/ExtendedConsole/ExtendedConsole.csproj b/ExtendedConsole/ExtendedConsole.csproj
index e9adcd6..b90f58a 100644
--- a/ExtendedConsole/ExtendedConsole.csproj
+++ b/ExtendedConsole/ExtendedConsole.csproj
@@ -12,13 +12,11 @@
true
7.3
- 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
- 2.0.0
+
+ Changes from previous version:
+ 1. Added a ReadLine overload that validates the entered string and returns it if it's valud
+
+ 2.1.0
https://raw.githubusercontent.com/Peled-Zohar/ExtendedConsole/master/ExtendedConsole.png
extendedconsole.ico
diff --git a/ExtendedConsole/ExtendedConsole.xml b/ExtendedConsole/ExtendedConsole.xml
index 7b0c0cf..4b482c5 100644
--- a/ExtendedConsole/ExtendedConsole.xml
+++ b/ExtendedConsole/ExtendedConsole.xml
@@ -422,6 +422,27 @@
+
+
+ Writes the title to the console, reads a line of text,
+ and returns the input only if it passed the validataion.
+
+ The current instance of ExConsole.
+ The title to show the user before asking for input.
+ The error message to show the user if the validation failed.
+ The to validate the input with.
+ The string the user entered.
+ Thrown when or are null.
+ Thrown when is empty.
+ Thrown when isn't properly formatted xml.
+
+ 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.
+
+ var firstName = exConsole.ReadLine("Please enter your <c f='green'>first</c> name:", "<c f='green'>first</c> name must start with a capital letter", s => char.IsUpper(s[0]));
+
+
+
Reads an input line from the user and converts it to T.
diff --git a/UsingExtendedConsole/Program.cs b/UsingExtendedConsole/Program.cs
index 79b92bb..269913e 100644
--- a/UsingExtendedConsole/Program.cs
+++ b/UsingExtendedConsole/Program.cs
@@ -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)
@@ -68,6 +69,9 @@ private static int StringsMenu()
case 6:
TryItYourself();
break;
+ case 7:
+ ReadValidatedString();
+ break;
}
return result;
}
@@ -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()
@@ -168,6 +173,13 @@ private static void MultipleSelectMenus()
var b = exConsole.MultipleSelectMenu(new MultipleSelectDisplayArgs("Multiple select with args and toString"), s => $"{s.Id} {s.Name}", arr);
}
+
+ private static void ReadValidatedString()
+ {
+ var line = exConsole.ReadLine("Enter a string that starts with a digit", "invalid string entered", s => char.IsDigit(s[0]));
+ exConsole.WriteLine($"You've entered: {line}");
+ exConsole.Pause();
+ }
}
public class MID