Skip to content

Commit

Permalink
Update API naming
Browse files Browse the repository at this point in the history
  • Loading branch information
slang25 committed Sep 15, 2023
1 parent 81b80f9 commit 7145357
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/Spectre.Console/Prompts/List/IListPromptStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ internal interface IListPromptStrategy<T>
/// <param name="scrollable">Whether or not the list is scrollable.</param>
/// <param name="cursorIndex">The cursor index.</param>
/// <param name="items">The visible items.</param>
/// <param name="searchFilter">The search filter.</param>
/// <param name="searchText">The search text.</param>
/// <returns>A <see cref="IRenderable"/> representing the items.</returns>
public IRenderable Render(IAnsiConsole console, bool scrollable, int cursorIndex,
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string searchFilter);
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string searchText);
}
6 changes: 3 additions & 3 deletions src/Spectre.Console/Prompts/List/ListPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ListPrompt(IAnsiConsole console, IListPromptStrategy<T> strategy)
public async Task<ListPromptState<T>> Show(
ListPromptTree<T> tree,
SelectionMode selectionMode,
bool searchFilterEnabled,
bool searchEnabled,
int requestedPageSize,
bool wrapAround,
CancellationToken cancellationToken = default)
Expand All @@ -40,7 +40,7 @@ public async Task<ListPromptState<T>> Show(
}

var nodes = tree.Traverse().ToList();
var state = new ListPromptState<T>(nodes, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize), wrapAround, selectionMode, _strategy.ShouldSkipUnselectableItems, searchFilterEnabled);
var state = new ListPromptState<T>(nodes, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize), wrapAround, selectionMode, _strategy.ShouldSkipUnselectableItems, searchEnabled);
var hook = new ListPromptRenderHook<T>(_console, () => BuildRenderable(state));

using (new RenderHookScope(_console, hook))
Expand Down Expand Up @@ -112,6 +112,6 @@ private IRenderable BuildRenderable(ListPromptState<T> state)
scrollable, cursorIndex,
state.Items.Skip(skip).Take(take)
.Select((node, index) => (index, node)),
state.SearchFilter);
state.SearchText);
}
}
20 changes: 10 additions & 10 deletions src/Spectre.Console/Prompts/List/ListPromptState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ internal sealed class ListPromptState<T>
public bool WrapAround { get; }
public SelectionMode Mode { get; }
public bool SkipUnselectableItems { get; private set; }
public bool SearchFilterEnabled { get; }
public bool SearchEnabled { get; }
public IReadOnlyList<ListPromptItem<T>> Items { get; }
private readonly IReadOnlyList<int>? _leafIndexes;

public ListPromptItem<T> Current => Items[Index];
public string SearchFilter { get; private set; }
public string SearchText { get; private set; }

public ListPromptState(IReadOnlyList<ListPromptItem<T>> items, int pageSize, bool wrapAround, SelectionMode mode, bool skipUnselectableItems, bool searchFilterEnabled)
public ListPromptState(IReadOnlyList<ListPromptItem<T>> items, int pageSize, bool wrapAround, SelectionMode mode, bool skipUnselectableItems, bool searchEnabled)
{
Items = items;
PageSize = pageSize;
WrapAround = wrapAround;
Mode = mode;
SkipUnselectableItems = skipUnselectableItems;
SearchFilterEnabled = searchFilterEnabled;
SearchFilter = string.Empty;
SearchEnabled = searchEnabled;
SearchText = string.Empty;

if (SkipUnselectableItems && mode == SelectionMode.Leaf)
{
Expand Down Expand Up @@ -118,14 +118,14 @@ public bool Update(ConsoleKeyInfo keyInfo)
};
}

var search = SearchFilter;
var search = SearchText;

