Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions Sharprompt/Forms/InputForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ public InputForm(InputOptions<T> options)

_options = options;

_defaultValue = Optional<T>.Create(options.DefaultValue);
var defaultText = options.DefaultValue?.ToString();

if (defaultText is not null)
{
foreach (var c in defaultText)
{
InputBuffer.Insert(c);
}
}
}

private readonly InputOptions<T> _options;
private readonly Optional<T> _defaultValue;

protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
{
offscreenBuffer.WritePrompt(_options.Message);

if (_defaultValue.HasValue)
{
offscreenBuffer.WriteHint($"({_defaultValue.Value}) ");
}

if (InputBuffer.Length == 0 && !string.IsNullOrEmpty(_options.Placeholder))
{
offscreenBuffer.PushCursor();
Expand Down Expand Up @@ -56,7 +58,7 @@ protected override bool HandleEnter([NotNullWhen(true)] out T? result)
{
if (string.IsNullOrEmpty(input))
{
if (!TypeHelper<T>.IsNullable && !_defaultValue.HasValue)
if (!TypeHelper<T>.IsNullable)
{
SetError(Resource.Validation_Required);

Expand All @@ -65,7 +67,7 @@ protected override bool HandleEnter([NotNullWhen(true)] out T? result)
return false;
}

result = _defaultValue;
result = default;
}
else
{
Expand Down
Loading