DefaultCollectionNavigatorMatcher.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// Default implementation of <see cref="ICollectionNavigatorMatcher"/>, performs
  4. /// case-insensitive (see <see cref="Comparer"/>) matching of items based on
  5. /// <see cref="object.ToString()"/>.
  6. /// </summary>
  7. internal class DefaultCollectionNavigatorMatcher : ICollectionNavigatorMatcher
  8. {
  9. /// <summary>The comparer function to use when searching the collection.</summary>
  10. public StringComparison Comparer { get; set; } = StringComparison.InvariantCultureIgnoreCase;
  11. /// <inheritdoc/>
  12. public bool IsMatch (string search, object? value) { return value?.ToString ()?.StartsWith (search, Comparer) ?? false; }
  13. /// <summary>
  14. /// Returns true if <paramref name="key"/> is key searchable key (e.g. letters, numbers, etc) that are valid to pass
  15. /// to this class for search filtering.
  16. /// </summary>
  17. /// <param name="key"></param>
  18. /// <returns></returns>
  19. public bool IsCompatibleKey (Key key)
  20. {
  21. Rune rune = key.AsRune;
  22. return rune != default && !Rune.IsControl (rune);
  23. }
  24. }