Skip to content

Commit

Permalink
issue : #111
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cerqueira committed Oct 27, 2023
1 parent eeef11a commit b876eea
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 98 deletions.
10 changes: 5 additions & 5 deletions Src/Controls/AddtoList/ListControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override string InitControl(CancellationToken cancellationToken)
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.s_empty,
Optional<ItemListControl>.Empty(),
(item1,item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -376,7 +376,7 @@ public override ResultPrompt<IEnumerable<string>> TryResult(CancellationToken ca
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.s_empty,
Optional<ItemListControl>.Empty(),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand All @@ -393,7 +393,7 @@ public override ResultPrompt<IEnumerable<string>> TryResult(CancellationToken ca
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(item),
Optional<ItemListControl>.Set(item),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -441,7 +441,7 @@ public override ResultPrompt<IEnumerable<string>> TryResult(CancellationToken ca
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(new ItemListControl(_inputBuffer.ToString())),
Optional<ItemListControl>.Set(new ItemListControl(_inputBuffer.ToString())),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -477,7 +477,7 @@ public override ResultPrompt<IEnumerable<string>> TryResult(CancellationToken ca
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(new ItemListControl(_inputBuffer.ToString())),
Optional<ItemListControl>.Set(new ItemListControl(_inputBuffer.ToString())),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down
10 changes: 5 additions & 5 deletions Src/Controls/AddtoList/MaskEditListControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override string InitControl(CancellationToken cancellationToken)
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.s_empty,
Optional<ItemListControl>.Empty(),
(item1,item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -516,7 +516,7 @@ public override ResultPrompt<IEnumerable<ResultMasked>> TryResult(CancellationTo
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.s_empty,
Optional<ItemListControl>.Empty(),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand All @@ -533,7 +533,7 @@ public override ResultPrompt<IEnumerable<ResultMasked>> TryResult(CancellationTo
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(item),
Optional<ItemListControl>.Set(item),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -580,7 +580,7 @@ public override ResultPrompt<IEnumerable<ResultMasked>> TryResult(CancellationTo
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(new ItemListControl(_inputBuffer.ToMasked())),
Optional<ItemListControl>.Set(new ItemListControl(_inputBuffer.ToMasked())),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down Expand Up @@ -638,7 +638,7 @@ public override ResultPrompt<IEnumerable<ResultMasked>> TryResult(CancellationTo
FilterMode.StartsWith,
_options.Items,
_options.PageSize,
Optional<ItemListControl>.Create(new ItemListControl(_inputBuffer.ToMasked())),
Optional<ItemListControl>.Set(new ItemListControl(_inputBuffer.ToMasked())),
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text,
(item) => !item.Immutable);
Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/Calendar/CalendarControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public override ResultPrompt<DateTime> TryResult(CancellationToken cancellationT
_localpaginator = new Paginator<string>(
FilterMode.StartsWith,
GetNotes(_currentdate),
_options.PageSize, Optional<string>.s_empty,
_options.PageSize, Optional<string>.Empty(),
(item1, item2) => item1 == item2,
(item) => item);

Expand Down
6 changes: 3 additions & 3 deletions Src/Controls/Input/AutoCompleteControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override string InitControl(CancellationToken cancellationToken)
FilterMode.Contains,
_inputItems,
Math.Min(_options.PageSize, _options.CompletionMaxCount),
Optional<string>.s_empty,
Optional<string>.Empty(),
(item1, item2) => item1 == item2,
(item) => item);

Expand Down Expand Up @@ -456,7 +456,7 @@ public override ResultPrompt<string> TryResult(CancellationToken cancellationTok
FilterMode.Contains,
_inputItems,
Math.Min(_options.PageSize, _options.CompletionMaxCount),
Optional<string>.s_empty,
Optional<string>.Empty(),
(item1, item2) => item1 == item2,
(item) => item);
_localpaginator.UnSelected();
Expand Down Expand Up @@ -531,7 +531,7 @@ private Task ObserverAutoComplete(CancellationToken cancellationToken)
FilterMode.Contains,
_inputItems,
Math.Min(_options.PageSize, _options.CompletionMaxCount),
Optional<string>.s_empty,
Optional<string>.Empty(),
(item1, item2) => item1 == item2,
(item) => item);
_localpaginator.UnSelected();
Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/Input/InputControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public override ResultPrompt<string> TryResult(CancellationToken cancellationTok
_localpaginator = new Paginator<ItemHistory>(
FilterMode.StartsWith,
GetItemHistory(FilterMode.StartsWith),
_options.HistoryPageSize, Optional<ItemHistory>.s_empty,
_options.HistoryPageSize, Optional<ItemHistory>.Empty(),
(item1,item2) => item1.History == item2.History,
(item) => item.History);

Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/Input/MaskEditControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public override ResultPrompt<ResultMasked> TryResult(CancellationToken cancellat
_localpaginator = new Paginator<ItemHistory>(
FilterMode.StartsWith,
GetItemHistory(FilterMode.StartsWith),
_options.HistoryPageSize, Optional<ItemHistory>.s_empty,
_options.HistoryPageSize, Optional<ItemHistory>.Empty(),
(item1,item2) => item1.History == item2.History,
(item) => item.History);

Expand Down
22 changes: 12 additions & 10 deletions Src/Controls/Objects/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@
// MIT LICENCE
// The maintenance and evolution is maintained by the PromptPlus project under MIT license
// ***************************************************************************************
// Scenario usage:
// https://stackoverflow.com/questions/65787544/nullable-enum-type-cannot-be-assigned-to-null-when-used-as-a-generic
// ***************************************************************************************

namespace PPlus.Controls.Objects
{
internal readonly struct Optional<T>
{
private Optional(T value)
private Optional(T value, bool hasValue = true)
{
HasValue = true;
HasValue = hasValue;
Value = value;
}

public bool HasValue { get; }

public T Value { get; }

public static implicit operator T(Optional<T> optional) => optional.Value;

internal static readonly Optional<T> s_empty = new();

public static Optional<T> Create(T value)
public static Optional<T> Set(T value)
{
return value == null ? s_empty : new Optional<T>(value);
return new Optional<T>(value, true);
}

public static Optional<T> Create(object value)
public static Optional<T> Empty()
{
return value == null ? s_empty : new Optional<T>((T)value);
return new(default, false);
}

public static implicit operator T(Optional<T> optional) => optional.Value;

}
}
20 changes: 10 additions & 10 deletions Src/Controls/SelectColletion/MultiSelectControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class MultiSelectControl<T> : BaseControl<IEnumerable<T>>, IControlMult
private readonly List<ItemMultSelect<T>> _selectedItems = new();
private Paginator<ItemMultSelect<T>> _localpaginator;
private readonly EmacsBuffer _filterBuffer = new(CaseOptions.Uppercase, modefilter: true);
private Optional<IList<T>> _defaultHistoric = Optional<IList<T>>.Create(null);
private Optional<IList<T>> _defaultHistoric = Optional<IList<T>>.Empty();
private bool ShowingFilter => _filterBuffer.Length > 0;


Expand Down Expand Up @@ -90,7 +90,7 @@ public override string InitControl(CancellationToken cancellationToken)
{
if (!defvalue.HasValue)
{
defvalue = Optional<IList<T>>.Create(null);
defvalue = Optional<IList<T>>.Empty();
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public override string InitControl(CancellationToken cancellationToken)
_options.FilterType,
_options.Items,
_options.PageSize,
Optional<ItemMultSelect<T>>.s_empty,
Optional<ItemMultSelect<T>>.Empty(),
(item1,item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text??string.Empty,
(item) => !item.Disabled,
Expand Down Expand Up @@ -266,9 +266,9 @@ public IControlMultiSelect<T> AddDefault(IEnumerable<T> value)
{
if (_options.DefaultValues.Value == null)
{
_options.DefaultValues = Optional<IList<T>>.Create(new List<T>());
_options.DefaultValues = Optional<IList<T>>.Set(new List<T>());
}
_options.DefaultValues.Value.Add(Optional<T>.Create(item));
_options.DefaultValues.Value.Add(item);
}
return this;
}
Expand All @@ -279,9 +279,9 @@ public IControlMultiSelect<T> AddDefault(params T[] value)
{
if (_options.DefaultValues.Value == null)
{
_options.DefaultValues = Optional<IList<T>>.Create(new List<T>());
_options.DefaultValues = Optional<IList<T>>.Set(new List<T>());
}
_options.DefaultValues.Value.Add(Optional<T>.Create(item));
_options.DefaultValues.Value.Add(item);
}
return this;
}
Expand Down Expand Up @@ -715,7 +715,7 @@ public override ResultPrompt<IEnumerable<T>> TryResult(CancellationToken cancell
{
_localpaginator.TryGetSelected(out var currentItem);
_filterBuffer.Clear();
_localpaginator.UpdateFilter(_filterBuffer.ToString(), Optional<ItemMultSelect<T>>.Create(currentItem));
_localpaginator.UpdateFilter(_filterBuffer.ToString(), Optional<ItemMultSelect<T>>.Set(currentItem));
if (currentItem != null)
{
if (currentItem.IsGroupHeader)
Expand Down Expand Up @@ -896,15 +896,15 @@ private string EnumDisplay(T value)

private void LoadHistory()
{
_defaultHistoric = Optional<IList<T>>.Create(null);
_defaultHistoric = Optional<IList<T>>.Empty();
if (!string.IsNullOrEmpty(_options.OverwriteDefaultFrom))
{
var aux = FileHistory.LoadHistory(_options.OverwriteDefaultFrom, 1);
if (aux.Count == 1)
{
try
{
_defaultHistoric = Optional<IList<T>>.Create(JsonSerializer.Deserialize<IList<T>>(aux[0].History));
_defaultHistoric = Optional<IList<T>>.Set(JsonSerializer.Deserialize<IList<T>>(aux[0].History));
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/SelectColletion/MultiSelectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal MultiSelectOptions(StyleSchema styleSchema, ConfigControls config, ICon

public List<T> DisableItems { get; set; } = new List<T>();

public Optional<IList<T>> DefaultValues { get; set; } = Optional<IList<T>>.Create(null);
public Optional<IList<T>> DefaultValues { get; set; } = Optional<IList<T>>.Empty();

public Func<T, T, bool> EqualItems { get; set; }

Expand Down
18 changes: 9 additions & 9 deletions Src/Controls/SelectColletion/SelectControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class SelectControl<T> : BaseControl<T>, IControlSelect<T>
private readonly SelectOptions<T> _options;
private Paginator<ItemSelect<T>> _localpaginator;
private readonly EmacsBuffer _filterBuffer = new(CaseOptions.Uppercase,modefilter:true);
private Optional<T> _defaultHistoric = Optional<T>.Create(null);
private Optional<T> _defaultHistoric = Optional<T>.Empty();
private bool ShowingFilter => _filterBuffer.Length > 0;
private int _lengthSeparationline;

Expand Down Expand Up @@ -242,7 +242,7 @@ public IControlSelect<T> Config(Action<IPromptConfig> context)

public IControlSelect<T> Default(T value)
{
_options.DefaultValue = Optional<T>.Create(value);
_options.DefaultValue = Optional<T>.Set(value);
return this;
}

Expand Down Expand Up @@ -347,25 +347,25 @@ public override string InitControl(CancellationToken cancellationToken)
}
_lengthSeparationline = maxlensep;

Optional<T> defvalue = Optional<T>.s_empty;
Optional<T> defvalue = Optional<T>.Empty();

Optional<ItemSelect<T>> defvaluepage = Optional<ItemSelect<T>>.s_empty;
Optional<ItemSelect<T>> defvaluepage = Optional<ItemSelect<T>>.Empty();

if (_options.DefaultValue.HasValue)
{
defvalue = Optional<T>.Create(_options.DefaultValue.Value);
defvalue = Optional<T>.Set(_options.DefaultValue.Value);
}
if (_defaultHistoric.HasValue)
{
defvalue = Optional<T>.Create(_defaultHistoric.Value);
defvalue = Optional<T>.Set(_defaultHistoric.Value);
}

if (defvalue.HasValue)
{
var found = _options.Items.FirstOrDefault(x => !x.IsGroupHeader && _options.EqualItems(x.Value, defvalue.Value));
if (found != null && !found.Disabled)
{
defvaluepage = Optional<ItemSelect<T>>.Create(found);
defvaluepage = Optional<ItemSelect<T>>.Set(found);
}
}

Expand Down Expand Up @@ -666,15 +666,15 @@ private void SaveHistory(T value)

private void LoadHistory()
{
_defaultHistoric = Optional<T>.Create(null);
_defaultHistoric = Optional<T>.Empty();
if (!string.IsNullOrEmpty(_options.OverwriteDefaultFrom))
{
var aux = FileHistory.LoadHistory(_options.OverwriteDefaultFrom, 1);
if (aux.Count == 1)
{
try
{
_defaultHistoric = Optional<T>.Create(JsonSerializer.Deserialize<T>(aux[0].History));
_defaultHistoric = Optional<T>.Set(JsonSerializer.Deserialize<T>(aux[0].History));
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/SelectColletion/SelectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal SelectOptions(StyleSchema styleSchema, ConfigControls config, IConsoleC

public Func<T, T, bool> EqualItems { get; set; }

public Optional<T> DefaultValue { get; set; } = Optional<T>.Create(null);
public Optional<T> DefaultValue { get; set; } = Optional<T>.Empty();

public Func<T, string> TextSelector { get; set; }

Expand Down
Loading

0 comments on commit b876eea

Please sign in to comment.