if (SearchFilterEnabled)
if (SearchEnabled)
{
// If is text input, append to search filter
if (!char.IsControl(keyInfo.KeyChar))
{
search = SearchFilter + keyInfo.KeyChar;
search = SearchText + keyInfo.KeyChar;
var item = Items.FirstOrDefault(x => x.Data.ToString()?.Contains(search, StringComparison.OrdinalIgnoreCase) == true && (!x.IsGroup || Mode != SelectionMode.Leaf));
if (item != null)
{
Expand All @@ -152,10 +152,10 @@ public bool Update(ConsoleKeyInfo keyInfo)
? (ItemCount + (index % ItemCount)) % ItemCount
: index.Clamp(0, ItemCount - 1);

if (index != Index || SearchFilter != search)
if (index != Index || SearchText != search)
{
Index = index;
SearchFilter = search;
SearchText = search;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Prompts/MultiSelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem

/// <inheritdoc/>
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex,
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string stateSearchFilter)
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string searchText)
{
var list = new List<IRenderable>();
var highlightStyle = HighlightStyle ?? Color.Blue;
Expand Down
26 changes: 13 additions & 13 deletions src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public sealed class SelectionPrompt<T> : IPrompt<T>, IListPromptStrategy<T>
public SelectionMode Mode { get; set; } = SelectionMode.Leaf;

/// <summary>
/// Gets or sets a value indicating whether or not the search filter is enabled.
/// Gets or sets a value indicating whether or not search is enabled.
/// </summary>
public bool SearchFilterEnabled { get; set; }
public bool SearchEnabled { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellat
{
// Create the list prompt
var prompt = new ListPrompt<T>(console, this);
var result = await prompt.Show(_tree, Mode, SearchFilterEnabled, PageSize, WrapAround, cancellationToken).ConfigureAwait(false);
var result = await prompt.Show(_tree, Mode, SearchEnabled, PageSize, WrapAround, cancellationToken).ConfigureAwait(false);

// Return the selected item
return result.Items[result.Index].Data;
Expand Down Expand Up @@ -132,12 +132,12 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem
}

var scrollable = totalItemCount > requestedPageSize;
if (SearchFilterEnabled || scrollable)
if (SearchEnabled || scrollable)
{
extra += 1;
}

if (SearchFilterEnabled)
if (SearchEnabled)
{
extra += 1;
}
Expand All @@ -157,7 +157,7 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem

/// <inheritdoc/>
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex,
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string stateSearchFilter)
IEnumerable<(int Index, ListPromptItem<T> Node)> items, string searchText)
{
var list = new List<IRenderable>();
var disabledStyle = DisabledStyle ?? Color.Grey;
Expand Down Expand Up @@ -193,14 +193,14 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
text = text.RemoveMarkup().EscapeMarkup();
}

if (stateSearchFilter.Length > 0 && !(item.Node.IsGroup && Mode == SelectionMode.Leaf))
if (searchText.Length > 0 && !(item.Node.IsGroup && Mode == SelectionMode.Leaf))
{
var index = text.IndexOf(stateSearchFilter, StringComparison.OrdinalIgnoreCase);
var index = text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
var before = text.Substring(0, index);
var match = text.Substring(index, stateSearchFilter.Length);
var after = text.Substring(index + stateSearchFilter.Length);
var match = text.Substring(index, searchText.Length);
var after = text.Substring(index + searchText.Length);

text = new StringBuilder()
.Append(before)
Expand All @@ -215,16 +215,16 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,

list.Add(grid);

if (SearchFilterEnabled || scrollable)
if (SearchEnabled || scrollable)
{
// Add padding
list.Add(Text.Empty);
}

if (SearchFilterEnabled)
if (SearchEnabled)
{
list.Add(new Markup(
stateSearchFilter.Length > 0 ? stateSearchFilter.EscapeMarkup() : ListPromptConstants.SearchPlaceholderMarkup));
searchText.Length > 0 ? searchText.EscapeMarkup() : ListPromptConstants.SearchPlaceholderMarkup));
}

if (scrollable)
Expand Down
4 changes: 2 additions & 2 deletions src/Spectre.Console/Prompts/SelectionPromptExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ public static SelectionPrompt<T> WrapAround<T>(this SelectionPrompt<T> obj, bool
/// <param name="obj">The prompt.</param>
/// <param name="enabled">Whether the search filter should be enabled.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static SelectionPrompt<T> SearchFilter<T>(this SelectionPrompt<T> obj, bool enabled = true)
public static SelectionPrompt<T> Search<T>(this SelectionPrompt<T> obj, bool enabled = true)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

obj.SearchFilterEnabled = enabled;
obj.SearchEnabled = enabled;
return obj;
}

Expand Down

0 comments on commit 7145357

Please sign in to comment.