Skip to content

Commit

Permalink
fixed the correct count of items on the page ref #68 , fixed methods …
Browse files Browse the repository at this point in the history
…to "IgnoreColorTokens" and "AcceptColorTokens" ref #65
  • Loading branch information
Fernando Cerqueira committed Sep 19, 2023
1 parent c3fbb03 commit ddcd589
Show file tree
Hide file tree
Showing 26 changed files with 203 additions and 151 deletions.
42 changes: 24 additions & 18 deletions Samples/ConsoleFeaturesSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,39 @@ static void Main()
PromptPlus.Reset();
PromptPlus.Clear();

//write to standard error output stream for any output included within 'using'
using (PromptPlus.OutputError())
//standard
PromptPlus.DoubleDash($"PromptPlus IgnoreColorTokens = false - Default value and usage" );
PromptPlus.WriteLine("Valid[[]]formedColor_TokenAny[[RED ON WHITE]]Text[[/]]_[[YELLOW]]Othertext[[/]]");
PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");

PromptPlus.DoubleDash($"PromptPlus IgnoreColorTokens = true");
//global change IgnoreColorTokens
PromptPlus.IgnoreColorTokens = true;
//show text with no color!
PromptPlus.WriteLine("Valid[]formedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
//create context to IgnoreColorTokens = false
using (PromptPlus.AcceptColorTokens())
{
PromptPlus.WriteLine("Test");
PromptPlus.DoubleDash($"PromptPlus with context IgnoreColorTokens = false");
//show text with color!
PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
}

//standard
PromptPlus.WriteLine("MalformedColor_TokenAnyText[[c4201]]_Othertext");

//global change IgnoreErrorColorTokens
PromptPlus.IgnoreErrorColorTokens = true;
//write text with char token
PromptPlus.WriteLine("MalformedColorToken_AnyText[c4201]_Othertext[/]");
//show text with color
PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
PromptPlus.IgnoreErrorColorTokens = false;

//change local IgnoreErrorColorTokens
PromptPlus.IgnoreColorTokens = false;
//create context to IgnoreColorTokens = true
using (PromptPlus.EscapeColorTokens())
{
//write text with char token
PromptPlus.WriteLine("MalformedColor_TokenAnyText[c4201]_Othertext");
PromptPlus.DoubleDash($"PromptPlus with context IgnoreColorTokens = true");
//show text with color
PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
}
//restore original value IgnoreErrorColorTokens
//restore original value IgnoreColorTokens

//write to standard error output stream for any output included within 'using'
using (PromptPlus.OutputError())
{
PromptPlus.WriteLine("Test Output Error");
}

PromptPlus.WriteLine("[RGB(255,0,0) ON WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[#ff0000 ON WHITE]Test [YELLOW] COLOR [/] BACK COLOR [/] other text");
Expand Down
6 changes: 3 additions & 3 deletions Samples/MultiSelectUserScopeSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal enum MyEnum
internal class MyClass
{
public int Id { get; set; }
public string MyText { get; set; }
public string MyDesc { get; set; }
public required string MyText { get; set; }
public required string MyDesc { get; set; }
public bool IsDisabled { get; set; }
public bool IsSelected { get; set; }
public bool IsHide { get; set; }
Expand All @@ -40,7 +40,7 @@ internal class MyClass
internal class Program
{
private static IEnumerable<MyClass>? _datasample;
static void Main(string[] args)
static void Main()
{
_datasample = LoadData();
PromptPlus.WriteLine("Hello, World!");
Expand Down
6 changes: 3 additions & 3 deletions Samples/MultiSelectUserTypeSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace MultiSelectUserTypeSamples
internal class MyClass
{
public int Id { get; set; }
public string MyText { get; set; }
public string MyDesc { get; set; }
public required string MyText { get; set; }
public required string MyDesc { get; set; }
}

internal class Program
{
private static IEnumerable<MyClass>? _datasample;
static void Main(string[] args)
static void Main()
{
_datasample = LoadData();
PromptPlus.WriteLine("Hello, World!");
Expand Down
9 changes: 4 additions & 5 deletions Samples/SelectBasicSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ private static (string City,string other)[] MyCities()
}


static void Main(string[] args)
static void Main()
{
PromptPlus.WriteLine("Hello, World!");

.Separator() //Default Separator : SeparationLineType.SingleLine
//Ensure ValueResult Culture for all controls
PromptPlus.Config.DefaultCulture = new CultureInfo("en-us");

Expand Down Expand Up @@ -122,13 +121,13 @@ static void Main(string[] args)
PromptPlus.Select<string>("Select")
.AddItem("Seattle")
.AddItem("New York")
.AddSeparationline() //Default Separationline : SeparationLineType.SingleLine
.Separator() //Default SeparatorLine : SeparatorLine.SingleLine
.AddItem("Tokyo")
.AddItem("Singapore")
.AddItem("Shanghai")
.AddSeparationline(SeparationLineType.DoubleLine)
.Separator(SeparatorLine.DoubleLine)
.AddItem("London")
.AddSeparationline(SeparationLineType.Char, '*')
.Separator(SeparatorLine.Char, '*')
.AddItem("Other city")
.Run();

Expand Down
6 changes: 3 additions & 3 deletions Samples/SelectUserScopeSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ internal enum MyEnum
internal class MyClass
{
public int Id { get; set; }
public string MyText { get; set; }
public string MyDesc { get; set; }
public required string MyText { get; set; }
public required string MyDesc { get; set; }
public bool IsDisabled { get; set; }
public bool IsHide { get; set; }
}

internal class Program
{
private static IEnumerable<MyClass>? _datasample;
static void Main(string[] args)
static void Main()
{
_datasample = LoadData();
PromptPlus.WriteLine("Hello, World!");
Expand Down
6 changes: 3 additions & 3 deletions Samples/SelectUserTypeSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace SelectUserTypeSamples
internal class MyClass
{
public int Id { get; set; }
public string MyText { get; set; }
public string MyDesc { get; set; }
public required string MyText { get; set; }
public required string MyDesc { get; set; }
}

internal class Program
{
private static IEnumerable<MyClass>? _datasample;
static void Main(string[] args)
static void Main()
{
_datasample = LoadData();
Console.WriteLine("Hello, World!");
Expand Down
2 changes: 1 addition & 1 deletion Src/Controls/Input/MaskEditOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal MaskEditOptions(StyleSchema styleSchema, ConfigControls config, IConsol
public char? FillNumber { get; set; } = null;
public CaseOptions InputToCase { get; set; } = CaseOptions.Any;
public string DefaultEmptyValue { get; set; }
public string DefaultValue { get; set; }
public string? DefaultValue { get; set; }
public IList<Func<object, ValidationResult>> Validators { get; } = new List<Func<object, ValidationResult>>();
public bool ValidateOnDemand { get; set; }
public Func<string, string> ChangeDescription { get; set; }
Expand Down
26 changes: 24 additions & 2 deletions Src/Controls/Objects/Paginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ internal class Paginator<T>
private readonly Func<T, bool> _validatorAction;
private readonly FilterMode _filterMode;
private readonly Func<T, T, bool>? _founddefault;
private readonly Func<T, bool>? _countvalidator;

public Paginator(FilterMode filterMode, IEnumerable<T> items, int pageSize, Optional<T> defaultValue, Func<T, T, bool>? founddefault, Func<T, string> textSelector = null, Func<T, bool> validatorAction = null)
public Paginator(FilterMode filterMode, IEnumerable<T> items, int pageSize, Optional<T> defaultValue, Func<T, T, bool>? founddefault, Func<T, string>? textSelector = null, Func<T, bool> validatorAction = null, Func<T, bool> countvalidator = null)
{
_countvalidator = countvalidator;
_filterMode = filterMode;
_items = items.ToArray();
_userpageSize = pageSize;
Expand Down Expand Up @@ -97,6 +99,26 @@ public T SelectedItem
}
}

public int TotalCountValid
{
get
{
if (_countvalidator == null)
{
return _filteredItems.Length;
}
var qtd = _filteredItems.Length;
foreach (var item in _filteredItems)
{
if (!_countvalidator.Invoke(item))
{
qtd--;
}
}
return qtd;
}
}

public int TotalCount => _filteredItems.Length;

public int Count => Math.Min(_filteredItems.Length - (_userpageSize * SelectedPage), _userpageSize);
Expand Down Expand Up @@ -231,7 +253,7 @@ public bool FirstItem()

public string PaginationMessage()
{
return string.Format(Messages.PaginationTemplate, TotalCount, SelectedPage + 1, PageCount);
return string.Format(Messages.PaginationTemplate, TotalCountValid, SelectedPage + 1, PageCount);
}

public bool LastItem()
Expand Down
6 changes: 3 additions & 3 deletions Src/Controls/SelectColletion/IControlSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public interface IControlSelect<T> : IPromptControls<T>
/// <summary>
/// Add Separation line
/// </summary>
/// <param name="typeSeparation">Type Separation line.Default value is SeparationLineType.SingleLine <see cref="SeparationLineType"/></param>
/// <param name="value">Char Separation line. Valid only SeparationLineType is SeparationLineType.Char</param>
/// <param name="separatorLine">Type Separation line.Default value is SeparatorLine.SingleLine <see cref="SeparatorLine"/></param>
/// <param name="value">Char Separation line. Valid only SeparatorLine is SeparatorLine.Char</param>
/// <returns><see cref="IControlSelect{T}"/></returns>
IControlSelect<T> AddSeparationline(SeparationLineType typeSeparation = SeparationLineType.SingleLine, char? value = null);
IControlSelect<T> Separator(SeparatorLine separatorLine = SeparatorLine.SingleLine, char? value = null);

/// <summary>
/// Add items colletion to list
Expand Down
4 changes: 2 additions & 2 deletions Src/Controls/SelectColletion/ItemSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ItemSelect()
public string Group { get; set; }
public bool IsGroupHeader { get; set; }
public bool IsLastItemGroup { get; set; }
public bool IsSeparationline { get; set; }
public SeparationLineType? SeparationType { get; set; }
public bool IsSeparator { get; set; }
public SeparatorLine? SeparatorType { get; set; }
public char? CharSeparation { get; set; }

}
Expand Down
5 changes: 3 additions & 2 deletions Src/Controls/SelectColletion/MultiSelectControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ public override string InitControl(CancellationToken cancellationToken)
Optional<ItemMultSelect<T>>.s_empty,
(item1,item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text??string.Empty,
(item) => !item.Disabled);
(item) => !item.Disabled,
(item) => !item.IsGroupHeader);


if (_localpaginator.TotalCount > 0 && _localpaginator.SelectedItem != null && _localpaginator.SelectedItem.Disabled)
if (_localpaginator.TotalCountValid > 0 && _localpaginator.SelectedItem != null && _localpaginator.SelectedItem.Disabled)
{
_localpaginator.UnSelected();
if (!defvalue.HasValue)
Expand Down
4 changes: 2 additions & 2 deletions Src/Controls/SelectColletion/ScreenBufferSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static void WriteFilterSelect<T>(this ScreenBuffer screenBuffer, SelectOp
{
if (input.StartsWith(filter.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
screenBuffer.WriteAnswer(options, input.Substring(0, filter.Length));
screenBuffer.WriteAnswer(options, input[..filter.Length]);
screenBuffer.SaveCursor();
screenBuffer.WriteSuggestion(options, input.Substring(filter.Length));
screenBuffer.WriteSuggestion(options, input[filter.Length..]);
}
else
{
Expand Down
41 changes: 21 additions & 20 deletions Src/Controls/SelectColletion/SelectControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ public SelectControl(IConsoleControl console, SelectOptions<T> options) : base(c

#region IControlSelect

public IControlSelect<T> AddSeparationline(SeparationLineType typeSeparation = SeparationLineType.SingleLine, char? value = null)
public IControlSelect<T> Separator(SeparatorLine separatorLine = SeparatorLine.SingleLine, char? value = null)
{
switch (typeSeparation)
switch (separatorLine)
{
case SeparationLineType.DoubleLine:
case SeparationLineType.SingleLine:
case SeparatorLine.DoubleLine:
case SeparatorLine.SingleLine:
_options.Items.Add(new ItemSelect<T>
{
Disabled = true,
IsGroupHeader = true,
IsSeparationline = true,
SeparationType = typeSeparation,
IsSeparator = true,
SeparatorType = separatorLine,
});
break;
case SeparationLineType.Char:
case SeparatorLine.Char:
if (!value.HasValue)
{
throw new PromptPlusException($"char value is empty");
Expand All @@ -54,13 +54,13 @@ public IControlSelect<T> AddSeparationline(SeparationLineType typeSeparation = S
{
Disabled = true,
IsGroupHeader = true,
IsSeparationline = true,
SeparationType = typeSeparation,
IsSeparator = true,
SeparatorType = separatorLine,
CharSeparation = value.Value
});
break;
default:
throw new PromptPlusException($"Not implemented TypeSeparation: {typeSeparation}");
throw new PromptPlusException($"Not implemented SeparatorLine: {separatorLine}");
}
return this;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public override string InitControl(CancellationToken cancellationToken)
var maxlensep = 0;
foreach (var item in _options.Items)
{
if (item.IsGroupHeader && !item.IsSeparationline)
if (item.IsGroupHeader && !item.IsSeparator)
{
if (maxlensep < item.Group.Length)
{
Expand Down Expand Up @@ -331,11 +331,11 @@ public override string InitControl(CancellationToken cancellationToken)
{
if (_options.IsOrderDescending)
{
_options.Items = _options.Items.Where(x => !x.IsSeparationline).OrderByDescending(x => x.Group ?? string.Empty + _options.OrderBy.Invoke(x.Value)).ToList();
_options.Items = _options.Items.Where(x => !x.IsSeparator).OrderByDescending(x => x.Group ?? string.Empty + _options.OrderBy.Invoke(x.Value)).ToList();
}
else
{
_options.Items = _options.Items.Where(x => !x.IsSeparationline).OrderBy(x => x.Group ?? string.Empty + _options.OrderBy.Invoke(x.Value)).ToList();
_options.Items = _options.Items.Where(x => !x.IsSeparator).OrderBy(x => x.Group ?? string.Empty + _options.OrderBy.Invoke(x.Value)).ToList();
}
}

Expand All @@ -346,9 +346,10 @@ public override string InitControl(CancellationToken cancellationToken)
defvaluepage,
(item1, item2) => item1.UniqueId == item2.UniqueId,
(item) => item.Text??string.Empty,
(item) => !item.IsGroupHeader && IsEnnabled(item));
(item) => !item.IsGroupHeader && IsEnnabled(item),
(item) => !item.IsGroupHeader);

if (_localpaginator.TotalCount > 0 && _localpaginator.SelectedItem != null && (_localpaginator.SelectedItem.Disabled || _localpaginator.SelectedItem.IsGroupHeader))
if (_localpaginator.TotalCountValid > 0 && _localpaginator.SelectedItem != null && (_localpaginator.SelectedItem.Disabled || _localpaginator.SelectedItem.IsGroupHeader))
{
_localpaginator.UnSelected();
if (!defvalue.HasValue)
Expand Down Expand Up @@ -398,18 +399,18 @@ public override void InputTemplate(ScreenBuffer screenBuffer)
var indentgroup = string.Empty;
if (item.IsGroupHeader)
{
if (item.IsSeparationline)
if (item.IsSeparator)
{
value = new string('-', _lengthSeparationline);
switch (item.SeparationType.Value)
switch (item.SeparatorType.Value)
{
case SeparationLineType.SingleLine:
case SeparatorLine.SingleLine:
value = new string(_options.Symbol(SymbolType.SingleBorder)[0], _lengthSeparationline);
break;
case SeparationLineType.DoubleLine:
case SeparatorLine.DoubleLine:
value = new string(_options.Symbol(SymbolType.DoubleBorder)[0], _lengthSeparationline);
break;
case SeparationLineType.Char:
case SeparatorLine.Char:
value = new string(item.CharSeparation.Value, _lengthSeparationline);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PPlus.Controls
/// Represents Type Separation line
/// </summary>

public enum SeparationLineType
public enum SeparatorLine
{
/// <summary>
/// Single Line
Expand Down
Loading

0 comments on commit ddcd589

Please sign in to comment.