How to reset the default value to an empty string
How can you reset the default value to an empty string in the following situation?
stringInputPrompt = "I'm the default value";
stringInputPrompt = Prompt.Input
The goal is to prepopulate the value, so the user doesn't have to re-enter it each time (similar to a setup wizard).
Anybody?
Do you mean something like that?
using Sharprompt;
var firstValue = Prompt.Input<string>("Please provide default value");
var defaultForSecondValue = string.IsNullOrWhiteSpace(firstValue) ? null : firstValue;
var secondValue = Prompt.Input<string>("Please provide second value", defaultForSecondValue);
Console.WriteLine();
Console.WriteLine($"Done :) - first value: {firstValue}, second value: {secondValue}");
EDIT: I understand right now. I think you cannot change default value as long as you don't provide something.
using Sharprompt;
string? valueToBeDefault = null;
for (var i = 0; i < 10; i++)
{
var tmpInput = Prompt.Input<string>("Provide value for next default", valueToBeDefault);
valueToBeDefault = string.IsNullOrWhiteSpace(tmpInput) ? null : tmpInput;
Console.WriteLine();
}
in the code above you can reset default value if you insert space.
Or you can create the behaviour to clear default value when it's the selected one - it will be default only for the next input.
using Sharprompt;
string? valueToBeDefault = null;
for (var i = 0; i < 10; i++)
{
var tmpInput = Prompt.Input<string>("Provide value for next default", valueToBeDefault);
valueToBeDefault = tmpInput == valueToBeDefault ? null : tmpInput;
Console.WriteLine();
}
Hello Radek, indeed. I want to clear the value during the prompt. I'm not really in favor of the workaround but yes that can work for now. Wouldn't it be better to implement a short key to clear the value?
The workaround also has the drawback of requiring additional code to clear the value. This issue is even more pronounced when using the Bind method. I believe implementing a shortcut key could be a great solution! What do you think?
I'm not the author or the owner of this library, but I've created PR which hopefully covers your issue: https://github.com/shibayan/Sharprompt/pull/300
The approach is different than adding shortcut to clear default value, because it looks like strange side effect/strange behaviour for me.
Hi Radek,
Based on your idea, I've implemented a DefaultValueTabBehaviour enum. This allows the tab key to be configured to either do nothing, populate the value, or clear the value. The repository is not public, but let me know if you want access or if we can merge all functionality.
Additionally, I've implemented a SkillLevelAttribute that can be set using Prompt.SkillLevel to show or hide properties based on the attribute.
I've also added a method to bind to a single property (Prompt.BindProperty) to avoid duplicating code for attributes set on the model.
For DefaultValueTabBehaviour, I've extended your logic as follows:
As I said before, I'm not the author/owner of this library, so @shibayan must decide. For me, reseting default value is strange behaviour. Also, I'm against using reflections as long as it's possible - they are hard to debug/maintain.