mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-07 21:17:15 +08:00
Add culture option to TypeConverterHelper, TextPrompt and AnsiConsole (#1014)
* Add culture option to TypeConverterHelper, TextPrompt and AnsiConsole * Add IHasCulture interface
This commit is contained in:
@ -4,7 +4,7 @@ namespace Spectre.Console;
|
||||
/// Represents a prompt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
public sealed class TextPrompt<T> : IPrompt<T>
|
||||
public sealed class TextPrompt<T> : IPrompt<T>, IHasCulture
|
||||
{
|
||||
private readonly string _prompt;
|
||||
private readonly StringComparer? _comparer;
|
||||
@ -19,6 +19,11 @@ public sealed class TextPrompt<T> : IPrompt<T>
|
||||
/// </summary>
|
||||
public List<T> Choices { get; } = new List<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the culture to use when converting input to object.
|
||||
/// </summary>
|
||||
public CultureInfo? Culture { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message for invalid choices.
|
||||
/// </summary>
|
||||
@ -28,12 +33,12 @@ public sealed class TextPrompt<T> : IPrompt<T>
|
||||
/// Gets or sets a value indicating whether input should
|
||||
/// be hidden in the console.
|
||||
/// </summary>
|
||||
public bool IsSecret { get; set; }
|
||||
|
||||
public bool IsSecret { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the character to use while masking
|
||||
/// Gets or sets the character to use while masking
|
||||
/// a secret prompt.
|
||||
/// </summary>
|
||||
/// </summary>
|
||||
public char? Mask { get; set; } = '*';
|
||||
|
||||
/// <summary>
|
||||
@ -131,7 +136,7 @@ public sealed class TextPrompt<T> : IPrompt<T>
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
if (DefaultValue != null)
|
||||
{
|
||||
{
|
||||
var defaultValue = converter(DefaultValue.Value);
|
||||
console.Write(IsSecret ? defaultValue.Mask(Mask) : defaultValue, promptStyle);
|
||||
console.WriteLine();
|
||||
@ -160,7 +165,7 @@ public sealed class TextPrompt<T> : IPrompt<T>
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null)
|
||||
else if (!TypeConverterHelper.TryConvertFromStringWithCulture<T>(input, Culture, out result) || result == null)
|
||||
{
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
@ -208,8 +213,8 @@ public sealed class TextPrompt<T> : IPrompt<T>
|
||||
{
|
||||
appendSuffix = true;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var defaultValueStyle = DefaultValueStyle?.ToMarkup() ?? "green";
|
||||
var defaultValue = converter(DefaultValue.Value);
|
||||
var defaultValueStyle = DefaultValueStyle?.ToMarkup() ?? "green";
|
||||
var defaultValue = converter(DefaultValue.Value);
|
||||
|
||||
builder.AppendFormat(
|
||||
CultureInfo.InvariantCulture,
|
||||
|
Reference in New Issue
Block a user