Skip to content

fix: resolve default value and placeholder collision in InputForm (#192) - #433

Open
mkarimdev wants to merge 1 commit into
shibayan:masterfrom
mkarimdev:audit/issue-192-default-hinting
Open

mkarimdev wants to merge 1 commit into
shibayan:masterfrom
mkarimdev:audit/issue-192-default-hinting

Conversation

@mkarimdev

Copy link
Copy Markdown

fix: resolve default value and placeholder collision in InputForm (#192)

Fixes #192

Summary

InputForm<T> rendered DefaultValue and Placeholder independently, so configuring both produced two adjacent hints with no separator and the same Hint color:

? Pick a color: (blue) ▏type a color

The placeholder looked like editable text following the default, and it was unclear which value Enter would submit.

This change makes the two hints mutually exclusive: while a default value is active, the default hint wins and the placeholder is not drawn. The placeholder is revealed only after the default is dismissed.

Behavior

State Rendered Enter returns
DefaultValue = "blue", Placeholder = "type a color" ? Pick a color: (blue) ▏ "blue"
Same, after [Backspace] on the empty buffer ? Pick a color: ▏type a color null (string) / validation error (non-nullable)
Placeholder only ? Pick a color: ▏type a color As before
DefaultValue only ? Pick a color: (blue) ▏ "blue"

No public API changes. The placeholder remains non-submittable, and DefaultValue remains the value used by Enter/Tab.

Before / After

Before — both hints drawn, no separation:

? Pick a color: (blue) ▏type a color

After — default suppresses the placeholder:

? Pick a color: (blue) ▏

After — [Backspace] dismisses the default and reveals the placeholder:

? Pick a color: ▏type a color

Implementation

src/Sharprompt/Forms/InputForm.cs — the placeholder branch in InputTemplate() becomes an else if of the default-value branch:

if (!string.IsNullOrEmpty(defaultValueText))
{
    offscreenBuffer.WriteHint($"({defaultValueText}) ");
}
else if (InputBuffer.Length == 0 && !string.IsNullOrEmpty(_options.Placeholder))
{
    offscreenBuffer.PushCursor();
    offscreenBuffer.WriteHint(_options.Placeholder);
}

This relies on the existing HandleBackspace() behavior, which already clears _defaultValue when Backspace is pressed on an empty buffer.

Tests

Added three tests in tests/Sharprompt.Tests/Forms/FormInteractionTests.cs:

  • InputForm_DefaultValueAndPlaceholder_RendersDefaultOnly — asserts (blue) is rendered and the placeholder is absent.
  • InputForm_BackspaceDismissesDefaultValue_RevealsPlaceholder — asserts the post-backspace frame contains the placeholder and no longer the default.
  • InputForm_EmptyInputWithDefaultValueAndPlaceholder_ReturnsDefaultValue — asserts Enter on an empty buffer resolves to the default value.

Verification

dotnet build -c Release
dotnet test --project tests/Sharprompt.Tests/Sharprompt.Tests.csproj -c Release -f net10.0 --no-build
# 469 passed, 0 failed

dotnet test --project tests/Sharprompt.SourceGenerator.Tests/Sharprompt.SourceGenerator.Tests.csproj -c Release -f net10.0 --no-build
# 210 passed, 0 failed

dotnet format --verify-no-changes reports no formatting changes. Both net8.0 and net10.0 targets build successfully.

Compatibility

  • No public API or option-surface changes.
  • Existing default-value rendering tests are unaffected.
  • Prompts without a DefaultValue behave exactly as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consistent default value hinting

1 participant