DefaultSearchMatcher.cs 896 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.IO;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. class DefaultSearchMatcher : ISearchMatcher {
  7. string [] terms;
  8. public void Initialize (string terms)
  9. {
  10. this.terms = terms.Split (new string [] { " " }, StringSplitOptions.RemoveEmptyEntries);
  11. }
  12. public bool IsMatch (IFileSystemInfo f)
  13. {
  14. //Contains overload with StringComparison is not available in (net472) or (netstandard2.0)
  15. //return f.Name.Contains (terms, StringComparison.OrdinalIgnoreCase);
  16. return
  17. // At least one term must match the file name only e.g. "my" in "myfile.csv"
  18. terms.Any (t => f.Name.IndexOf (t, StringComparison.OrdinalIgnoreCase) >= 0)
  19. &&
  20. // All terms must exist in full path e.g. "dos my" can match "c:\documents\myfile.csv"
  21. terms.All (t => f.FullName.IndexOf (t, StringComparison.OrdinalIgnoreCase) >= 0);
  22. }
  23. }
  24. }