DefaultCollectionNavigatorMatcher.cs 1.1 KB

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